Vi and Vim, Macros

Vi and Vim have a "macro" feature to help automate routine editing tasks.

Sometime, you get a document, a file, or some data that's just messed up looking, or was formatted for printing, and you need to reformat it.

Most editors have some kind of macro function, where repetitive tasks can be automated. Unfortunately, these macros have, over the years, acquired an acute case of featureitis. Vim keeps it simple.

Unlike MS Word's Visual Basic, or even Excel's macros, which require some programming, vim's macros simply play back keystrokes. (There is a built-in programming language for vim, but that's a different feature.)

To record a macro, press q, then press a key to name the macro. The macro is named by a single character - you can't have a long name. You can use the letter keys and number keys. (If you use the number "2", it's easy to run the macro.)

Then, type your keystrokes.

When you're done, press q again.

To run the macro, press @ and then the name of the macro. If you selected "2", you can keep the shift key pressed down and type "@" again, and it'll still run macro 2. (My personal preference is to use the "q" key.)


The typical use of a macro is to reformat data. For example, I got some tabular data in a word processor. I couldn't easily extract the data, at first, but eventually figured out that it could be copied to the NVU HTML editor, which has a slightly better "copy and paste" function.

I pasted the tabular data into Vim, and got this:


Joe Blow


Harry Carey


Mary Christmas


Ann R. Key

I wanted one number and name per line. The macro was "JJJ[Enter]". It joins the number and name, then the third join closes up the gap below the line. The enter key moves the cursor down to the next line.

To execute this, I run the macro over and over, until I get"

1 Joe Blow
2 Harry Carey
3 Mary Christmas
4 Ann R. Key


A macro can run another macro. That allows you to run a macro over and over.

If you're writing a macro named "a", and you type "@a" at the end of the macro, the macro will call itself. This will cause the macro to be run over and over!

To stop the "infinite loop", press Control-C.

Typically, I don't bother with loops. Instead, I create a second macro that calls the first macro several times. The macro might be "@q@q@q@q@q" to run the "q" macro five times. Then, run that second macro several times, by hand. Often, there are small variations in the data that will cause the macro not to work perfectly, and I have to fix it up by hand.

If there's less than a hundred lines, it's easier to just type the 200 or so keystrokes to get the job done, rather than get complex.


One reason why vi/vim macros are more powerful (and popular) than macros in other editors, is because vi/vim has modal editing. When you're moving around the file, you are typically moving through lines and words, not characters. Joining and splitting lines, deleting words and lines, and moving to the front and end of the line, and of the file, are single keystrokes.

This is a slightly higher level of abstraction than most editors, and vi/vim forces you to use these abstractions. When coupled with macro recording, the macros are that much more powerful, because you can move around the file more precisely.

Related Articles