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

How do I make Vim reuse an already-open window or tab when jumping to a quickfix entry?

Answer

:set switchbuf=useopen,usetab

Explanation

By default, Vim opens a new window (or reloads the buffer in the current window) whenever you navigate to a quickfix entry, tag, or :buffer command — even if the target file is already visible in another split. Setting 'switchbuf' changes this behavior so Vim jumps to the existing window instead.

How it works

The 'switchbuf' option is a comma-separated list of flags:

Flag Effect
useopen Jump to any window in the current tab that already has the buffer open
usetab Jump to any window in any tab that already has the buffer open
split Open a new horizontal split if the buffer is not already visible
vsplit Open a new vertical split instead
newtab Open a new tab if the buffer is not already visible

Flags are evaluated left to right; the first match wins.

Example

Add to vimrc:

set switchbuf=useopen,usetab

Now when you press ]q (:cnext) to jump to the next quickfix entry, if that file is already open in a split, the cursor moves to that split rather than creating a duplicate window.

This also affects :buffer, :sbuffer, tag jumps, and :pop.

Tips

  • Combine with :set hidden to keep modified buffers loaded without forcing a save
  • Use switchbuf=useopen,vsplit if you always prefer vertical splits for new windows
  • The flag only applies to buffer-switching commands, not regular :edit or <C-w>f

Next

How do I highlight all occurrences of a yanked word without typing a search pattern?