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

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 N opens buffer number N in a new horizontal split
  • :sb filename opens the buffer matching filename (supports partial matching and tab completion)
  • :vert sb N opens buffer N in 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 :sb to cycle through available buffer names
  • :sball opens all buffers in horizontal splits
  • This is faster than :split | b N since it combines both steps into one command

Next

How do I run a search and replace only within a visually selected region?