vimtricks.wiki Concise Vim tricks, one at a time.

How do I run a command across all open buffers at once?

Answer

:bufdo %s/old/new/ge | update

Explanation

The :bufdo command executes an Ex command in every loaded buffer. This is perfect for applying the same change to all files you currently have open, without needing to set up an argument list or quickfix list first. Just open the files you want to modify and run your command across all of them.

How it works

  • :bufdo — Iterates through every buffer in the buffer list, executing the command in each one.
  • %s/old/new/ge — Substitutes old with new across the entire buffer. The e flag suppresses errors when no match is found in a buffer.
  • | update — Saves the buffer only if changes were made.

Example

You have three files open and want to update a copyright year in all of them:

:bufdo %s/Copyright 2025/Copyright 2026/ge | update

Or set consistent file encoding across all open buffers:

:bufdo set fileencoding=utf-8 | update

Tips

  • Always use the e flag with :bufdo substitutions — without it, the command stops at the first buffer with no matches.
  • Use :bufdo when you've already opened your target files. For pattern-based file selection, :argdo or :cfdo are better choices.
  • :windo is the window equivalent — it runs a command in every visible window.
  • :tabdo runs a command in every tab page's active window.
  • Combine with :buffers to verify which buffers are loaded before running :bufdo.

Next

How do I return to normal mode from absolutely any mode in Vim?