如果没有 "unhidden" 个缓冲区打开,则关闭 vim

Close vim if no "unhidden" buffers open

如果只有实用程序缓冲区打开,是否有任何脚本可以关闭 vim?我看过一些 NERDTree 的文章 (Close vim NERDtree on close of file),但 none 更笼统。

一个例子,因为我不确定我的意思是否清楚。

  1. Tagbar、Magit 和一个文件已打开
  2. 我关闭文件
  3. 只有实用程序缓冲区打开(两个缓冲区都对 :ls 隐藏)-> vim 关闭

有没有简单的方法来做到这一点?

您可以使用以下脚本来执行您想要的操作:

augroup auto_close_win
  autocmd!
  autocmd BufEnter * call s:quit_current_win()
augroup END

" Quit Nvim if we have only one window, and its filetype match our pattern.
function! s:quit_current_win() abort
  let quit_filetypes = ['qf', 'vista']
  let buftype = getbufvar(bufnr(), '&filetype')
  if winnr('$') == 1 && index(quit_filetypes, buftype) != -1
    quit
  endif
endfunction

我们使用 autocmd BufEnter 检查是否只剩下一个 window 并且当前缓冲区的文件类型在黑名单中。

更新列表 quit_filetypes 以包含您想要的文件类型。