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

How do I make Vim reuse an existing window when jumping to a buffer instead of opening a new split?

Answer

:set switchbuf=useopen

Explanation

The switchbuf option controls how Vim decides where to display a buffer when switching to it via commands like :sb, :cc, :cn, quickfix jumps, or <C-]>. By default, Vim opens the buffer in the current window (replacing whatever's there). Setting switchbuf=useopen tells Vim to reuse an existing window that already has the buffer loaded instead of replacing the current window or opening a new split.

How it works

switchbuf accepts a comma-separated list of flags:

  • useopen — if the buffer is already visible in any window in the current tab, jump to that window
  • usetab — extend the search to all tab pages (not just the current tab)
  • split — if the buffer is not open, open it in a new horizontal split (instead of replacing current window)
  • vsplit — same as split, but opens a vertical split
  • newtab — open in a new tab page

A common, highly useful combination:

set switchbuf=useopen,usetab

With this, quickfix entries, :sb {buf}, tag jumps, and similar commands all prefer a window that's already showing the target buffer — across all tabs — rather than duplicating it or stomping on your current edit.

Example

You have foo.c open in window 1 and bar.c in window 2. Cursor is on window 2. You run :cn to jump to a quickfix error in foo.c:

  • Without useopen: Vim opens foo.c in window 2, replacing bar.c
  • With useopen: Vim jumps to window 1 (where foo.c is already open) instead

Tips

  • Put set switchbuf=useopen,usetab in your vimrc to make this permanent
  • split and vsplit flags are useful for LSP/quickfix workflows where you want a new split only if the buffer isn't already visible
  • :help switchbuf lists all valid flag values

Next

How do I insert the entire current line into the command line without typing it?