1

I have the autocommand described here in my .vimrc: https://superuser.com/a/669463/199803

EDIT: Copied into body of question for convenience:

function CountListedBuffers() 
     let cnt = 0 
     for nr in range(1,bufnr("$")) 
         if buflisted(nr) && ! empty(bufname(nr))
             let cnt += 1 
         endif 
     endfor 
     return cnt 
endfunction

function QuitIfLastBuffer()
     if CountListedBuffers() == 1
         :q
     endif
 endfunction

autocmd BufDelete * :call QuitIfLastBuffer()

Bizarrely, though, it seems that if I have exactly one listed buffer, and I try to use the :help command, the help window fails to open (or perhaps opens and is immediately closed). If I comment out that autocommand line in my .vimrc, this behavior no longer occurs.

Why is this happening, and how can I fix it? Why would :h even trigger the BufDelete event?

EDIT: setting verbosity to level 12, I can see that the BufDelete event is indeed occurring. Here's the portion of the output that looks relevant to me:

Executing BufAdd Auto commands for "*"
autocommand call <SID>BMAdd()
calling function <SNR>6_BMAdd
calling function <SNR>6_BMAdd..<SNR>6_BMFilename
calling function <SNR>6_BMAdd..<SNR>6_BMFilename..<SNR>6_BMMunge
calling function <SNR>6_BMAdd..<SNR>6_BMFilename..<SNR>6_BMMunge..<SNR>6_BMTruncName
function <SNR>6_BMAdd..<SNR>6_BMFilename..<SNR>6_BMMunge..<SNR>6_BMTruncName returning '/usr/local/share/vim/vim74/doc'
continuing in function <SNR>6_BMAdd..<SNR>6_BMFilename..<SNR>6_BMMunge
function <SNR>6_BMAdd..<SNR>6_BMFilename..<SNR>6_BMMunge returning 'help\.txt\ (4)\^I/usr/local/share/vim/vim74/doc'
continuing in function <SNR>6_BMAdd..<SNR>6_BMFilename
calling function <SNR>6_BMAdd..<SNR>6_BMFilename..<SNR>6_BMHash
function <SNR>6_BMAdd..<SNR>6_BMFilename..<SNR>6_BMHash returning #340582286
continuing in function <SNR>6_BMAdd..<SNR>6_BMFilename
function <SNR>6_BMAdd..<SNR>6_BMFilename returning #0
continuing in function <SNR>6_BMAdd
function <SNR>6_BMAdd returning #0
continuing in BufAdd Auto commands for "*"
Executing BufDelete Auto commands for "*"
autocommand :call QuitIfLastBuffer()
3
  • Set your 'verbose' level to 12 and try to open a help window. Also, it might help if we knew what was in your QuitIfLastBuffer function.
    – Heptite
    Nov 8, 2013 at 18:58
  • @Heptite, the function in question is copied in full in the link I included. Nov 8, 2013 at 20:01
  • Put it in the body of your question.
    – romainl
    Nov 8, 2013 at 21:09

1 Answer 1

1

Try changing

if buflisted(nr) && ! empty(bufname(nr))

to

if buflisted(nr) && ! empty(bufname(nr)) || getbufvar(nr, '&buftype') ==# 'help'

so that help buffers do not trigger the logic to quit.

5
  • Vim then tells me that buftype is an "unknown function." Nov 8, 2013 at 21:43
  • Ah sorry, that is an option, not a function; see my edit. Nov 8, 2013 at 21:48
  • Awesome, that works. Thank you. The autocommand also appears to be preventing me from opening directories in Vim (i.e. vim /path/to/dir just flashes my shell screen)--think there's a similar fix for that? Nov 8, 2013 at 22:04
  • Those may have a buftype of nofile. Try ! empty(getbufvar(nr, '&buftype')); that should also cover the help buffer, so replace the conditional in my answer with it. Nov 8, 2013 at 22:09
  • 1
    Okay, with that change, :h still works, but now when I try to open vim to a directory I just see /path/to/dir: is a directory flash, then vim still quits. So...slight progress. Nov 8, 2013 at 22:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .