201

If you are editing a file in Vim and then you need to open an existing buffer (e.g., from your buffer list: :buffers), how can you open it in a vertical split?

I know that you already can open it with a normal split like:

:sbuffer N

Where N is the buffer number you want. However, the above opens that N buffer horizontally, not vertically.

I'm also aware that you can change the window placement after opening and have a vertical split like so:

Ctrl + W, H
Ctrl + W, L

Which will vertically split the window to the right or the left.

It seems to me that if there is a sbuffer there should be a vsbuffer, but that doesn't exist (not that I am aware of).

Also, please note that I am not looking for a plugin to solve this question. I know about a wealth of plugins that will allow you to do this.

I am sure I might be missing something that is already there.

I have created a simple function with a mapping if someone else stumbles across this issue and do not want to install a plugin:

Function:

" Vertical Split Buffer Function
function VerticalSplitBuffer(buffer)
    execute "vert belowright sb" a:buffer
endfunction

Mapping:

" Vertical Split Buffer Mapping
command -nargs=1 Vbuffer call VerticalSplitBuffer(<f-args>)

This accomplishes the task of opening a buffer in a right split, so for buffer 1, you would call it like:

:Vbuffer 1

6 Answers 6

233

Try:

:vert sb N

which will open a left vertical split (by default, unless you have modified some options).

To open a split to the right, on the other hand:

:vert belowright sb N
3
  • 6
    doesn't it seem rather odd to not have vsbuffer N ? Annoying. Your answer nails it. Thanks! Commented Dec 31, 2010 at 19:03
  • 13
    I always feel like there should be a vsbuffer too, and I also often forget Ctrl-w T to open a buffer in a new tab (or I want to do that with a buffer that's not currently active or visible). So as an alternative, you can use a bar for either of these cases, which I find easier to remember than @Jeet's valid answer: :vsp | b N and :tabe | b N.
    – ches
    Commented Sep 12, 2011 at 12:16
  • I like that this command allows N to be autocompleted (which doesn't seem possible with the command in the other answer). Both upvoted, nevertheless. Commented Jul 29, 2015 at 21:44
135

:vsp | b1

1 being some buffer number. Use buffers to list all buffers.

Here's some additional info on splits, if you're interested. Link

4
  • 10
    You could also use :ls, which seems to be a shortcut for :buffers.
    – dskecse
    Commented Oct 19, 2014 at 19:16
  • 5
    you can also do :vsp | b <buffer name> Commented Jun 14, 2016 at 17:52
  • 5
    @KennyBambridge even better - you only have to type part of the buffer name (case sensitive) and then hit tab to cycle through the filtered list
    – icc97
    Commented May 13, 2017 at 22:21
  • Bear in mind that although the Link provided by @Jerinaw is good the terminology on that page is seriously misleading so you'll need to read the comments on that page as well to clarify.
    – NeilG
    Commented Dec 31, 2019 at 0:06
11

The answer to the OP that I found most useful is embedded deep in Jerinaw's answer and a comment on it, and in Wolfson's answer. But I felt it might be brought out more. (Nor have those been voted most highly, even though they seemed to me the ones that answered OP best.)

The answer to the question, Why is there not :vsbuffer, is that there is. It's called :vsplit and does the trick either as

:vsplit NameOfBuffer

OR

:vsplit #NumberOfBuffer.

(In this second use, take care to note that the hash # is significant. If you want to get to buffer number 3, you need to say :vsplit #3, not just :vsplit 3 which will instead create a new file named "3".)

Again, this answer is embedded above, it's just not brought out clearly enough for the quick scanner, IMV.

2
  • 1
    Doesn't seem to work. It makes a new file with the number/name. Commented Dec 4, 2020 at 20:03
  • When using NumberOfBuffer, say it's 3, did you put in :vsplit #3? Without the hash, :vsplit 3 will indeed make a new file with name 3? (That is, the hash is required.)
    – Danny Quah
    Commented Dec 4, 2020 at 23:12
8

You can ease your pain by adding the following to your .vimrc

cabbrev vb vert sb

Now you can use it in the following way.

:vb <buffer>
1
  • this is nice, the extended version offers also buffers name autocompletion! I didn't know cabbrev too, thanks.
    – kidpixo
    Commented Aug 27, 2019 at 12:34
4

You can also combine :ls that lists your current buffers and the commands to open the desired buffer in either

  1. current window: :b <N/bufname>
  2. vertical split: :vsp | b <N/bufname>
  3. horizontal split: :sp | b <N/bufname>

For this, I've added the following mappings to my ~/.vimrc (order of mappings represents the above list of desired windows)

 nnoremap <leader>b :ls<cr>:b<space>
 nnoremap <leader>v :ls<cr>:vsp<space>\|<space>b<space>
 nnoremap <leader>s :ls<cr>:sp<space>\|<space>b<space>

Based on this, you can see the buffer list as soon as you hit

  1. <leader>b
  2. <leader>v
  3. <leader>s

and then just enter the desired buffer number N. This will then open the buffer in the desired window. You can of course still use a part of the buffer name bufname as well.

I mapped the <leader> to , based on

let mapleader = ","

For some people (e.g. me) this could even replace plugins like MiniBufExpl and thus save space on the screen.

1

You can use Neovim, like this:

autocmd FileType python nmap <F5> :rightbelow vertical split <bar> :term python %<cr>

Not the answer you're looking for? Browse other questions tagged or ask your own question.