ViM Macro

Once in a while, I need to manipulate a list of items, and it would be much easier to use a Macro. Under Windows, I use to use the Record and Playback features available in Textpad. Unfortunately, Textpad is not available for Linux. But, ViM does the same thing (and more), once you learn it.

In this example, I received a list of fields that my customer wanted to put in an HTML form. So, I needed to take a list like the following:

First
Last
Phone

And then turn that list into an HTML form like the following:

<label>First:</label><input type="text" name="First">
<label>Last:</label><input type="text" name="Last">
<label>Phone:</label><input type="text" name="Phone">

Assuming the list is more than just 3 items, there are two challenges here. First, we need to type text before and after the field, and second, we need to copy the name of the field so that it shows up in the label and as the name of the field. Below are the keystrokes I used to accomplish this.

Now that the macro is recorded, you can just replay the macro from register 9 with @9. You'll notice that the macro will automatically move the the next line. Repeat the process with @9 or even @@.

Usually, I just hit @@ to the end of the file at this point and I'm done. You can also repeat the macro 10 times by typing 10@9. You could even try 999@9 to repeat to the end of the file.