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— Substitutesoldwithnewacross the entire buffer. Theeflag 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
eflag with:bufdosubstitutions — without it, the command stops at the first buffer with no matches. - Use
:bufdowhen you've already opened your target files. For pattern-based file selection,:argdoor:cfdoare better choices. :windois the window equivalent — it runs a command in every visible window.:tabdoruns a command in every tab page's active window.- Combine with
:buffersto verify which buffers are loaded before running:bufdo.