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

How do I add more steps to an existing macro without re-recording it from scratch?

Answer

qQ

Explanation

When you realize a recorded macro is missing a step at the end, you don't need to re-record the whole thing. Using an uppercase register letter to start recording appends to the existing macro rather than replacing it. If your macro is in register q, typing qQ begins an append recording session.

How it works

  • q{register} — starts recording into the specified register (replaces existing content)
  • q{REGISTER} — starts recording with an uppercase letter, which appends to the register
  • So qQ appends new keystrokes to whatever is already stored in register q
  • Press q again to stop recording, just as normal

This works because uppercase and lowercase letters refer to the same named register, but uppercase signals an append operation — the same mechanism used with "Ayy to append yanked text.

Example

Suppose you recorded qq to add a semicolon and move down:

qq          " start recording into q
A;<Esc>     " append semicolon
j           " move down
q           " stop recording

You later realize you also need to move to the beginning of the line. Instead of re-recording:

qQ          " append to macro q
0           " go to beginning of line
q           " stop recording

Now @q does: append semicolon → move down → go to line start.

Tips

  • Works with any named register (az): qA, qB, etc.
  • View the full macro contents with :reg q before and after to confirm the append
  • Use :let @q = @q . "\<Esc>" for programmatic appends when the content involves special keys

Next

How do I use capture groups in Vim substitutions to rearrange or swap matched text?