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

How do I call one macro from inside another macro?

Answer

@a (within macro @b)

Explanation

Vim macros can call other macros, enabling modular macro composition. By recording @a as part of macro @b, you create reusable macro building blocks. This is a powerful technique for complex multi-step transformations.

How it works

  • Record macro a with a reusable operation: qa{commands}q
  • Record macro b that calls @a: qb{commands}@a{more_commands}q
  • When @b runs, it executes @a inline as part of its sequence
  • Nesting can go multiple levels deep

Example

" Macro a: swap two words
qa dwwPq

" Macro b: swap words and move to next line
qb @a j q

" Run macro b on 10 lines
10@b
Before:        After 3@b:
hello world    world hello
foo bar        bar foo
baz qux        qux baz

Tips

  • Recursive macros: qa...@aq calls itself, running until an error stops it
  • Store utility macros in registers you rarely use (e.g., z, y, x)
  • Modify the called macro: changes to @a automatically affect @b's behavior
  • Use @: within a macro to repeat the last Ex command

Next

How do I return to normal mode from absolutely any mode in Vim?