Skip to content

Instantly share code, notes, and snippets.

@dergachev
Last active March 10, 2021 11:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dergachev/d2c5eafe0c02938765dc to your computer and use it in GitHub Desktop.
Save dergachev/d2c5eafe0c02938765dc to your computer and use it in GitHub Desktop.
VIM snippet to show diff of unsaved changes.

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.

Source: http://stackoverflow.com/a/22360650/9621

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment