Subversion diff with vim

<1 min read

Once in a while I have to use Subversion on a Linux box. Not a big deal, except for diffs. The standard svn diff output is not the most readable. Vim, or more exactly vimdiff, does a terrific job at displaying differences between files side-by-side. Here's a little Bourne shell script I wrote that uses vimdiff to view the differences between a local file and the latest revision in the repository:

Nothing too fancy. It uses svn cat to get the latest rev, saves it to a temporary file, and opens both files in vimdiff:

The temp file is deleted as soon as vimdiff is quit.

[@389]
I also just found svncommand.vim, a nifty Subversion integration plugin.

Comments

12 (Closed)

christopher baus

Feb 14, 2006

svn diff --diff-cmd vimdiff

will do it.

Erik C. Thauvin

Feb 14, 2006

The --diff-cmd option returns arguments that are not understood by vimdiff. Someone wrote a python hack to re-arrange the parameters, but I find my script much easier to use.

E.

Marius Scurtescu

Feb 15, 2006

There is a really nice Linux tool for visual diffs, it is called meld (meld.sourceforge.net).

Give it a try, it does understand Subversion and all you have to do is:
$ meld <your_file>

It can also show diffs for a whole folder structure, just use a folder name instead of a file name.

Fernando Brucher

Apr 3, 2006

Your script works better if the extension of the temporary file is the same as for the original file. That way syntax highlighting works also for the temporary file. I modified the temp file name generation in your script like so:

TEMP=/tmp/tmp.$$.$1

Thanks for creating you script. It works great. And I'm happy to be able to use vimdiff, which I'm familiar with.

Erik C. Thauvin

Apr 3, 2006

Thanks, Fernando. I've changed the script accordingly.

Francesc

May 11, 2006

Good tip!

I've found it has problems using directories (ie, 'svndiff lib/foo.php') but it works if you change temp file name with this:

TEMP=/tmp/tmp.$$.`basename $1`

Erik C. Thauvin

May 11, 2006

Thanks, Francesc. I'll update the script with your suggestion.

fREW Schmidt

Nov 25, 2006

Hi Erik,

I don't know if you actually do any editing when you use this script.

I personally don't so I found it useful to do the following:

vimdiff -R $TEMP $1

Only because I tend to leave files open in vim in between commits and I got sick of the error message and whatnot.

wulf

Mar 25, 2007

I added a option -r maybe it is also interesting for you or others.

You will find my version here
gpl.coulmann.de…

Balbir Singh

Apr 6, 2007

I would suggest a trap in the begining to make sure the temp file is removed no matter how the script exits.

e.g.

trap 'rm -f $tempfile' 0 1 2 15

Thanks for the idea.

Carlo

Jan 16, 2011

I've made a script with the same purpose. It's intended as an argument to the --diff-cmd option, which is usually achieved via a ~/.subversion/config entry. The advantage is that you have all the options of "svn diff" available, such as --summarize.

It also gives friendly names to the Vim buffers -- Subversion passes these as additional parameters.

Furthermore, it ensures Vim syntax highlighting does not break, in the vein of Francesc's suggestion.

Erik C. Thauvin

Jan 18, 2011

Nice script, Carlo. Too bad I don't have gVim to use on my servers.

BTW, I just checked in a new version of the script incorporating all of the suggestions posted here.

Thanks,

E.