Cycling between 2 buffer
You can use :b #
, <c-6>
, or <c-^>
to go between the current buffer and the alternative buffer (often the previous buffer).
For more help see:
:h alternative-file
:h :c_#
:h CTRL-6
We can do better! or The problem with cycling buffers
Cycling buffers is kind slow. I believe :bprevious
and :bnext
are only useful in a narrow set of conditions:
- These commands become useful after you use more than 2 buffers (probably due to
<c-6>
/<c-^>
).
- Once you hit a certain buffer number threshold, there is sort of an upper limit on the usefulness of cycling with these commands. Is it faster to cycle forward? Backwards? Does it matter because it simply takes too long either way?
Instead of cycling with :bp
and :bn
you can jump directly to a buffer via :b
command. Simply use :b {partial_name}<tab>
.
Behold the power of :b
:
- Uses
<tab>
completion
- Use
<c-d>
to list out completion
- Use partial file name. e.g.
:b foo
. Works great with <tab>
.
- Globbing. e.g.
:b foo*bar
or :b foo/**/bar
- Split variant of
:b
is :sb
.
- Also accepts a buffer number
- A common mapping:
nnoremap <leader>b :ls<cr>:b<space>
For more help see:
:h :b
:h :ls
:h cmdline-completion
:h file-searching
Can we do better than :b
?
Skip the buffer management completely and use tags, cscope, and/or GNU Global. These will help you go directly to where you want to go not just the right buffer with where ever you last left the cursor.
For beginners to tags I suggest Gutentags and :h tags
.
You can also use :find
with tab completion and set your 'path'
to .,,**
for a basic less fuzzy finder.
For more help see:
:h CTRL-]
:h tags
:h cscope
:h :find
:h 'path'
Plugins?
A fuzzy finder like CtrlP or fzf allows for general file navigation. For more specific project navigation you can use something like Projectionist.vim.
Conclusion
I would suggest slowly learning more buffer and general navigation commands. These commands will serve you well and help you navigation quicker without resorting to buffer cycling.
Personally, I use a combination of :b
, tags, cscope/GNU Global, and projectionist.vim for most of my navigation needs. I often have over 50+ buffers open and get to my desired file without ever resorting to buffer cycling.
:bd
. – Ralf Jan 22 '19 at 13:53:args
and:next
and:prev
– D. Ben Knoble♦ Jan 22 '19 at 14:17:bd
delete the whole buffer? – CIsForCookies Jan 22 '19 at 14:31