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

How do I append keystrokes to an existing macro without re-recording it?

Answer

:let @q .= "A;\<Esc>"

Explanation

If a recorded macro is almost correct but missing a final step, re-recording from scratch is slow and error-prone. You can patch it directly by appending keystrokes to the macro register with :let. This is especially useful after long recordings where only the tail behavior needs adjustment.

How it works

:let @q .= "A;\<Esc>"
  • @q is the macro register you want to edit
  • .= appends text to the current register contents
  • \<Esc> inserts a real Escape key into the macro, not literal characters

After updating the register, run it with @q (or 10@q) as usual.

Example

Suppose macro q already appends a field at line end, but now you need every edited line to end with a semicolon too. Instead of recording again, append that behavior:

:let @q .= "A;\<Esc>"

Now each replay of @q performs the original steps and then adds ; at end-of-line. This keeps your iterative macro workflow fast: record once, patch many times.

Tips

  • Inspect a macro before editing with :put =@q
  • Replace (not append) with :let @q = "..." when needed
  • Use this with versioned snippets in your notes so complex macros are reproducible

Next

How do I inspect a register as a list of lines in Vimscript?