1

I tend to flick between files open in different Vim buffers.

When quickly switching between files (e.g. from the quickfix list, Ack results, tags) files often get opened and then immediately hidden. However the buffer remains open, poluting my open buffer list.

How can I set the buffers to close instead of hiding if they have not been modified in any way.

1

There's not really a great solution for that, as most of them have shortcomings.

You could try to :set bufhidden=delete (see 'bufhidden' for more details.) This looks pretty close to what you want.

But 'bufhidden' is a setting that is local to buffer, so you would have to set it for every new buffer, or for the buffers you want to be ephemeral as you describe. You could potentially do that from an autocmd or perhaps a few of them, depending on your criteria for which buffers should be deleted when hidden.

Also note that Vim documentation strongly recommends against using that option!

CAREFUL: when unload, delete or wipe is used changes in a buffer are lost without a warning. Also, these values may break autocommands that switch between buffers temporarily.

So that's probably a non-starter...

There's also :set nobuflisted (see 'buflisted', in which case the buffer exists, only isn't listed in :ls output.

It is also a setting local to a buffer, so you'd need an autocmd to set it on new buffers.

This one doesn't seem to be as dangerous, and seems to almost do what you asked for. But this one will unlist even the current buffer, or buffers for the open windows, so this essentially makes sure :ls will be always empty, which makes it useless, in which case are you really better off than leaving buffers around?

Finally, you can cleanup buffers after a large operation (say involving :args or quickfix), once you're done and saved everything, you can use:

:%bd

This will close all buffers and leave you with a new empty file to edit. (See documentation for :bdelete.)

This might be an useful command once you're switching to something new and you want to be able to keep only a small handful of buffers around, so you can quickly switch between them using short substrings of their filenames, or :bnext and friends.

  • 1
    Thanks. I have posted the autocmds I have started using since I posted the question. I haven't had too many issues with it so far (occasionally it hides a file I would rather see). It uses the nobuflisted option you mention. – Mark Perryman Jul 18 at 15:24
1

This is what I came up with, I am happy with it so far.

" Unlist unchanged buffers if they are hidden
function! BufferUnchanged()
  return exists("b:undo_seq_load") && b:undo_seq_load==changenr()
endfunction
function! UnlistUnchangedBuffer()
  if BufferUnchanged()
    set nobuflisted
  endif
endfunction
" Save the undo sequence position on buffer load
autocmd BufReadPost * let b:undo_seq_load=changenr()
autocmd BufWinLeave * call UnlistUnchangedBuffer()

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.