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

How do I record a macro that uppercases a word and advances so I can replay it across words?

Answer

qagUiwWq2@a

Explanation

Macros are most powerful when they encode both the edit and the movement to the next target. qagUiwWq2@a is a compact pattern for that: you record one transformation, then replay it across subsequent words without rethinking navigation.

How it works

  • qa starts recording into register a
  • gUiw uppercases the inner word under the cursor
  • W jumps to the start of the next WORD, preparing the next replay target
  • q stops recording
  • 2@a replays the macro two more times

Because the movement is built into the macro, each replay lands in the correct place automatically. This is much more reliable than manually moving between runs, and it scales to larger batches with a bigger count (10@a, for example).

Example

Before:

alpha beta gamma delta

Run:

qagUiwWq2@a

After:

ALPHA BETA GAMMA delta

Tips

  • If a macro should continue until failure, you can use a high replay count and let motion failure stop it
  • Keep the macro idempotent when possible so accidental extra runs are easy to recover from

Next

How do I move the current split into a new tab and immediately jump back to the previous tab?