Change-Next-Match Loop โ cgn
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 |
|---|---|
| {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.
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