The joy of :bufdo

If you use vim, then you may have discovered that its documentation, while extensive, is nearly impenetrable. One operation I often have to do is

$ for x in [list of files]; do
> sed -e ‘[some substitution]’ < $x > $x.bak
> mv $x.bak $x
> done
since most Unix-based editors don’t offer multi-file replace. It turns out that, sometime between 5.3 and 6.x, vim has added the

:bufdo command, so this operation is much simpler as

  • you needn’t leave the editor, and
  • you needn’t maintain a list of files, provided you’ve opened them all in the editor. I’m posting to share this small tip, since when I canvassed a few folks at the office, no one seemed to have heard of

:bufdo. (See “impenetrable documentation”, above.)