How to make the main gvim window not close when typing `: q` or`: qa`?

Instead, I want it to close files and open the No Namedefault buffer . I sometimes accidentally close the last file, and I need to restart gvim and cd in the appropriate directory.

+5
source share
3 answers

put it in your $ MYVIMRC

function! NumberOfWindows()
  let i = 1
  while winbufnr(i) != -1
  let i = i+1
  endwhile
  return i - 1
endfunction


function! DonotQuitLastWindow()
  if NumberOfWindows() != 1
    let v:errmsg = ""
    silent! quit
    if v:errmsg != ""
        "echohl ErrorMsg | echomsg v:errmsg | echohl NONE
        "echoerr v:errmsg
        echohl ErrorMsg | echo v:errmsg | echohl NONE
    endif
  else
     echohl Error | echo "Can't quit the last window..." | echohl None
  endif
endfunction

if has("gui_running")
    cnoreabbrev <expr> q getcmdtype() == ":" && getcmdline() == 'q' ? 'call DonotQuitLastWindow()' : 'q'
    cnoreabbrev <expr> qa getcmdtype() == ":" && getcmdline() == 'qa' ? 'call DonotQuitLastWindow()' : 'qa'
endif
+5
source

Why don't you use

:bd

to close the buffer (after saving the latest changes)?

+4
source

If you use the command instead :close, this command will not close the last window.

+3
source

All Articles