Vim macros let you record a sequence of keystrokes and play them back, making repetitive editing tasks incredibly efficient.

Let’s see this in action. Imagine you have a file with a list of items, and you want to add a prefix to each line.

apple
banana
cherry
date

We want to transform it into:

item_apple
item_banana
item_cherry
item_date

Here’s how you’d do it with a Vim macro:

  1. Start recording: Press q followed by a register letter (e.g., q again). The status line will show recording @q.
  2. Perform the edit:
    • i (enter insert mode)
    • item_ (type the prefix)
    • <Esc> (exit insert mode)
    • j (move down to the next line)
  3. Stop recording: Press q again. The recording @q message disappears.

Now, to replay this macro on the remaining lines:

  • Repeat once: Press @q. The first line will be transformed: item_apple.
  • Repeat multiple times: Press 3@q. This will apply the macro three more times, transforming the remaining lines.

The final result:

item_apple
item_banana
item_cherry
item_date

Vim macros solve the problem of tedious, repetitive text manipulation. Instead of manually typing the same commands over and over, you record them once and then execute them as many times as needed. This dramatically speeds up editing, especially for large files or complex transformations.

Internally, Vim stores the sequence of keystrokes you made in a named register (like @q). When you execute @q, Vim simply replays those keystrokes as if you were typing them yourself. The power comes from Vim’s ability to execute these recorded sequences on multiple lines or in conjunction with other Vim commands.

The exact levers you control are:

  • Recording Register: Choosing which register (a through z) to store your macro in. You can have up to 26 different macros at once.
  • Playback Count: Specifying how many times to repeat a macro using a number prefix (e.g., 5@q).
  • Macro Chaining: You can even execute one macro from within another, though this can quickly become complex.
  • Editing Macros: If you make a mistake during recording, you can often fix it by re-recording the problematic part or by manually editing the register content (though this is advanced).

When you record a macro, Vim captures all keystrokes, including navigation. This means if you record yourself moving down 5 lines and then stop, playing that macro back will always move you down 5 lines, regardless of where your cursor is. It’s crucial to record actions that are relative to the current cursor position or that operate on the line the cursor is on, unless you intend to record absolute movements. For example, recording j moves down one line from the current position, while recording 5G moves to line 5, which is an absolute movement.

If you want to edit a macro after recording it, you can yank the content of the register into a buffer and edit it there. For instance, to see what’s in register q, you can type :let @q in command mode. To yank it to a new buffer, you can type :put q into a new file. You can then edit it and put it back into the register using :let @q = "your_edited_macro_string".

The next concept to explore is using Vim’s visual mode within macros to operate on selections.

Want structured learning?

Take the full Vim course →