level 10
Macros
Record a sequence of edits and replay it across lines.
The dot command repeats your last change. A macro repeats your last routine — a whole sequence of moves and edits, replayed on command. This is the answer to the task that would otherwise eat your afternoon: “make this same fiddly edit in fifty places.” Record it once, fire it fifty times, go get coffee.
Recording and Replaying
q{letter} → start recording into a register (qa)
q → stop recording
@{letter} → replay the macro (@a)
@@ → repeat the last macro
5@a → replay 5 times
Tips
- Start from a known position — macros replay exactly what you recorded, so keep them positionally consistent.
- End with a motion to the next target (often
j) so each replay lands ready for the next. - Use
ninside a macro to jump by search when targets aren’t evenly spaced.
Use the dot command for a single repeated change; reach for a macro when you need to chain several commands together.
Your first macro — add semicolons
- On line 1, press
qato start recording into registera. - Press
A, type;, pressEsc. - Press
jto move down, thenqto stop recording. - Press
7@ato apply it to the remaining lines.
goal Record A; Esc j once, then replay it down the block.
Wrap each value in quotes
- On
red, pressqa. - Press
I, type", pressEsc. - Press
A, type",, pressEsc. - Press
j, thenqto stop. - Press
5@afor the remaining lines.
goal A macro can mix line-start and line-end edits.
Prefix each line
- On line 1, press
qa. - Press
I, typeasync, pressEsc. - Press
j, thenq. - Press
4@ato apply to the rest.
goal Insert at the line start, then move on.
Step through with @@
- On line 1, record
qa: pressI, type- [ ], pressEsc, pressj, pressq. - Press
@aon line 2. - Press
@@to repeat on line 3. - Press
@@again for lines 4 and 5 — inspecting each result as you go.
goal Replay once with @a, then @@ to repeat one item at a time.
Capstone — a macro that searches and edits
The lines start differently, so columns won’t help and the targets aren’t adjacent — this is the job the dot command can’t finish on its own. Each getValue("...") needs to become a bare fetch(): rename the call and empty the arguments. Bake the search into the macro so every replay finds its own next target.
- Press
ggto start from a known spot. - Record:
qa, then/getValueand Enter to land on the first call. - Press
ciw, typefetch, pressEsc— the name is changed. - Press
f(, thenci(andEsc— the argument is gone, leavingfetch(). - Press
qto stop. - Press
3@a— the search makes each replay hunt down the next call by itself.
goal When targets aren't aligned, search inside the macro and do two edits per hit.