How do I combine two recorded macros into one without re-recording them from scratch?
Answer
:let @q = @a . @b
Explanation
Macros in Vim are stored as plain text in named registers. Because registers are just strings, you can programmatically concatenate them with :let @q = @a . @b. This creates a new macro in register q that runs everything in @a followed by everything in @b — no re-recording required.
How it works
@aand@bare register contents (the raw keystroke sequences)- The
.operator concatenates strings in Vimscript - The result is assigned to
@q, which can then be executed with@q - You can also prepend or append to an existing macro:
:let @a = 'prefix' . @a
Example
Suppose you have:
@a = moves to start of line and capitalizes the first word
@b = moves to end of line and adds a semicolon
Instead of re-recording both actions as one, combine them:
:let @q = @a . @b
@q
Now @q performs both transformations in sequence.
Tips
- Inspect a macro's raw contents with
:echo @ato see the keystroke sequence before concatenating - Use
:put ato paste registeraas text, edit it in the buffer, then yank it back with0"ay$— a complementary workflow for more complex edits - Prepend a safety reset:
:let @q = '0' . @a . @bto always start from column 1 - Works for any registers, not just named ones — though avoid overwriting special registers like
@/(search) unintentionally