I'm putting together a presentation that involves some live demos in the terminal. In one of the demos, I run a shell script that takes an input file, runs it through an Awk script, and generates a transformed output file. After doing so, I'd like to display the diff between the input and output files.

Ediff is perfect for this, since it colors the diff and I can navigate easily with n and p. However, I've only been using a gui Emacs. If I start ediff in terminal emacs, the coloring seems to be absent, and other things are just wonky, since I've been gui-centric in my configuration.

I'd like to have the shell script focus a running gui Emacs and run ediff on the two files. I'm on macOS, and I have a shell script that uses applescript to focus Emacs, but I don't know of a way it could tell Emacs to evaluate an expression.

My current workaround is to add a keybinding in my init.el that will launch the desired ediff session, and have my script simply launch/focus Emacs. Then it's up to me to hit the keybinding.

The demo shell script looks like:

#!/usr/bin/env bash



awk -f "$awk_script" "$input" > "$output"


The e script launches/focuses emacs. It can be seen here, but the gist of it is:

osascript -e "tell application \"$emacs_app\" to activate"

And in my init.el, I've added:

(defun ivan-demo-ediff ()
  (ediff-files "/path/to/input"

(global-set-key [f2] #'ivan-demo-ediff)

Is there a better way to do this?


I use this to diff from bash. --eval is what I think you are looking for. If you are not using emacsclient you can adjust the first portion accordingly.

ediff () 
    if [ -d $1 ]; then
        emacsclient -c -a emacs -q --eval "(ediff-directories \"$1\" \"$2\" \"\")";
        emacsclient -c -a emacs -q --eval "(ediff-files \"$1\" \"$2\")";

ediff3 () 
    if [ $# -ne 3 ]; then
        echo Usage: $0 local base other;
        exit 1;
    emacsclient -c -a emacs -q --eval '(ediff-merge-with-ancestor "'$1'" "'$2'" "'$3'")'
  • Does this open in a gui or in the terminal? I tried it and for me it opens in terminal, but that may be because of the emacs build I'm using. – ivan Aug 18 '17 at 11:40
  • Oh, I was mistaken. I must not have had a gui server running the first time I tried this. It works! – ivan Aug 18 '17 at 11:43
  • 1
    The one thing I don't know is how to restore emacs frame properly after quitting the ediff. Magit does a really good job of cleaning up afterwards. But magit is invoked from within emacs itself. – eflanigan00 Aug 18 '17 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.