0

I'm using :bp to re-open the previous file I worked on, and :bn to go back, but I'm stuck on a loop with 3 files, and I only want to loop over 2 of them. Is there a way to edit the buffer and pop the unwanted file?

sure I can use bp and then bn to use the same 2 files, but that wouldn't work in the general case

Not sure which tags are relevant, and how this feature (using Ctrl+P to find files and :bp and :bn to go back and forth) of vim is called

  • 3
    Change to the third file and remove it from the buffer list with :bd. – Ralf Jan 22 at 13:53
  • Or use :args and :next and :prev – D. Ben Knoble Jan 22 at 14:17
  • @Ralf wouldn't :bd delete the whole buffer? – CIsForCookies Jan 22 at 14:31
  • 1
    @CIsForCookies Yes, from Vims buffer list. After that command Vim only works with two files. – Ralf Jan 22 at 14:39
2

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.

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.