vimtricks.wiki Concise Vim tricks, one at a time.

How do I append more commands to a recorded macro without re-recording it?

Answer

:let @q .= 'j.'

Explanation

When a recorded macro is almost right but missing one repeated step, re-recording from scratch is usually slower and riskier than patching the register directly. Vim stores macros as plain text in registers, so you can modify them with :let just like any other string. This is especially useful in long refactors where you discover one extra movement or repeat command after testing the macro a few times.

How it works

  • @q is the macro register you already recorded (from qq ... q)
  • .= means concatenate in place (append to existing content)
  • 'j.' appends two normal-mode keystrokes:
    • j moves to the next line
    • . repeats the most recent change on that line

After running the command, replaying @q executes the original macro plus this newly appended tail.

Example

Suppose you recorded a macro in q that edits one line but forgot to advance and repeat:

line one TODO
line two TODO
line three TODO

Append the missing behavior:

:let @q .= 'j.'

Now 3@q can walk downward and reapply your last change across multiple lines without re-recording the macro body.

Tips

  • Inspect macro contents first with :reg q
  • If you need to prepend instead, use :let @q = 'prefix' . @q
  • This technique is safer than re-recording when your macro already contains precise motions or searches

Next

How do I tune Vim diff so moved code blocks align more accurately?