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.
:g/pattern/normal {cmd}
Combining :global with :normal lets you run any normal-mode keystrokes on every line that matches a pattern.
: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
:let @a = "value"
The :let @{reg} = expr command lets you assign any string or expression directly into a named register without entering insert mode or performing a yank.
@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
:set opfunc and g@
Vim's operatorfunc option lets you define your own operators — just like the built-in d, y, or c — that accept any motion or text object.
:[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}.
:norm
:normal (abbreviated :norm) executes a sequence of normal-mode keystrokes on each line of an address range.
: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.
:let @a = substitute(@a, 'old', 'new', 'g')
After recording a macro or yanking text into a named register, you may need to tweak it — fix a typo in a recorded macro, change a variable name in yanked tex
:put a ... edit ... "ayy
Recorded macros are stored as plain text in registers, but editing them by re-recording is tedious for complex sequences.