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
awith a reusable operation:qa{commands}q - Record macro
bthat calls@a:qb{commands}@a{more_commands}q - When
@bruns, it executes@ainline 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...@aqcalls 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
@aautomatically affect@b's behavior - Use
@:within a macro to repeat the last Ex command