Skip to content

Instantly share code, notes, and snippets.

@zhester
Created September 10, 2013 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhester/6514562 to your computer and use it in GitHub Desktop.
Save zhester/6514562 to your computer and use it in GitHub Desktop.
Instructions for using jEdit as an external diff tool for Git.

Using jEdit as a Diff Tool for Git

Rationale

jEdit is my day-to-day editor. It works on every OS I use (primarily Windows at work and FreeBSD at home). With plugins, it provides every feature I need. It's open source [see note], easily extended, and it just works.

My employer has started using Git as our centralized version control system. Here, we have a fairly sophisticated tool for diff-ing and merging. However, the tool is neither open source nor free.

In order to reduce the time it takes me to learn Git, I'm trying to move more of my personal projects to Git. For my personal projects, I don't need to merge anything. I just want to see visual diffs between certain revisions of files. Therefore, I wanted to see if I could use jEdit as an external diff tool for Git, and avoid installing a separate diff program on each computer I use for personal work.

It wasn't as simple as I had hoped, but now I have a fairly resilient tool. Here's how I had to set everything up.

note: While I love everything about open source, I'm more than willing to pay for good tools. If Sublime Text ever manages to release a FreeBSD version, I'll be $70 poorer, and spending time turning it into my external diff tool for Git.

  1. Environment

I edit in "split" mode constantly. Once I switched to having two different files opened--and displayed--at once (when I switched all my computers over to widescreen monitors), I haven't gone back. This method takes advantage of that fact. That being said, I did try to make this work for people who use jEdit in the more normal single-pane mode.

This setup really only requires an up-to-date version of jEdit and the JDiff plugin. As of writing this, I'm using jEdit 5.1.0 and JDiff 3.3.0.

I'm also assuming that jEdit's executable is in your path. If that isn't so, you'll need to specify the complete path where necessary.

So far, this has only been tested on Windows 7 running Git 1.7.9 under Cygwin. Using a more Windows-friendly version of Git may result in some changes to your .gitconfig.

  1. Setting up jEdit

  1. Get this beanshell script:

    https://gist.github.com/zhester/6513880

  2. Copy it to a familiar path on your local system. I use the normal macro folder:

Windows:

$APPDATA/jEdit/macros/

Everyone else:

~/.jEdit/macros/
  1. Setting up Git

The necessary entries in the ~/.gitconfig file (for Cygwin Git) are as such:

[diff]
    tool = jedit
[difftool]
    prompt = false
[difftool "jedit"]
    cmd = jedit -run=\"$APPDATA/jEdit/macros/diff.bsh\" "$(cygpath -w $LOCAL)" "$REMOTE"

Change the difftool entry for normal Git installs to this:

[difftool "jedit"]
    cmd = jedit -run=\"~/.jEdit/macros/diff.bsh\" "$LOCAL" "$REMOTE"
  1. Diff-ing

You can test to make sure jEdit is doing its job just by diff-ing from the command line like this:

jedit -run="$APPDATA/jEdit/macros/diff.bsh" left_file right_file

jedit -run="~/.jEdit/macros/diff.bsh" left_file right_file

That's a lot to type, so you may want to alias it in your shell, if you plan on using this as your go-to diff tool.

In Git, however, you just have to change how you normally diff things:

git difftool <revision> <file>

Note: "difftool" not just "diff".

To compare a file to its previous version in the master branch:

git difftool master~1:path/file.ext path/file.ext

This will either open jEdit (if it wasn't already), or just open the files in your current editor. If you only use a single editing pane, it will split your pane into two, side-by-side panes. (Hint: Use CTRL+0 to quickly unsplit when you're done diff-ing.) The script will then open the reference version of the file (usually an older version) on the left side. It will open the working version of the file on the right side. After that, it will enable JDiff to display the differences visually.

Hint: I bound CTRL+SHIFT+D to toggle JDiff on and off. I use it for normal editing workflows, and it also works well to end your diff session that you started with this script.

Summary

Hopefully, I can use this as a reminder to how I got all this working in the first place. If you find it useful, too, that's great!

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