Improving svn diff with the Power of Vim

11, Apr 2012

Contents

One of vim's nice features is a powerful diff tool that can be used to easily tell the differences between multiple different files. This can be called up at any time by issuing the following:

vimdiff file1.xxx file2.xxx

Subversion's default diff tool - while effective - lacks a lot of options. Firstly, it simply outputs the results of the diff to standard out. This is limiting for several reasons: you can't edit the files you're diffing, so quickly changing code is impossible. There's also no color or syntax highlighting, so telling at a glance what's changed is more difficult. Vimdiff solves all these problems, as it drops you into a full instance of vim allowing you to do anything.

Why use svn diff when we can seamlessly integrate vimdiff into the workflow in two simple steps? Let's get started!

Step 1: The Script

The first thing that needs to be done is to create a simple bash script that will be the wrapper for svn diff. Create the following file on your system named diffwrap.sh:

#!/bin/sh

# Configure your favorite diff program here.
DIFF="/usr/bin/vimdiff"

# Subversion provides the paths we need as the sixth and seventh
# parameters.
LEFT=${6}
RIGHT=${7}

# Call the diff command
$DIFF $LEFT $RIGHT

You can either put it in a system wide folder (/usr/local/bin) or in your own folder. In any event, don't forget to

chmod a+x diffwrap.sh

Step 2: Make Subversion Use It

Now that the script exists, we have to tell Subversion to use it for diff. Luckily that's quite easy. Simply edit ~/.subversion/config and find the diff-cmd line inside the [helpers] section. Uncomment it and change it to something like this:

diff-cmd = /path/to/diffwrap.sh

Step 3: Profit!

You're all done! To see it in action, change directory into a Subversion project with some local changes, and simply diff like normal:

svn diff

Voila!

Comments

Raul said on May 30, 2013:

Hi Corey

This is a great script! I been wanting to do this for a while.

What about revisions:

-rXXXX

or

-rXXXX:YYYY

Benji Wiebe said on Jun 6, 2014:

Thanks! This was very helpful. I've just started using SVN a project or two ago, and I have been wishing I could use vimdiff instead of the default 'svn diff'.

Have Something to Say?

Questions? Comments? Concerns? Let me know what you’re thinking.

  • You can use Markdown formatting here.