0

when i open multi windows like bellow:

vim foo.txt bar.txt

then , if i'm closing foo.txt by :bw
then ,i'm closing bar.txt by :bw

  • vim still open ! , i must close by :q
  • i don't want vim open if last buffer killed
  • or i want to make one keybinding for kill empty/non-empty window too i want to make mapkey to quil windows one by one like that:
function! Quitwindows()
    if bufexists(0) && bufname(0) == ''
      exe ':qall<cr>' 
    endif
endfunction
nnoremap <Space>q :call Quitwindows()
2
  • 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 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 at 23:10
-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
      exit 
    else
      bw
    endif
endfunction
nnoremap <silent> <Space>q  :call Quitwindows()<cr>
2
  • 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 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 at 12:22

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.