Vim autocommand on BufDelete prevents opening help window

Posted by Kyle Strand on Super User See other posts from Super User or by Kyle Strand
Published on 2013-11-08T17:27:17Z Indexed on 2013/11/08 22:00 UTC
Read the original article Hit count: 169

Filed under:

I have the autocommand described here in my .vimrc: http://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()

© Super User or respective owner

Related posts about vim