vim tip: expression register + autoincrement + recording ….

Repost from my tumblelog:

I had a line like this:
    PARTITION p_op1 VALUES LESS THAN 150,
and I wanted to generate this:
    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 300,
    PARTITION p_op3 VALUES LESS THAN 450,
    PARTITION p_op4 VALUES LESS THAN 600,
    PARTITION p_op5 VALUES LESS THAN 750,
    PARTITION p_op6 VALUES LESS THAN 900,
    PARTITION p_op7 VALUES LESS THAN 1050,
    PARTITION p_op8 VALUES LESS THAN 1200,
    PARTITION p_op9 VALUES LESS THAN 1350,
    PARTITION p_op10 VALUES LESS THAN 1500,
    PARTITION p_op11 VALUES LESS THAN 1650,
    PARTITION p_op12 VALUES LESS THAN 1800,
    PARTITION p_op13 VALUES LESS THAN 1950,
    PARTITION p_op14 VALUES LESS THAN 2100,
    PARTITION p_op15 VALUES LESS THAN 2250,
    PARTITION p_op16 VALUES LESS THAN 2400,
    PARTITION p_op17 VALUES LESS THAN 2550,
    PARTITION p_op18 VALUES LESS THAN 2700,
    PARTITION p_op19 VALUES LESS THAN 2850,
    PARTITION p_op20 VALUES LESS THAN 3000,

How to do it the vim way:
Copy the line we originally had and multiply the value by 1, so that we have this:

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op1 VALUES LESS THAN 150 * 1,

place cursor at the beginning of the line (cursor position is highlighted): _

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op1 VALUES LESS THAN 150 * 1,

start recording into register a: qa
increment partition number: ctrl+a
Now we have:

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 1,

Move to the comma: f,

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 1,

Move one position left: h

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 1,

Increment the number 1 by 1: ctrl+a

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Copy & paste line: yyp

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Move back to the end of the previous line: k$

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Delete expression into register n: “nd3b

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN ,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Enter insert mode: i
Enter expression mode: ctrl+r =
Paste deleted expression: ctrl+r n
Insert result of expression: <Enter>

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 300,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Exit insert mode and move to the beginning of the next line: <esc>2_

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 300,
    PARTITION p_op2 VALUES LESS THAN 150 * 2,

Stop recording: q
Repeat what has been recorded as many times as you want, for instance, 10 times: 10@a

    PARTITION p_op1 VALUES LESS THAN 150,
    PARTITION p_op2 VALUES LESS THAN 300,
    PARTITION p_op3 VALUES LESS THAN 450,
    PARTITION p_op4 VALUES LESS THAN 600,
    PARTITION p_op5 VALUES LESS THAN 750,
    PARTITION p_op6 VALUES LESS THAN 900,
    PARTITION p_op7 VALUES LESS THAN 1050,
    PARTITION p_op8 VALUES LESS THAN 1200,
    PARTITION p_op9 VALUES LESS THAN 1350,
    PARTITION p_op10 VALUES LESS THAN 1500,
    PARTITION p_op11 VALUES LESS THAN 1650,
    PARTITION p_op12 VALUES LESS THAN 1800,
    PARTITION p_op12 VALUES LESS THAN 150 * 12,

Complicated? Maybe, but a lot more fun and much faster than doing it manually ;-)

About these ads

4 Responses to vim tip: expression register + autoincrement + recording ….

  1. I am brazilian, and translate it for portuguese :)
    Very usefull!

  2. davitenio says:

    nice, I love screencasts ;-)

  3. Dan says:

    Wouldn’t it be easier to just increment by 150 each time?
    i.e.
    qx (Begin macro x recording)
    yyp (yank the whole line and paste it below
    ctrl-a (increment the p_op1 value to p_op2)
    w (skip a word, so we increment the next number)
    150ctrl-a (increment the final value by 150)
    q (end the macro recording)
    19@x (repeat macro x 19 times, for a total of 20 lines)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: