Automating Repetitive Text Editing with Vim Macros

Don’t you just hate it when you have to do some repetitive text editing, such as wrapping several lines of text in <li>...</li> tags or just about anything involving tables? (Actually, HTML is a veritable goldmine of repetition…) If you use vim, you can whip up macros on-the-fly to reduce this sort of tedium.

The commands to know are:

  • q — Start or stop macro recording. When starting, it should be used with a letter from a to z, which is the register it will record into. (So you can have more than one macro in memory at once.) The command is used singularly to end recording.
  • @ — Play back the macro. Use it in the form of @a to play the macro in register a.
  • @@ — Play the last-played macro again.

Rather than explain it in textual form, I recorded a short screencast that illustrates the basics of using macros in vim.