The following is a section that we left out of this week’s Mac DevCenter article What Is Vim (It’s Easier than You Think) because of length constraints. I hope you find it useful as one more point on your radar screen as you ponder the productivity that Vim may be able to add to your daily workflow.

Save Time With Vim Macros

Do you find yourself frequently whipping out the python interpreter or cooking up regular expressions in Perl just to munge some text? Even if you’re already really good, I bet Vim macros can still make you even better. A Vim macro is simply a set of keystrokes that you can record and use over again to perform repetitious tasks.

But wait a tick. “What’s wrong with regexes,” you ask? We’ll, consider this conventional wisdom:

Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems. –Jamie Zawinski, in comp.lang.emacs

Although I do commonly use regular expressions in production level code that I write, day-to-day events involving a quick transform of a text file hardly require the effort of cooking up a regex. Read on to see why.

Let’s say you have this text file that contains a list of several Dr. Seuss books and you want to convert it into a numbered HTML list, apply italics to the title only, and remove the initial numbering. Without using a WYSIWYG, what’s the quickest way you could do it?

Open the file in Vim and follow along to see if using a macro to make the changes would take less time than your normal routine.

When writing the macro, we want to leverage the commands available to us in normal mode to apply a generic fix to one line. We’ll save that sequence of keystrokes, and then apply it repeatedly to every other line in the file.

To get started, position your cursor in the upper left corner of the file and follow along. Because HTML tags have angled brackets in them already, I’ll explicitly state when to press enter and escape instead of trying to encode them as <cr> and <esc>.

  • Remove the initial numbering
    • xxxxx
  • Apply the opening HTML tags
    • i<li><em>
    • Press escape to go back to normal mode
  • Find the end of the title
    • /(
    • Press return to complete the command
    • h
  • Apply the closing tag around the title
    • i</em>
    • Press escape to go back to normal mode
  • Advance to the end of the line and apply the closing tag
    • $a</li>
    • Press escape to go back to normal mode
  • Advance the cursor to the beginning of the next line
    • j^

Once you understand the process, it’s a snap to turn it into a macro. Here’s all you do:

  • With your cursor on the beginning of the line you want to modify, press “q” to start the recording process, and then press any other letter to name your macro. Let’s use “w”. At this point, you should see “recording” appear at the bottom of your window
  • Follow the process outlined above to modify one of the lines and record the body of your macro
  • Press “q” again to stop the recording process

Now you have your macro mapped to the “w” key, and all you need to do to invoke it is to press “@w”. If you want to apply it repeatedly, a shortcut is to press “@@” after you’ve applied it an initial time, or you can even press a number followed by “@w” to apply it a successive number of times. For example, pressing “10@w” would apply the macro 10 times.

Simply finish off this exercise by wrapping the list items in an opening and closing <ol></ol> and you’re all done. While this first macro may have seemed like a bit of work, imagine that you already knew the keystrokes and how to record macros, and then reconsider the effort required; it’s fairly negligible.

I’d love to ramble on all evening about Vim stuff, but I bet you have some good tips of your own. Why not share them below and/or in the article’s Talk Back section?