Sunday, 5 November 2017

Vim: quit if buffer list is empty


I'd like :bd and :bw to actually quit my vim session if, after the current buffer is removed, the buffer list is empty (i.e. if, before executing :bd or :bw, I only had the current buffer in the buffer list). How can I accomplish this? I'd assume it should be pretty easy, but googling hasn't really yielded anything useful, and so far I haven't managed to write an autocmd to accomplish this.



Answer



To check for a single buffer, you need to iterate over all potential buffers and check whether they are still listed (e.g. in :ls). The check is triggered whenever a buffer is deleted:


:autocmd BufDelete * if len(filter(range(1, bufnr('$')), '! empty(bufname(v:val)) && buflisted(v:val)')) == 1 | quit | endif

No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...