Michele Marcionelli's Blog

… about me, my life and what I like to do

×

View differences since last write with vim

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:

​:q

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)

Vim's "no write since last change" error message

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 % -

Vim with diff's output

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.

Leave a Reply

Your email address will not be published. Required fields are marked *


9 − = two