vimtricks.wiki Concise Vim tricks, one at a time.

How do I record one edit and replay it on the next two lines with a macro?

Answer

qqgUiwjq2@q

Explanation

Macros are strongest when the edit pattern is stable but too awkward for a one-liner substitute. qqgUiwjq2@q records a small transformation once, moves to the next line, then replays it with a count. This gives you fast batch editing without leaving Normal mode.

How it works

  • qq starts recording into register q.
  • gUiw uppercases the inner word under the cursor.
  • j moves to the next line so the macro naturally advances.
  • q stops recording.
  • 2@q replays the macro two more times.

Because movement is baked into the macro, each replay lands on the next target line automatically. This is the key pattern for reliable macro design: perform the edit, then move to where the next edit should begin.

Example

Starting text:

alpha one
beta two
gamma three

Run:

qqgUiwjq2@q

Result:

ALPHA one
BETA two
GAMMA three

Tips

  • Replace 2@q with 99@q when you want to run until an error stops replay.
  • Use @@ to repeat the last macro once more.
  • Keep macros deterministic: avoid jumps that depend on hidden folds or search state.

Next

How do I reindent the previous visual selection and keep it selected?