Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force GVim to prompt before close

Tags:

vim

I've been making a transition to GVim lately, because I find it to be more aesthetically pleasing and a little bit faster then vim in the terminal. I have this really bad habit that I'm trying to break. When I used vim from the command line my work flow was like this:

vim filename.txt
# make some edits
ZZ
# do other stuff
vim otherfile.txt
# make some edits
ZZ

Now using GVim, I end up closing the editor far too frequently. I'm wondering if there is a way to force just GVim to either prompt me or open an empty buffer when I do a :wq or ZZ. Any ideas?

EDIT: I know how to remap keys, but I'm wondering if there is a way to force GVim to have a different behavior then when vim is called from the command line.

like image 293
jfocht Avatar asked Jul 26 '12 14:07

jfocht


1 Answers

Call a function on ZZ and if there is only one tab and window left, prompt whether to close or not (default is to close). See :help confirm().

nnoremap ZZ :call QuitPrompt()<cr>

fun! QuitPrompt()
   if has("gui_running") && tabpagenr("$") == 1 && winnr("$") == 1
      let choice = confirm("Close?", "&yes\n&no", 1)
      if choice == 1 | wq | endif
   else | wq | endif
endfun
like image 68
Conner Avatar answered Sep 30 '22 14:09

Conner