When I open multi windows like the below:

vim foo.txt bar.txt

Then, if close foo.txt by :bw
then, close bar.txt by :bw

  • vim is still open! , it should close by :q
  • I don't want vim steel opened if the last buffer that already killed
  • or do I want to make one keybinding to kill empty/non-empty windows too I want to make a map key to quit windows one by one like that:
function! Quitwindows()
    if bufexists(0) && bufname(0) == ''
      exe ':qall<cr>' 
nnoremap <Space>q :call Quitwindows()
  • 2
    “Closing buffers” isn’t a traditional vim thing. Not that you’re doing anything wrong, just that most people tend to ignore any kind of buffer management. They all go away when you exit. I use windows or tabs to look at multiple files (or multiple parts of a file) simultaneously, and close those when done, but in general “not looking at a buffer” is the closest I come to “closing” one unless I need to open the file in a different vim instance (rare)
    – D. Ben Knoble
    Jul 30, 2021 at 0:22
  • I second the previous comment. BTW, for added perspective, I've been a heavy and pretty advanced user of Vim for many years and I have never ... like, for real, not one single time ... used :bw. :)
    – B Layer
    Jul 30, 2021 at 23:10

1 Answer 1


you can use this function

function! Quitwindows()
    let s:total_nr_buffers = len(filter(range(1, bufnr('$')), 'buflisted(v:val)'))
    if  s:total_nr_buffers == 1
nnoremap <silent> <Space>q  :call Quitwindows()<cr>
  • A good answer would have some explanatory text to go with the code and wouldn't have sloppy code with unreachable statements and unnecessary/unhelpful comments. FYI.
    – B Layer
    Jul 30, 2021 at 23:25
  • it's right , bur sometime any one can edit answer or question for better syntax and correct langue, thx @B_Layer
    – nextloop
    Jul 31, 2021 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.