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.
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.
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:
Increment the second 1 by placing the cursor on it and typing CTRL-a. You should have this:
Now stop the recording by pressing q
Now, “play it back” by pressing @q and you should have:
Repeat it 7 times with 7@q and you will have a 1-10