jfocht
jfocht

Reputation: 1754

Force GVim to prompt before close

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.

Upvotes: 7

Views: 1503

Answers (4)

101010
101010

Reputation: 15726

Gvim and vim handle quit a little differently. Option 1 works as you would expect in both. Option 2 works nice in vim, but in gvim you can get around it (in part) by pressing the "X" to close in X Windows. It will still confirm if the file is unedited, but it will just quit everything when everything is saved. Maybe someone else knows how to deal with that issue.

Option 1: Confirm quit only when a file is unsaved

Add this in your .vimrc:

set confirm

Option 2: Confirm quit all the time (when you do ZZ or q)

Use this plugin

 Plugin 'vim-scripts/confirm-quit'

Or hand code it, following along with this to see what you need to do (BTW -- this plugin was born from this SE Question)

Upvotes: 0

Shivanshu Goyal
Shivanshu Goyal

Reputation: 1452

Add this in your .vimrc:

set confirm

Upvotes: 2

Conner
Conner

Reputation: 31060

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

Upvotes: 7

sybkar
sybkar

Reputation: 386

Putting the following in your vimrc could be used to completely disable the ZZ shortcut altogether:

nnoremap ZZ   <Nop>

Or you could remap them to the standard behaviour of :q :

if has("gui_running")
    nnoremap ZZ :q
    nnoremap :wq :q
endif

Upvotes: 6

Related Questions