Using Vim Recording to Make Macros

If you’re a frequent Vim user, like me, you’re probably attracted to its ability to navigate and otherwise manipulate your document easily through the use of keyboard commands — I know I’m certainly spoiled by never having to use the mouse!

But you may also find yourself using the same key commands repeatedly, especially when fixing common mistakes. For example, as a former technical writer and Markdown-user, I often found myself having to add backticks (`) around commands. This process, when done manually in Vim, looks like this:

  • Move to the front of the command
  • Hit i to enter insert mode
  • Add the first backtick
  • Hit Esc to exit insert mode
  • Hit e to move to the end of the command
  • Press a to enter insert mode after the last character
  • Add the closing backtick
  • Exit insert mode

Wouldn’t it be nicer if I could just hit, say, @c to perform all those steps instead?

Vim recordings let us do just that, by allowing us to set macros that can be reused repeatedly through the document (and later documents).

Create a Recording

Let’s go ahead and use the following Jinja lookup table as an example to learn how to set a macro:

{% set apache = salt['grains.filter_by']({
'Debian': {
package: apache2,
service: apache2,
configfile: /etc/apache2/apache2.conf,
configsource: salt://apache/config/debian-apache2.conf,
user: www-data,
group: www-data,
},

If you’re familiar with Jinja, you’ll notice that this is improperly formatted; our keys (like package) and our values (like apache2) need to be encased in single quotes. Now, we could manually go in and add each single quote. This process could be about as nasty as the one detailed above. Instead, we’re going to set a recording.

When adding a Vim recording, we’ll use the following process:

  • Press q to signal that we’re assigning a recording to a key
  • Select the key we want to assign the macro to
  • Perform the steps you want recorded
  • Press q again to end and save the recording

So let’s go ahead and create the macro for formatting the above lookup table.

  • First, we want to move our cursor to the start of the package: apache2 line. Press q, then w to record our macro to the w key.
  • Next, we need to perform the macro itself, which I’ve broken down by keystroke:
  • Press w to move to the front of the word package
  • Enter insert mode (i)
  • Insert the first single quote (')
  • Esc insert mode
  • Press e to move to the end of the key (again, this is package)
  • Enter insert mode by pressing the a button to append to the end of the key
  • Insert the next single quote (')
  • Esc insert mode
  • Press w again to move to the start of the value
  • Enter insert mode (i)
  • Add the single quote (')
  • Esc insert mode
  • Use $ to move to the end of the line
  • Enter insert mode (i)
  • Add the final single quote (')
  • Press j to move to the next line
  • Press 0 to move to the beginning of that line
  • We can now end the recording by pressing q again.
Run the Recording

To ensure that the recording works, move to the beginning of the next line (service), then press @, then w, which is where we assigned our recording. The service line is now formatted!

We can even take this a step further. See how we moved to the start of the next line at the end of the command? That’s so we can run something like 4, @, w — this will run our macro four times, finishing out the reformatting of our lookup table!

As long as we don’t record over our w macro (and haven’t changed Vim from using the default nocompatible mode), we can also use this in other files where we might have the same or similar issues. Personally, I keep macro records for bolding, italicizing, and adding backticks around commands in Markdown, although the options are near-endless.

View Recordings

We also have the option to view our available recordings. We can view all of them by running:

:registers

Or just a single one by running:

:reg 

For example, let’s take a look at what our w recording looks like:

:reg w
—- Registers —-
“w wi’^[ea’^[wi’^[$i’^[j0

The ^[ keystrokes denotes where we’ve hit our Esc key.

And that’s it! You can now record macros within Vim to speed up any coding, editing, writing, or otherwise that you’re working on from within the text editor. And if you’re already a frequent macro user, feel free to share how you use Vim recording to speed up your processes in the comments below!

Looking for team training?