Jeff Roberts
RHCE #804006066322833
Vim-Fu is now iPhone and Android friendly

Check out the Vim-Fu Store!

Vim-Fu

using macros in VIM

Repetitive tasks can be annoying and are easily mitigated using the macro functionality in VIM.

For our simple example, let’s count 1 to 10 with each number on a new line.  This will make use of the Auto-increment functionality, discussed in a previous post entitled increment/decrement numbers in VIM, which is CTRL-a and yy, which yanks the current line into the cut and paste buffer and finally p to paste.  We will start by typing a 1 and then record the steps needed to drop to the next line and increment the number by 1, ten times.  These are the steps:

Place the first line in by hand.

1

Now, start recording the macro by typing q and any letter a-z.  I use qq for speed, which records to the register q and if I need to record another macro, leaving the first one intact, I use qw, and so on.

qq

You should see “recording” at the bottom of VIM.

Next, cut and paste the first line to the second, with yy then p, which should give you:

1
1

Increment the second 1 by placing the cursor on it and typing CTRL-a. You should have this:

1
2

Now stop the recording by pressing q

Now, “play it back” by pressing @q and you should have:

1
2
3

Repeat it 7 times with 7@q and you will have a 1-10

2 comments to using macros in VIM

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>