我在我的这里有描述的自动命令.vimrc
:https://superuser.com/a/669463/199803
编辑:为方便起见,复制到问题正文中:
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()
但奇怪的是,如果我只有一个列出的缓冲区,并且我尝试使用该:help
命令,帮助窗口似乎无法打开(或者可能打开后立即关闭)。如果我在 中注释掉该自动命令行.vimrc
,则此行为不再发生。
为什么会发生这种情况?我该如何解决?为什么会:h
触发该BufDelete
事件?
编辑:将详细程度设置为 12 级,我可以看到 BufDelete 事件确实发生了。以下是输出中与我相关的部分:
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()
答案1
尝试改变
if buflisted(nr) && ! empty(bufname(nr))
到
if buflisted(nr) && ! empty(bufname(nr)) || getbufvar(nr, '&buftype') ==# 'help'
这样帮助缓冲区就不会触发退出逻辑。