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

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

Answer

:let @q .= 'A;<Esc>'

Explanation

Re-recording a long macro just to add one extra step is slow and error-prone. A faster approach is to treat macro registers as strings and append additional keystrokes directly. This lets you iteratively refine automation in small increments, which is especially valuable when tuning repetitive edits on large files.

How it works

  • @q refers to register q, which can store a macro sequence
  • :let @q = ... sets macro contents explicitly
  • .= appends to the existing value instead of replacing it
  • 'A;<Esc>' is the extra Normal-mode keystroke sequence being appended

After appending, running @q executes the original macro plus the new suffix. You can keep extending macros this way and avoid going through full q{register} recording sessions each time.

Example

Start with a macro in q that prepends - to a line. Later you realize each line should also end with ;.

:let @q .= 'A;<Esc>'

Now @q performs both actions in order, and repeating with @@ applies the updated behavior across subsequent lines.

Tips

  • Use :echo @q to inspect raw macro contents before and after edits
  • You can build macros entirely from script by assigning full key sequences with :let @q = '...'

Next

How do I enable synchronized scrolling in every open window?