How do I define or edit a macro's content programmatically without re-recording it?
:let @q = 'keystrokes'
You can assign a string directly to any register using :let, turning it into a macro instantly.
:let @q = 'keystrokes'
You can assign a string directly to any register using :let, turning it into a macro instantly.
:silent! %normal @q
Combining :silent!, the % range, and :normal @q gives you a powerful pattern for applying a macro across an entire file while gracefully skipping lines that don
v:count1
The v:count1 variable holds the count typed before a command, defaulting to 1 if no count was given.
:{range} normal @{reg}
The :normal command lets you execute Normal mode keystrokes over a range of lines.
matchlist()
matchlist({str}, {pattern}) runs a regex match and returns a list of all captured groups, making it the idiomatic way to extract structured data from strings in
getchar()
getchar() blocks and waits for the user to press a key, returning its numeric character code.
:let @a = substitute(@a, "old", "new", "g")
When a recorded macro has a typo or wrong command buried inside it, you don't have to re-record the entire thing.
:let @a = '...'
When a recorded macro contains a typo or needs a small tweak, you can modify it directly via the :let command rather than re-recording the entire sequence.
:normal! {cmd}
When you use :normal {cmd} in a Vimscript function, macro, or Ex command, Vim expands any keys through the user's current mappings first.
:let @q = substitute(@q, "old", "new", "g")
Vim macros are stored as plain text in registers, which means you can inspect and modify them like any other string.
1000@q
Vim macros stop executing the moment any step in the macro causes an error — a failed search, a motion that cannot proceed, or a substitution with no matches.
@+
In Vim, @{register} executes the contents of any register as a macro.
qQ (or any uppercase register letter)
When recording a macro with qq, you can append additional keystrokes to the existing macro by recording into the uppercase version of the same register.
<C-r>=input('Enter: ')<CR>
By embedding =input('prompt: ') inside a recorded macro, you can pause the macro at any point to ask for user input and insert the result.
:let @a = @a . "\<CR>extra"
Vim stores macros as plain text in registers — the same registers used for yanked text.
qQ
Recording a macro with an uppercase register letter appends to the existing macro in the corresponding lowercase register instead of overwriting it.
:let @q = @a . @b
Macros in Vim are stored as plain text in named registers.
reg_recording() and reg_executing()
Vim exposes two built-in functions for querying the current macro state: regrecording() and regexecuting().
:call setreg('q', keys, 'c')
The setreg() function writes any string directly into a named register, letting you construct macro keystrokes from Vimscript expressions rather than live recor
qq{actions}@qq
A recursive macro ends by calling itself, so it loops automatically without you pressing @q repeatedly.