Skip to content
Vim

Macros (Recorded Keystrokes)

Record a sequence of keys and replay it to automate repetitive edits.

By EZ4Code Team
macroautomationregister

Code

" Record into register q
qa                  " start recording into q
0f,x                " example: go to line start, find comma, delete it
jq                  " move to next line, stop recording

" Replay
@q                  " replay q once on current line
5@q                 " replay q 5 times
@@                  " replay last used macro
:'a,'bnormal @q     " run q on every line from mark a to b
:%normal @q         " run q on every line in the file

" Editing a macro: paste the register, edit, yank back
:let @q = "0f,xj"   " assign macro as a string
"ip                  " insert register i contents for editing

" Recursive macros (end with @q to loop) — useful but advanced

Explanation

Macros turn repetitive edits into one keystroke. q followed by a register name (a–z) starts recording; q again stops. Replay with @register. @@ repeats the last. For ranges, use :'<,'>normal @q on a visual selection, or :%normal @q for the whole file. Because macros live in registers, you can paste them with "qp, edit the text, then yank back with "qy to refine. End macro lines with j so they advance; if a motion fails (e.g. f, finds no comma) the macro aborts — which is often desirable for stopping at EOF.

More Vim Snippets