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

Search a word, change one, repeat with dot.

Keys: *, c, w, n, .

*cwNEWEsc, then 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
{key:*}Cursor on word: search next exact match
{key:c}{key:w}Change word โ€” Vim drops you in insert mode
`NEW`Type replacement
{key:Esc}Back to normal mode โ€” change is saved as the dot-target
{key:n}Jump to next match
{key:.}Apply the same change
{key:n}Skip this one โ€” just move to next
{key:.}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 ยท cwbarEsc ยท 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.

Watch

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