Vim: multi-file replace `:%s`

9 Dec 2022

So you wanna run vim’s s command on multiple files. I gotchu.

  1. Open all files you want to replace as buffers

    • :e test/*
  2. Run the command on all files and update the content

    • :bufdo %s/heading/root_text/g | update
  3. Don’t forget to overwrite if you are satisfied with the results

    • :bufdo w

Done! Note that this is not just limited to replacing. It applies to any command, or set of commands.

If you already have some buffers open and you’d rather make a new list of files, you can replace: