*, cw, then . . . โ€” The Replace Loop

Search a word, change one, repeat with dot.

Keys: *, c, w, n, .

*cwNEW Esc, then n.n.n. โ€” change every occurrence of a word, one at a time, with confirmation. The most-loved Vim refactoring loop.

*, cw, then . . . โ€” The Replace Loop

The classic Vim refactoring loop. Searches for a word, changes one instance, then jumps to each next match where you decide whether to dot-repeat (apply) or skip.

Step by step
KeyNote
*Cursor on word: search next exact match
cwChange word โ€” Vim drops you in Insert
NEWType replacement
EscBack to Normal โ€” change is saved as the dot-target
nJump to next match
.Apply the same change
nSkip this one โ€” just move to next
.Applyโ€ฆ

Worked example โ€” * cw . . .

Search-cursor-change-repeat โ€” mass rename.

Step 1 ยท
pattern.star-cw-dot
Step 2 ยท * ยท * โ€” search for 'foo'.
* โ€” search for 'foo'.
Step 3 ยท cwbar Esc ยท cwbar โ€” change to bar.
cwbar โ€” change to bar.
Step 4 ยท n . ยท n then . โ€” next match, dot.
n then . โ€” next match, dot.
Step 5 ยท n . ยท And again. The Replace Loop.
And again. The Replace Loop.

The classic Vim mass-edit. cgn is similar and even slicker โ€” see cgn-dot.

โ–ถ Try this in the simulator

Watch

See also: Word Search, Repeat Last Change, {key:cgn} โ€” Change-Next-Match Loop