Lately I've been drastically reducing the number of plugins I'm using so I'm looking for answers using only built-in Vim features.
I've been noticing a flaw in my workflow when I'm editing a lot of buffers: I am
on a buffer and I want to switch to another one of which I don't remember the
name and which isn't the alternative buffer. So using :b#
is not possible and
using the completion with the command :b
isn't convenient neither.
To do that I was doing the following:
- First use
:buffers
to get the list of open buffers - Remember the number of the buffer I want to use
- Press Enter to close the list of buffers
- Use
:bX
withX
the number of the buffer to use.
This workflow seemed pretty heavy to me so I added the following lines to my
.vimrc
:
" Improve the navigation in the buffers
nnoremap <Leader><S-b> :B<CR>
command! B call Buffers()
function! Buffers()
execute "buffers"
let buffer=input("Enter a buffer:")
execute "b" . buffer
endfunction
This give me a command :B
(and a mapping) which calls the function :buffers
wait for an input
and finally calls :b
followed by the input.
It works well but I'm surprised that I had to develop such a function: usually Vim has a pretty efficient default workflow so I'm wondering if there is a better way to switch to a buffer of which I don't know anything excepted that it as already been opened in my editing session.
:ls
followed by:b#
(don't need to hit return or escape first, can go directly from the buffer list in 'ls'.) (Posting as comment because it doesn't seem substantial enough to be an answer? Also wondering if I'm missing something and answering the wrong question.):b#
is not what I'm looking for but yeah the fact that you can save a keystroke by entering directly:b
without pressingenter
before is interesting. (Indeed that is maybe too light to be an answer, but it was worth a comment :-) )#
character as a stand-in for a (buffer) number, rather than the literal#
character.