Vim: Substitute in all buffers

To replace all instances of FOO with BAR in one file, you’d do:

:%s/FOO/BAR/g|:up

The pipe | lets you separate multiple commands and :up is like :w but only writes changed buffers.

Now for all open files:

:bufdo :%s/FOO/BAR/g|:up

Roughly translates to: for all open buffers, substitute FOO with BAR and write the changes.

Comments

  1. RedBeard0531
    October 2nd, 2008 | 8:31 pm

    You may want to use :up[date] instead of :w[rite] so you only save files that are actually modified. this is how :wa[ll] works.

  2. October 3rd, 2008 | 11:40 am

    Proves I don’t know a whole lot of Vim commands! :)

    I updated the original text to use :up.

    Thanks!

  3. Lee
    October 4th, 2008 | 3:46 am

    Great tip, thanks!

Leave a reply