How do I run the same command across all windows, buffers, or tabs?
Answer
:windo / :bufdo / :tabdo {command}
Explanation
Vim's do commands iterate over collections and execute a command in each context. These are essential for applying changes, settings, or operations across your entire editing session.
The three do-commands
| Command | Scope |
|---|---|
:windo |
Every visible window in the current tab |
:bufdo |
Every loaded buffer |
:tabdo |
Every tab page |
Examples
" Set line numbers in all visible windows
:windo set number
" Search and replace across all open buffers
:bufdo %s/old/new/ge | update
" Close all folds in every tab
:tabdo windo normal zM
" Enable diff mode in all visible windows
:windo diffthis
" Turn off syntax highlighting everywhere
:bufdo set syntax=off
" Save all modified buffers
:bufdo update
" Run a macro in all buffers
:bufdo execute 'normal @q' | update
Combining do-commands
" Run a command in every window of every tab
:tabdo windo set cursorline
" Run a substitution in every buffer and save
:bufdo %s/2024/2025/ge | update
Tips
:set hiddenis recommended with:bufdo— it allows switching from unsaved buffers- Always use the
eflag with:sinbufdo/windoto avoid errors on files without matches | updatesaves each buffer only if it was modified — safer than| w:windois perfect for synchronizing window settings (scrollbind, diff, etc.):bufdoworks on ALL loaded buffers, including hidden ones — be careful:tabdo windocovers every window across all tabs — maximum coverage