I have this function and mapping:
fun! TwiceToQuit() sil! q if v:errmsg == 'E37: No write since last change (add ! to override)' echohl ErrorMsg echo 'No write since last change, really quit?' echohl None if !exists('w:countofq') let w:countofq = 0 endif let w:countofq += 1 if w:countofq == 2 q! endif endif endfun noremap <F2> <Esc>:call TwiceToQuit()<CR>
So when I press F2 at a modified file it asks me if I really want to quit, if so I press F2 again.
It works, but I have a few problems:
When I have 2 windows, one with some regular file and the second one with
helpand I press F2 when the first window is active, it closes the whole vim. I have found some autocommand event related to this -
QuitPre, but there aren't any autocommands associated with it so I don't know why is this happening.
If I accidentally hold F2, it just spams the command so everything gets closed. I don't want that.
I want to reset the variable
w:countofqif I press some other key than F2. Is that possible?