开发者

vim: vnew, but accepting a buffer instead of a filename?

开发者 https://www.devze.com 2023-03-08 15:14 出处:网络
Currently, if I want to create a new window then load a buffer, I use :vnew 开发者_JAVA百科:buf foo.py. Is there one command which will do both?Yes, there is a command for that:

Currently, if I want to create a new window then load a buffer, I use :vnew 开发者_JAVA百科:buf foo.py. Is there one command which will do both?


Yes, there is a command for that:

:[N]sb[uffer] [N]                   :sb :sbuffer
        Split window and edit buffer [N] from the buffer list.  If [N]
        is not given, the current buffer is edited.  Respects the
        "useopen" setting of 'switchbuf' when splitting.  This will
        also edit a buffer that is not in the buffer list, without
        setting the 'buflisted' flag.

You may find these ones useful also:

:[N]sbn[ext] [N]                                    :sbn :sbnext
        Split window and go to [N]th next buffer in buffer list.
        Wraps around the end of the buffer list.  Uses 'switchbuf'

:[N]sbN[ext] [N]            :sbN :sbNext :sbp :sbprevious
:[N]sbp[revious] [N]
        Split window and go to [N]th previous buffer in buffer list.
        Wraps around the start of the buffer list.
        Uses 'switchbuf'.

The problem with both commands is that they will split horizontally. You can precede them with :vert[ical], but that breaks your one command paradigm :-)

Anyway, :vert sb foo.py is not that much of typing, and if you really use it often, you might want to consider creating a map for it. Maybe something like:

cnoremap ,sb vert sb 


Just tell :vnew the path to the file:

:vnew foo.py

Edit:

As sidyll said, there is no built in command that splits the window vertically to edit a buffer, then I have created a new ex commands which does what you want:

command! -nargs=1 -complete=buffer -bang Vbuffer vnew | buf<bang> <args>

The ! after command will replace an old :Vbuffer if it exists (you can remove that since I have added it for testing), -nargs=1 means that the new command accepts 1 argument which is passed to the :buf command with <args>, -complete=buffer will suggest buffer names as you type tab for the argument name and -bang indicates that the new command accepts the ! option which is also passed to the :buf command with <bang>.

Just add that line to your ~/.vimrc and re-:source it. ;-)

0

精彩评论

暂无评论...
验证码 换一张
取 消