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

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 hidden is recommended with :bufdo — it allows switching from unsaved buffers
  • Always use the e flag with :s in bufdo/windo to avoid errors on files without matches
  • | update saves each buffer only if it was modified — safer than | w
  • :windo is perfect for synchronizing window settings (scrollbind, diff, etc.)
  • :bufdo works on ALL loaded buffers, including hidden ones — be careful
  • :tabdo windo covers every window across all tabs — maximum coverage

Next

How do I quickly select an entire function body using visual mode?