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

How do I run the same Ex command in every open window at once?

Answer

:windo {cmd}

Explanation

:windo {cmd} executes an Ex command in every window in the current tab page, cycling through each one and applying the command before returning focus to the original window. This is indispensable for operations like enabling line numbers, syncing scrolling, or running a substitute across all visible splits simultaneously.

How it works

  • :windo iterates over all windows in the current tab, moving to each in turn
  • The supplied {cmd} is executed in every window's context (including its buffer)
  • Focus returns to the window you started in after the iteration

Related commands for other contexts:

  • :bufdo {cmd} — runs across all listed buffers
  • :tabdo {cmd} — runs across all tabs
  • :argdo {cmd} — runs across the argument list

Example

Enable relative line numbers in every split at once:

:windo set relativenumber

Search and replace in every visible buffer:

:windo %s/old_api/new_api/ge

The e flag silences errors on windows whose buffer doesn't contain the pattern.

Tips

  • If the command modifies buffers you don't want to save, combine with :update (windo update) to only write files that have been changed
  • To run a normal-mode command across all windows, combine with :normal: :windo normal gg
  • :windo diffthis activates diff mode on all windows; :windo diffoff turns it off

Next

How do I browse the full commit history of the current file using vim-fugitive?