Search and replace in multiple files with Vim


Update: I have a newer version of this post available here

There are plenty of ways of doing a search and replace in multiple files in linux, but this is how I do it with vim.

$> vim firstfile secondfile theothers*
:all
:windo %s/this/that/g

The :all will split the window to show you all the files at once.
This would not be the best option for lots of files, but for a small number you get to see the changes that are made.
The :windo will execute the given command for all the files in the current window.
Note this wont affect windows in other tabs.

If you want to undo that change in all buffers, you can use the same method:

:windo undo

When your finished, you can save and quit with:

:wqall

There are plenty of variations, for more information checkout the help pages for these:

:tabdo

:bufdo

:argdo