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

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

  • @a and @b are 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 @a to see the keystroke sequence before concatenating
  • Use :put a to paste register a as text, edit it in the buffer, then yank it back with 0"ay$ — a complementary workflow for more complex edits
  • Prepend a safety reset: :let @q = '0' . @a . @b to always start from column 1
  • Works for any registers, not just named ones — though avoid overwriting special registers like @/ (search) unintentionally

Next

How do I open a file in read-only mode so I cannot accidentally modify it?