Skip to content

Instantly share code, notes, and snippets.

@manuwell
Created March 30, 2015 11:12
Show Gist options
  • Save manuwell/c75cf0cac3f22dbc1780 to your computer and use it in GitHub Desktop.
Save manuwell/c75cf0cac3f22dbc1780 to your computer and use it in GitHub Desktop.
VIM find/replace multiple files

from: https://github.com/thoughtbot/til/pull/86/files

Search & Replace in multiple files

I recently had to replace static files path in multiple files from a project I were working on, after boggling my mind trying to do it with sed, I gave up and looked up how to do it using VIM, I was sure there was a way.

When VIM is started, you can specify multiple files to open as buffers from the terminal:

vim *.html

This will open all html files on the current directory as buffers.

  • You can list the buffers using :ls
  • You can add a buffer using :badd <filename>
  • You can close a buffer using :bdelete <buffer_number>

Now you can run commands on all open buffers:

:bufdo <command>

Searching all instances of foo and replacing with bar:

:bufdo %s/foo/bar/ge

Notice: the e on the replace command tells vim to ignore errors, otherwise vim would print an error on each file that doesn't have a match for foo

You can now open the buffers (:buffer <buffer_number>) and verify that the text was replaced.

But this command only replaced the text, we need to save the files to persist changes:

You can either run another bufdo with the update command to save the files:

:bufdo update

Or you can pipe it on the first command:

:bufdo %s/foo/bar/g | update

And that's it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment