How do I split a complex Vim macro into reusable subroutines?
Record worker macro in @b, call it from @a with @b
Complex macros are hard to debug and maintain when crammed into a single register.
Record worker macro in @b, call it from @a with @b
Complex macros are hard to debug and maintain when crammed into a single register.
:echo strtrans(@q)
When a macro behaves unexpectedly, :echo strtrans(@q) reveals exactly what is stored in register q—including invisible control characters—as human-readable
:let @q='commands'
Macros in Vim are stored in registers as plain text.
:'<,'>norm @a
Combining :normal with a visual range lets you replay a macro on each line of a selection individually — far more targeted than recursive macros or @@ repeati
@q
A recursive macro is a macro that calls itself as its last step, causing it to loop automatically until an operation fails (such as reaching the end of the file
:[range]normal @q
The :[range]normal @q command replays the macro in register q on every line within a given range.
{count}@{register}
Prefix any macro execution with a count to repeat it that many times in a single command.
:let @a = "content"
When a recorded macro has a typo or needs a small tweak, you don't have to re-record it entirely.
:let @q =
Instead of recording a macro with q, you can assign any string directly to a named register using :let @{register} = 'keys'.
@"
Vim macros are stored in registers — and you can execute any register as a macro with @{register}.
:argdo norm @a | update
Combining :argdo with :norm @a lets you apply a recorded macro to every file in Vim's argument list — a powerful pattern for bulk refactoring across a project
:let @q = 'commands'
Macros in Vim are just text stored in named registers.
:let @q = "dwelp"
Recording macros with q works well for simple sequences, but complex macros with special keys can be hard to get right in one take.
qQ...q
When you record a macro into register q with qq.
:put a ... edit ... "ayy
Recorded macros are stored as plain text in registers, but editing them by re-recording is tedious for complex sequences.
qqqqqq{edits}@qq
A recursive macro calls itself at the end of its sequence, creating a loop that automatically repeats until a motion or command fails (such as hitting the last
qaq qa...@aq @a
A recursive macro calls itself at the end of its recording, causing it to repeat indefinitely until a command inside it fails (like a search hitting the end of
qaqqa{actions}@aq@a
A recursive macro is a macro that calls itself at the end of its recording.
qq;.q then @q or @@
The dot command (.
:wviminfo / :rviminfo
Vim can persist register contents (including macros) across sessions using viminfo (Vim) or shada (Neovim).