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 windowusetab— 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 assplit, but opens a vertical splitnewtab— 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 opensfoo.cin window 2, replacingbar.c - With
useopen: Vim jumps to window 1 (wherefoo.cis already open) instead
Tips
- Put
set switchbuf=useopen,usetabin yourvimrcto make this permanent splitandvsplitflags are useful for LSP/quickfix workflows where you want a new split only if the buffer isn't already visible:help switchbuflists all valid flag values