Change-Next-Match Loop โ€” cgn

Like *cw, but . jumps and changes in one step.

Keys: c, g, n, .

After a /search, cgn changes the next match. Then . by itself jumps to the next match and changes it โ€” no need for separate n. The pinnacle of dot-repeat refactoring.

cgn

cgn is the better version of the *cw loop. It rolls jump-to-next and change into a single dot-repeatable action.

The full workflow
KeyNote
{key:/}`old`Search for the thing you want to change
{key:Enter}
{key:c}{key:g}{key:n}Change next match
`NEW`Type replacement
{key:Esc}Commit
{key:.}Jump to next match and change it
{key:.}Again
{key:n}Skip a match โ€” move to the one after
{key:.}Change it

Worked example โ€” * then cgn then . . .

Change-next-match โ€” fewer keystrokes than star-cw-dot.

Step 1 ยท
pattern.cgn-dot
Step 2 ยท * ยท * โ€” pattern set.
* โ€” pattern set.
Step 3 ยท cgnbarEsc ยท cgn โ€” change next match.
cgn โ€” change next match.
Step 4 ยท . ยท . โ€” finds next AND changes it.
. โ€” finds next AND changes it.
Step 5 ยท . ยท One key per replacement.
One key per replacement.

cgn = c + gn (motion: 'next match'). Dot replays the whole thing โ€” search-and-change in one keystroke.

Watch

See also: The Replace Loop โ€” *, cw, then . . ., Repeat Last Change, The Vim Grammar