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!

  4. Pavan
    December 14th, 2010 | 8:02 am

    Thanks. It was very helpful.

  5. Harry
    February 15th, 2011 | 5:39 pm

    Really Helpful!

Leave a reply