cgn โ€” Change-Next-Match Loop

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
/oldSearch for the thing you want to change
Enter
cgnChange next match
NEWType replacement
EscCommit
.Jump to next match AND change it
.Again
nSkip a match โ€” move to the one after
.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 ยท cgnbar Esc ยท 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.

โ–ถ Try this in the simulator

Watch

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