Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make gvim main window not quit when I type `:q` or `:qa`?

Tags:

vim

Instead I want it to close the files and open the default No Name buffer. I sometimes accidentally close the last file and I have to restart gvim and cd to the proper directory.

like image 920
balki Avatar asked Mar 02 '12 09:03

balki


1 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
like image 94
histrio Avatar answered Sep 20 '22 19:09

histrio