chrisp

Emacs Start Scripts

I tend to run Emacs in a wide variety of environments - sometimes I have X11 available, sometimes not. If I do, I usually work with several terminal windows simultaneously, and I don't want Emacs to get in the way of that, nor do I want more than one instance running at once. Emacs has the facilities to accomodate all these requirements, but it requires you to run it The Right Way(tm) depending on the situation. That's why I came up with these three shell scripts to make my life easier.

How It Works

All three scripts examine the environment to see if X11 is available (i.e. DISPLAY is set). If it isn't, they just run Emacs in the terminal and wait for it to terminate. If X11 is available, the scripts check if there is an emacsserver running. If there is, they'll run emacsclient to load the files in the already running Emacs. If not, they'll start up a new one in the background and enable emacsserver if applicable.

The three scripts have slightly different semantics and uses:

  • e is intended for interactive work. Under X11, it returns immediately, i.e. it starts Emacs in the background if necessary. It also enables the emacsserver if it starts up a new instance of Emacs.
  • e. is the same as e, but it passes . as the first parameter, causing Emacs to display the current working directory of your shell in Dired mode. Of course, additional parameters are passed along as well.
  • ew stands for "Emacs, wait" and is designed to be used as the value of the EDITOR environment variable. It has the same basic properties as the e script, but always waits until editing is finished. When emacsclient gets used, you must "close" the buffer using C-x #. If ew starts up a new Emacs instance under X11, it doesn't enable emacsserver because has to wait for Emacs to terminate.

Installation

Just put the scripts where your shell will find them and make sure they are executable. ~/bin is a good place usually.

Configuration

By default, the scripts run emacs or emacsclient. If you want them to start other commands, you can put a file named .emacsconfig in your home directory and set the shell variables EMACS and EMACSCLIENT in it (/bin/sh syntax). For example, if you're running Debian with both Emacs 20 and Emacs 21 installed, but want to run Emacs 20, you can use this:

EMACS=emacs20
EMACSCLIENT=emacsclient.emacs20

Compatibility

The scripts themselves were tested on Linux and Mac OS X using the respective /bin/sh shells - bash and zsh. It is assumed they will run on any Unix-type system.

The functionality was tested with GNU Emacs 20 and GNU Emacs 21. I haven't tested XEmacs, but expect it to work. I tried using the scripts with the Mac OS X native (i.e. Aqua) port of GNU Emacs 21 and it didn't work - I have no idea why, but then again, I wasn't surprised.

Dec 9, 2002 Copyright © 2002-2011 Christoph Pfisterer