How do you use a macro to rename a variable throughout a function?
qa*Ncgn<C-r>=newName\n<Esc>q
Record a macro using * to search for the word under cursor, cgn to change the next match, type the new name.
45 results for "macro record qa"
qa*Ncgn<C-r>=newName\n<Esc>q
Record a macro using * to search for the word under cursor, cgn to change the next match, type the new name.
q{a-z}...q
Recording a macro captures a sequence of keystrokes into a register, which you can replay later.
qa Yp <C-a> q
By combining a macro with (increment number), you can quickly generate numbered sequences.
qa...@aq
A recursive macro calls itself at the end of its recording, causing it to repeat until a motion or search fails.
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
qaI// <Esc>jq
This macro adds a // comment prefix to the beginning of the current line and moves down.
qaA;<Esc>jq
This macro appends a semicolon to the current line and moves down, ready to repeat.
qa f,ldt,F(p q
This macro swaps two comma-separated arguments inside parentheses by cutting the second argument and placing it before the first.
qaq
How it works To clear a macro register, you simply start recording into that register and immediately stop.
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.
let @a = 'macro_contents'
Recorded macros are stored in registers, which are lost when you quit Vim (unless viminfo saves them).
Use :let i=1 with macro
By combining a Vimscript variable with a macro, you can create sequences with incrementing numbers.
: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
:if condition | execute 'normal cmd' | endif
How it works Vim macros can include Ex commands with conditional logic.
:'<,'>normal @a
The :'normal @a command executes the macro stored in register a on every line within the current visual selection.
@@
How it works After running a macro with @a (or any other register), you can repeat that same macro by pressing @@.
:nnoremap <leader>x :norm! @a<CR>
Once you've perfected a macro by recording and testing it, you can make it permanent by converting it into a mapping in your vimrc.
qaciw"<C-r>""<Esc>wq
This macro wraps the current word in double quotes by changing the word, inserting quotes around the original content, and moving to the next word.
qaI<li><Esc>A</li><Esc>jq
This macro wraps each line in tags by inserting the opening tag at the start and appending the closing tag at the end.
qa<C-r>=expression<CR>q
How it works The expression register (=) lets you evaluate Vimscript expressions and insert the result.