How do I see what keystrokes a macro contains?
:reg a
The :reg a command shows the contents of register a, which reveals the keystrokes stored in the macro.
:reg a
The :reg a command shows the contents of register a, which reveals the keystrokes stored in the macro.
q{a-z}...q
Recording a macro captures a sequence of keystrokes into a register, which you can replay later.
qa ci"replacement<Esc> /next<CR> q
Macros can contain any Vim command including text objects, searches, and multi-key motions.
:g/pattern/norm @a
The :g/pattern/norm @a command combines the global command with macro execution.
qa ... j@bj q
You can create macros that call other macros, applying different operations to alternate lines or creating complex editing patterns.
:let @a = "Iprefix: \<Esc>"
The :let @a = ".
Use :let i=1 with macro
By combining a Vimscript variable with a macro, you can create sequences with incrementing numbers.
"ap, edit, "add
Since macros are stored in registers, you can paste the register content into the buffer, edit it, and yank it back.
:'<,'>normal @q
The :'normal @q command runs macro q on every line of the visual selection.
:let @a=getline('.')<CR>@a
How it works Instead of recording keystrokes interactively, you can write a sequence of Vim commands as plain text in your buffer and then execute that text as
:if condition | execute 'normal cmd' | endif
How it works Vim macros can include Ex commands with conditional logic.
:%s/foo/bar/g | %s/baz/qux/g | w
The (bar) character in Vim's command line acts as a command separator, allowing you to chain multiple ex commands together on a single line.
qa0f=20i <Esc>20|C= <Esc>lDjq
How it works Aligning text on a delimiter such as = without plugins requires a clever macro technique.
qa{edits}@bq
How it works Vim macros can call other macros, creating a modular system of reusable building blocks.
qama{edits}'aq
How it works When a macro needs to jump to different parts of the file and then return to a starting position, marks are the perfect tool.
qa<C-r>=expression<CR>q
How it works The expression register (=) lets you evaluate Vimscript expressions and insert the result.
qabi"<Esc>ea"<Esc>wq
How it works This macro wraps the current word in double quotes and moves to the next word, making it easy to repeat across a line or file.
qa:s/old/new/g<CR>jq
How it works You can combine Ex commands like :s (substitute) with macro recording to create powerful repeatable find-and-replace operations that go beyond what
qa0f:dwj0q
How it works When recording a macro that you plan to repeat across multiple lines, the key technique is to end the macro positioned on the next line, ready for
qaq
How it works To clear a macro register, you simply start recording into that register and immediately stop.