35

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
    Have you ever tried :sp(lit) or :vs(plit) with no argument? :) – yo' Feb 3 '15 at 18:57
44

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.

| improve this answer | |
  • 2
    Is there a less verbose way to open an existing buffer in a new vertical split? Something like :v sb maybe? – zepp.lee Apr 28 '18 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 '18 at 14:14
  • 1
    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 at 7:33
3

:(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
| improve this answer | |
  • 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 '19 at 18:29
  • 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 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 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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