brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: cgn and . in Vim replace matches one at a time

Vim
~/tips/tip-vim-cgn-dot

/oldName

Changes the next search match, then repeats it where you choose.

:%s changes every match at once. To look at each one and decide, use cgn and the dot command.

/oldName
cgnnewName<Esc>
.

First search for the word. gn is a motion that selects the next match of the last search, so cgn changes it: the match disappears and you type the replacement. Press Escape, and that whole change is now what . repeats.

Each . jumps to the next match and changes it as well. To skip one, press n to move past it, then . again at the next one you want.

I like this a lot more than :%s/x/y/gc with its question for every match. You see the surrounding code, can scroll, and nothing changes until you press the dot.

It works with dgn too, which deletes the next match, and with * instead of a typed search: put the cursor on a word, press *, then cgn.

Remember that the search pattern decides what matches. /name also finds filename. Use * or write /\<name\> if you only want the whole word.

Further reading: Vim help: gn, Vim help: repeating with .

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Vim tips :%s/<Ctrl-r><Ctrl-w>/newname/g /oldname find . -name "*.conf" | vim – vim --clean file.txt :set textwidth=72 All Vim tips (45) All topics

Link