Quickly ediff files from dired

ediff.el --- a comprehensive visual interface to diff & patch

I wrote about ediff years ago. Today, I'll just reference a useful ediff snippet from my config that I've added some time ago and refined only recently.

The premise is quite simple: press e in dired-mode to immediately ediff two marked files, no questions asked:

( define-key dired-mode-map "e" 'ora-ediff-files )

And here's the code, with a few bells and whistles:

;; -*- lexical-binding: t -*- ( defun ora-ediff-files () ( interactive ) ( let (( files ( dired-get-marked-files )) ( wnd ( current-window-configuration ))) ( if ( <= ( length files ) 2 ) ( let (( file1 ( car files )) ( file2 ( if ( cdr files ) ( cadr files ) ( read-file-name "file: " ( dired-dwim-target-directory ))))) ( if ( file-newer-than-file-p file1 file2 ) ( ediff-files file2 file1 ) ( ediff-files file1 file2 )) ( add-hook 'ediff-after-quit-hook-internal ( lambda () ( setq ediff-after-quit-hook-internal nil ) ( set-window-configuration wnd )))) ( error "no more than 2 files should be marked" ))))

Some notes on how the extra code adds convenience:

In case no files are marked, the file at point is used as the first file, and read-file-name is used for the second file. Since I have the magic (setq dired-dwim-target t) in my config, in case a second dired buffer is open, dired-dwim-target-directory will offer it as the starting directory during completion. Very useful to compare two files in two different directories. Depending on the order of the arguments to ediff-files , the changes will appear either as added or removed; file-newer-than-file-p tries to put the arguments in a logical order by looking at the files' last change times. ediff-after-quit-hook-internal is used to restore the previous window configuration after I quit ediff with q .

That's about it. Hopefully, it's useful. Happy hacking.