Sometimes it happen to me to start a vim session and forget it somewhere, behind a lot of other terminals. Some hours later, when it’s time to go home and I want to close all programs, I find this vim session again and I want to close it, but I don’t remember if I wrote something since last time I saved it and I don’t want to just save it without knowing what I’m saving or if it’s not necessary. So I first try to quit:
And since I usually wrote something, I can not just quit without saving (:wq) or forcing (:q!) and I get following error message:
E37: No write since last change (add ! to override)
To solve my problem, I just need to know what I wrote since last time I saved the document. This can easily be done with following command:
:w ! diff % -
Explanation: vim writes the document (:w) to the standard output and pipes (!) it to the diff command, which needs two arguments (actually two files to be compared). The first argument (%) is the original file name and the second one (-) means “standard input” (i.e. kind of “what I’m getting from the pipe”).
I also found other better solutions to do this, which for instance have a better vim-integration, colours, etc., but they are all more complicated and not so easy to remember. So… this is my quick first choice.