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

How do I run a command on every open buffer?

Answer

:bufdo command

Explanation

The :bufdo command executes an Ex command on every buffer in the buffer list. This is the buffer-level equivalent of :windo and :argdo.

How it works

  • :bufdo command runs command in each buffer
  • :bufdo %s/old/new/ge replaces text across all buffers
  • :bufdo update saves all modified buffers

Example

Replace var with const in all open files:

:bufdo %s/var/const/ge | update

Tips

  • The e flag suppresses errors when pattern is not found
  • :bufdo update saves only modified buffers (better than :wall)
  • :set hidden is recommended when using :bufdo
  • :argdo and :cfdo are alternatives for specific file sets
  • :windo runs commands only in visible windows

Next

How do you yank a single word into a named register?