47

I know it's possible to open a new file in a split, with :split or :vsplit, but is it possible to open a new split with an existing buffer, potentially leveraging autocomplete?

1
  • 2
    Have you ever tried :sp(lit) or :vs(plit) with no argument? :)
    – yo'
    Feb 3, 2015 at 18:57

2 Answers 2

62

The sbuffer command will let you create a split with an existing buffer name or number. :sb# will open a split with buffer number # (as displayed in the :buffers list). :sb foo will open a split with the buffer named foo. Tab-complete will cycle the available buffer names.

You can use :vertical sb... if you want a vertical split instead of the default, horizontal, one.

3
  • 2
    Is there a less verbose way to open an existing buffer in a new vertical split? Something like :v sb maybe?
    – zepp133
    Apr 28, 2018 at 13:52
  • 2
    I believe “vertical” can only be shortened to “vert” so if you want something more concise you’ll have to make a custom command/function/etc
    – user72
    Apr 28, 2018 at 14:14
  • 2
    I use this mapping to do the job. ls lists all opened buffers, so you don't have to remember the name :) nnoremap <leader>vb :ls<cr>:vertical sb<space>
    – Mattia72
    Feb 3, 2020 at 7:33
8

:(v)split <name_of_buffer> or :(v)split #<buffer_number>

This answer shows a shorter way to @user72's answer above and @zepp.lee's comment.

For example, you can open buffer 2 in a vertical split with

:vsp #2

or

:vsp test.js
3
  • 1
    Welcome to this site @alexhg! We encourage our users to create detailed answers to make sure they are as useful as possible to other readers: here it would be useful if you explained why you suggest using this solution, you could also link to the doc. For now your answer has been flagged as being "low quality" but you can use the "edit" button to fix that.
    – statox
    Sep 2, 2019 at 18:29
  • 3
    The :(v)sp #<buffer_number> does work correctly but typically I don't know the buffer number I want. Using the name of the buffer is again something I don't typically know as it requires the full path of the buffer. The question was also hoping for auto-completion, the :vert sb <buffer_stub> followed by <tab> will auto-complete the buffer for you, that doesn't work for :vsp unfortunately.
    – icc97
    Sep 21, 2020 at 3:30
  • Autocompletion with <tab> does work for me if I type the name of a buffer (without having to type the full path). I ignore if it's due to my additional settings.
    – alexhg
    Sep 22, 2020 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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