1

Is there any script that closes vim if there are only utility buffers open? I have seen some scrips for NERDTree (Close vim NERDtree on close of file), but none that is more general.

An example, because I am unsure if it is clear what I mean.

  1. Tagbar, Magit and a file are open
  2. I close the file
  3. only utility buffers are open (both buffers are hidden from :ls) -> vim closes

Is there an easy way to do this?

Ondolin
  • 324
  • 2
  • 13
  • In the absence of a common definition of "utility buffer"/"utility window", you are left with dumb plugin-specific pattern matching, as in the linked thread, and the obligation to update your solution whenever you add a plugin. – romainl Oct 31 '21 at 15:47

1 Answers1

1

You can use the following script to do what you want:

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

We use autocmd BufEnter to check if we have only one window left and the filetype of the current buffer is in the blacklist.

Update the list quit_filetypes to include the filetypes you have in mind.

jdhao
  • 24,001
  • 18
  • 134
  • 273