An Emacs Diff (Ediff) tutorial

by

Since Jitesh is currently writing an extremely interesting series of posts on diff and patch (1,2), I decided to pitch in with my contribution. Ediff is one of those things which make Emacs so much more than just an editor. It is an extremely convenient way of viewing differences in files / buffers / directories.. You get the point.

Following the teaching tradition I prefer, lets get our hands down and dirty, and figure out the theory along the way:

Step 1:
Write two test files for our purposes.

Step 2:
Fire up ediff mode for our two files.

M-x ediff-files

The mini-buffer will ask you to enter the two files to compare (A and B). The order in which we enter the files does not matter, but tradition dictates that file A should be the original and file B should be the modified file.

Can you see the small blue window at the top? That is the ediff floating window. All ediff commands are processed (for lack of a better word) by this window. This window can be expanded to offer the quick help for ediff, which I believe is self-explanatory.

To point out the simple stuff, ‘v/V’ will scroll the two buffers (simultaneously) up and down, so you can read the orig and changed code. ‘n’ will take you directly to the next diff point, and the highlighting is super-cool, as you can see. So on and so forth, go read the quick help.

Step 3:
If you want to compare to directories of code, you use the command

M-x ediff-directories

In this case, all files / directories inside your directories are bunched into sessions.A powerful (and easy-to-understand) UI is provided for manipulating sessions. In this way, you can go through the differences file by file, disposing the session once you are done with it.

Points to note:

1) You can apply differences from one buffer to another region by region, i.e selectively apply a patch

2) You can merge two files into a third buffer.

3) Ediff understands version control systems, so you can compare two versions of the same file

4) Ediff indicates exact word differences. If the only differences in two regions are whitespace or line breaks, ediff will tell you so.

5) All of the above can be carried out on three files / buffers / directories! I’m not even going here till I get a complete hang of this!

So, that’s it folks. This is without doubt the easiest way of browsing code diffs that I know. Hope you find ediff as useful as I found it!

Note: This post is based on a one day study of Ediff, something I did because I was interested in the feature for a long time. This means, as always, I have barely scratched the surface. I’ll post more stuff if I come across something super-cool

Further InformationThe Ediff Manual

Tags: , ,

8 Responses to “An Emacs Diff (Ediff) tutorial”

  1. Aditya Says:

    Here is a thought… You keep writing tuts/info posts for emacs. I will write the corresponding one for vim on my blog. We can then have a combined site for vim/emacs bhai bhai πŸ˜€

    Where is my blog in the blogroll by the way πŸ˜€

  2. Vedang Says:

    very nice idea πŸ™‚
    your blog is not on the blogroll because when I initially began reading your blog, I had no idea it was a tech blog. I will now add it to the blog roll. Why don’t you at least make a separate tab on your page for tech posts?

  3. @$%deja vu$% Says:

    awesome feature ahe he !!!
    I just tried it out.. nice way to review stuff … great!!
    The good thing is that it works in emac’s terminal mode too. So, that if I am remotely working on my PC, I can still do this!

    btw.. is there a vi equivalent?

  4. Vedang Says:

    yes..
    it’s a mode called vimdiff, only i don’t know anything about it coz.. well.. i don’t know vi

  5. Aditya Says:

    He he… Yeah, people have been suggesting it to me and I am thinking of getting a new domain name for tech blog. Let’s see. This current host is cheap but too slow. Plus I am not able to change the theme now. 😐 Something very seriously wrong.

  6. gorro80 Says:

    awesome.. heh

  7. Rama Says:

    Good stuff

  8. Dav P Says:

    You can also use gnuclient to diff (or merge or anything) files automagically from apps that allow you to configure them properly:
    gnuclient -eval ‘(progn (ediff-files “/file/1” “/file/2”))’
    It would probably be best to wrap it in a script that takes the filenames from the app (e.g. git or something like cscope or gnu global) and put them in the proper place.
    Git requires a little modification to the .gitconfig file.
    I use something similar for merging:
    gnuclient -eval “(ediff-merge-files-with-ancestor \”$REMOTE\” \”$LOCAL\” \”$BASE\”)”

    I’m a heretic, since I switched to XEmacs and it uses gnuclient. Not sure what FSF Emacs uses (emacsclient still?), but it probably has the equivalent of `eval’

    Enjoy

Leave a reply to gorro80 Cancel reply