How do I open an existing buffer in a new split window?
Answer
:sb {buffer}
Explanation
The :sb (short for :sbuffer) command opens a buffer that is already loaded in Vim in a new horizontal split window. This is useful when you want to view a file you have previously opened without leaving your current window or navigating away from your current position.
How it works
:sb Nopens buffer numberNin a new horizontal split:sb filenameopens the buffer matchingfilename(supports partial matching and tab completion):vert sb Nopens bufferNin a new vertical split instead- The buffer must already be loaded (visible in
:ls)
Example
First, check which buffers are loaded:
:ls
Output:
1 %a "main.go" line 42
2 # "handlers.go" line 1
3 "utils.go" line 15
To open utils.go (buffer 3) in a horizontal split:
:sb 3
Or by name:
:sb utils
For a vertical split:
:vert sb 3
Tips
- Use tab completion with
:sbto cycle through available buffer names :sballopens all buffers in horizontal splits- This is faster than
:split | b Nsince it combines both steps into one command