How do you use a macro to append a comma to every line?
qaA,<Esc>jq
Record a macro that moves to end of line with A, types a comma, escapes to normal mode, and moves down.
qaA,<Esc>jq
Record a macro that moves to end of line with A, types a comma, escapes to normal mode, and moves down.
qA...q
Use uppercase register letter qA to append to macro a instead of overwriting.
qaI1. <Esc>jq
Start at line 1, record a macro that inserts 1.
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.
u after macro (single undo)
When a macro makes multiple changes, a single u undoes the entire macro as one unit.
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.
Paste with "ap and execute keys manually
To debug a macro, paste its contents into the buffer, read each keystroke, and execute them one at a time to find where the macro goes wrong.
qaI"<Esc>A": "",<Esc>jq
This macro transforms a plain word into a JSON key-value pair format, useful for converting lists of field names into JSON structure.
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.
qaA;<Esc>jq
This macro appends a semicolon to the current line and moves down, ready to repeat.
qaI// <Esc>jq
This macro adds a // comment prefix to the beginning of the current line and moves down.
qa I1. <Esc>j q
This simple macro inserts a list number prefix at the beginning of each line.
: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.
: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