Skip to content

Lesson 5 of 9 · 25m

Macros That Survive Real Files

Recording, replaying, editing, and debugging macros — plus the failure modes that turn fifty automated edits into cleanup work.

A macro is a recording of keystrokes stored in a register. That implementation detail explains everything: macros are fast because they are literal, fragile because they are literal, and editable because registers are just text.

Record the smallest complete transformation

qa       start recording into register a
...      perform the edit once
q        stop recording
@a       replay register a
@@       replay the last replayed macro
10@a     replay it ten times

The right macro handles one unit of work and ends in a position where the next unit can begin. For example, transform one CSV row, then move to the next row as the final step. A macro that edits the row but leaves the cursor in the middle of it will eat the file on the second replay.

Build macros with search, not distances

Suppose you are converting:

user_email = raw_input['email']
user_name = raw_input['name']

into constructor arguments. Do not count columns. Record operations like 0 to the line start, f= to the equals sign, ct<Space> or text objects, and / to find stable anchors. Search-based macros survive ragged whitespace and varying names; distance-based macros do not.

A robust sequence often starts with 0 or ^ and ends with j0. Normalize before transforming, then position for the next iteration.

Replay across a visual range

:'<,'>normal! @a

Select lines visually, run that command, and every line executes the macro in normal mode. normal! is important: it bypasses your custom mappings, so the macro means the same thing tomorrow as it did when you recorded it. Plain :normal can silently become a different macro after you install plugins.

For the whole file, use a global command instead:

:g/^user_/normal! @a

Now the macro applies only to matching lines. This is usually safer than 999@a because the pattern, not your estimate of repetition count, controls the blast radius.

Edit a macro like text

Because the macro lives in register a, inspect it with :reg a or paste it into a scratch buffer with "ap. Fix the typo as text, then yank the corrected version back with "ayy or "ay over the exact region. This is not elegant, but it is faster than rerecording a long macro because one l should have been f;.

Failure modes are literal

If a search in a macro fails, replay stops. If a substitution prompts, the macro waits or consumes your next keystrokes. If the cursor shape is wrong — characterwise versus linewise — paste behavior changes. Test on one line, undo with u, adjust, then run the range.

Field notes

  • Make macros idempotent where possible: running twice should not duplicate text.
  • End with a movement that puts the cursor at the next unit, commonly j0.
  • Use q: for the command-line window when constructing complicated :g or :normal commands; you can edit the command with full normal-mode power.
  • Recursive macros (@a inside register a) are clever and dangerous. A failed final search is often the intended termination condition — which means any earlier failure also silently terminates the batch.

Was this lesson helpful?

Sign in to rate