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: Vim's :bufdo runs one command in every open file

Vim
~/tips/tip-vim-bufdo

:bufdo %s/oldhost/newhost/ge | update

Applies the same change to all buffers in one go.

Open a handful of files with vim *.conf, and you can change all of them with one command instead of visiting each.

:bufdo %s/oldhost/newhost/ge | update

:bufdo runs the command after it in every buffer in the buffer list. Here that is a substitute across the whole file. The e flag stops Vim from complaining in files without a match, and update saves the file only if it changed.

That | update matters. Without it, :bufdo has to move to the next buffer while the current one has unsaved changes, and Vim refuses unless hidden is set. Then you’d still need :wa afterwards.

There are siblings for other lists:

:windo set number
:argdo %s/foo/bar/ge | update

:windo works on every visible window, :argdo on the files you named on the command line. I prefer :argdo, because the buffer list also contains files you opened along the way, like a help page or a file you jumped into with gf.

Check :ls before you run it, and use the c flag in the substitute if you want to confirm each change.

Further reading: Vim help: :bufdo, Vim help: :argdo

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