48

How can I use vimdiff to view the differences described in a diff file?

1

4 Answers 4

59

Instead of using /usr/bin/vimdiff command, try this:

$ vim file :vertical diffpatch path/to/diff

(:vert diffpa for short.)
This is equivalent to calling vimdiff on the original file and the subsequently patched file, but vim calls patch on a temporary file for you.

Edit

If you want vim's diff-mode to be entered automatically, use this:

$ vim file +'vert diffpa path/to/diff'

where +command asks vim to execute "command". (+123 jumps to line 123, +/abc jumps to the first match for "abc", it's all documented.)

Regarding Ken's query: if the diff file includes hunks applying to files other than the file you're currently editing, no worries; vim calls the patch executable underneath, which will ask for the locations of these mysteriously missing files, and you can tell patch to just skip those hunks.

Sign up to request clarification or add additional context in comments.

3 Comments

nice! But you should maybe warn about the risk of side-effects if the file contains diffs for other files?
That works great for a single file. Thanks! I didn't know about diffpatch.
Is there a way to do this without having to save the diff file to disk? Something like: :vert diffpatch - !git diff file? (I know there are better solutions for git diff specifically, but I'm curious if this is possible in general)
4

Coming from the other direction. I wrote a Vim plugin that shows the changes that have been made to a file since the last save in either vimdiff or unified diff format.

Get it here: diffchanges.vim

1 Comment

I think it's better to save early, save often and use Gundo: sjl.bitbucket.org/gundo.vim instead.
2

Make a copy of the original file, apply the diff and then

vimdiff original_file patched_file 

You could also look at vim.org scripts which have been written to deal with svn diff output. If you are generating your diff from a version control system then take a look at the vcscommand.vim : CVS/SVN/SVK/git integration plugin.

Comments

1

Would you say more about why you want to use vimdiff for this?

For instance, do you want to visualize per-character differences in a (unified) diff file?. If so, someone answered my question about same with a suggestion to basically just fire up emacs on the diff and hit ALT-n to cycle through hunks, nicely highlighting per-word or per-characters changes along the way (similar to vimdiff). It works well, but it's a bummer to fire up emacs just to do this. I only know enough emacs to do that then exit without mucking up the patch. :)

Anyway, the emacs answer gave me some ideas. I'm sure Vim can be cajoled into providing better highlighting for diff files, maybe something more like ediff. I looked into this a while back, and someone on the vim user list had the excellent suggestion to incorporate google-diff-match-patch. The Python interface could be used from a Python Vim ftplugin, for instance.

Sorry about using an "answer" to reply when this may not be a great answer, the formatting for "add comment" was too limited for what I wanted to say.

For posterity, here's a reference to another similar question (which already links here as well).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.