cgn โ Change-Next-Match Loop
Like *cw, but . jumps AND changes in one step.
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.
| Key | Note |
|---|---|
| /old | Search for the thing you want to change |
| Enter | |
| cgn | Change next match |
| NEW | Type replacement |
| Esc | Commit |
| . | Jump to next match AND change it |
| . | Again |
| n | Skip a match โ move to the one after |
| . | Change it |
Worked example โ * then cgn then . . .
Change-next-match โ fewer keystrokes than star-cw-dot.
cgn = c + gn (motion: 'next match'). Dot replays the whole thing โ search-and-change in one keystroke.
Watch
- ๐บ #0465 yyp โ Duplicate Line (not yet published)
- ๐บ #0466 Star-Dot Pattern (not yet published)
See also: {key:*}, {key:cw}, then {key:.} {key:.} {key:.} โ The Replace Loop, Repeat Last Change, The Vim Grammar