2

I have explictly said in my vimrc that I want nohidden behaviour (I am jumping between files a lot, with long lasting session, and after some time buffers list is very polluted). However when I jump to other file and I run :ls I still see old files listed. Is there a way to prevent such behaviour?

3

The 'hidden' setting

The 'hidden'/'nohidden' setting tells Vim what to do about buffers that are "abandoned".

  • 'hidden' means an abandoned buffer becomes hidden (similar :hide).
  • 'nohidden' (the default) means the buffer is unloaded (:bunload).

Both :bunload and :hide do not remove a buffer from the buffer list. Therefore 'hidden' setting will not affect if a buffer shows up in the buffer list.

For more help see:

:h abandon
:h 'hidden'
:h hidden-buffer
:h :bunload
:h :ls

Removing hidden buffers from the buffer list

To remove a buffer from the buffer list by doing :bdelete (unload buffer and delete from buffer list) or :bwipeout (really really get rid of the buffer).

Buffers can "automatically" be removed when they become hidden by changing the 'bufhidden' setting on each buffer.

autocmd BufReadPost * set bufhidden=wipe

I would not recommend this method a it can ruin the following commands:

  • Switch to the alternative buffer, <c-^>/<c-6>
  • Popping back from a tag, <c-t>
  • Moving through the jump list across buffers with <c-i>/<c-o>
  • Not to mention it may interfere with some plugins which uses a temporary buffer.

Switching buffers

I imagine the root problem is how you navigate between buffers. I am guessing you use buffer numbers or cycle through via :bnext/:bprev. There are some alternatives to these methods.

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
  • Might want to use 'hidden' via set hidden
  • Split variant of :b is :sb.
  • Also accepts a buffer number

A common mapping:

nnoremap <leader>b :ls<cr>:b<space>

If you like plugins you may want to check out something like CtrlP to switch between buffers. For more advanced file navigation use tags, cscope, GNU global, or plugin like projectionist.vim.

For more help see:

:h :ls
:h :b
:h 'hidden'
:h wildcard
:h c_CTRL-D
:h starstar
:h tags
:h cscope

Conclusion

Personally I would change how you switch buffers. I typically have 50+ buffers open and switch between without any issue. You can remove the occasional buffer via :bd or :bw when the need arises.

  • Actually I am quite heavy user of projectionist and Skim (FZF alternative) but thanks for explanation. – Hauleth Jan 25 '17 at 20:05
  • And also I stopped cycling through buffers as I use files instead. I often cycle through tabs as I am heavy user of NeoVim built-in terminal with Dispatch :Start and :Console command – Hauleth Jan 25 '17 at 21:53

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.