Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I deltect if there is a file modification in VIM upon exiting (:q)? If there is a file modification Vim echos

E37: No write since last change (add ! to override) 

I want to override this message with a custom message and an action similar to

File modified: Do you wish to save the file? (y)Yes (n)No (c)Cancel

I have modified Vim's behavior to save file on Ctrl-s and quit on Ctrl-w

Now I am looking for a solution if the file is already modified and user is quitting it with Ctrl-w I want Vim to prompt to save.

Here is the content of my .vimrc file:

map <C-S> :w<CR>
imap <C-S> <Esc>:w<CR>i

map <C-w> :q<CR>
imap <C-w> <Esc>:q<CR>
share|improve this question
1  
What about using :confirm quit? –  daniel kullmann Aug 2 '12 at 7:46
    
@danielkullmann Works!! never knew about that. However, how can I map that to Ctrl-w to confirm quit when there is a modification in file? –  Ashwin kumar Aug 2 '12 at 7:49
add comment

2 Answers

up vote 5 down vote accepted

You could use :confirm quit, e.g.

map <C-w> :confirm quit<CR>

By the way: C-w is a bad choice for a shortcut, because it is used as the start of other shortcuts, e.g. C-w v for splitting vertically. That's why you experience a short delay before the dialog pops open: after you press C-w, vim waits a short time for other keypresses, before it decides that you really just wanted to press C-w.

share|improve this answer
    
Awesome. and thanks for the tip. will avoid using Ctrl-w.. How about Ctrl-q?? Any thoughts? –  Ashwin kumar Aug 2 '12 at 8:00
1  
See :help c_CTRL-Q. Looks fine, if you're using the GUI version of vim, which you seem to do (Ctrl-S is also used for terminal control flow). –  daniel kullmann Aug 2 '12 at 11:15
add comment

You've already got a specific answer. Here's a generic one:

You can find examples (and complete working packages) of pretty nearly every kind of vim enhancement you can think of at http://www.vim.org/scripts/index.php

share|improve this answer
    
Thanks for the link. Will surely check it out.. :-) –  Ashwin kumar Aug 2 '12 at 8:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.