Let's say you're editing a file in VIM, and want to see a diff between your current unsaved version and what's on the hard disk. Turns out you can just type this:
:w !git diff --no-index % -
What this does it pipes the whole contents of the current file to the command git diff --no-index % -
The % symbol is substituted by vim to be the path to the saved file.
The - tells git diff
to read from STDIN.
To make this easier to run, add this to your .vimrc
command Diff execute 'w !git diff --no-index % -'
Now you can just type :Diff
and see the same results.
I find this trick indispensible when your vim crashed and you're trying to recover your work from a .swp file (by running vim -r .somefile.swp
, and have no idea whether it's better or worse than what's already saved.