2009-02-11

Emacs23 has gone in pre-test. As I mentioned before, there are a lot of improvements, and there's little reason not to migrate to this new version. YMMV, of course. As always, you can see all the news with C-h n (Ctrl-H and then n) -- but obviously you need emacs 23 to see the news for emacs 23.

A more useful new feature that I'll discuss here, is emacs --daemon. Since many versions, you can set up emacs as an emacs-server. The idea is that you start one emacs instance (the 'server'), and you can then use emacsclient to quickly pop up a new emacs-frame (window). This new frame is not a new emacs instance, it's just a new frame (window) for the already-running emacs. The nice thing is that emacsclient is very quick - it doesn't need to parse all the startup packages or your 5000-line .emacs. You could activate it by putting (server-start) in your .emacs, or calling M-x server-start. I've been happily using that for years with mutt.

Those were happy years. Still, things could be better. Emacs-client/server had its limitations: first, an emacs could have graphical or console (tty) clients, but not both at the same time. Second, you always needed to start emacs in the foreground before you could use emacslient.

Both of these limitations have gone now -- hurray! You simply start emacs as:

$ emacs --daemon

and it starts running in the background. After that, you can start emacs client frames (windows) using:

$ emacsclient -c

. This will give you a graphical version when running X, or a terminal (console) version otherwise. If you want a terminal version even in windows-mode, use:

$ emacsclient -t

. If you don't want the controlling terminal to wait for the emacsclient to finish, you can add the -n argument, so:

$ emacsclient -c -n

and you can even specify an alternate editor if emacs is not yet running, so:

$ emacsclient -c -a nano # if all else fails...

Of course, you can also start emacs itself as the 'alternate'.

Now, my new way to run emacs is a follows:

I start emacs in the background (as daemon) when logging into X; this can be done automatically by adding to make emacs --daemon to your list of startup-programs. In GNOME, this can be set through the Preferences/Sessions/Startup Programs-menu. I am sure other environments have something similar. Alternatively, you could put emacs --daemon & in ~/.xinitrc.

I fire up emacs frames (windows) with emacsclient -c (or emacsclient -t to run it in a console);

That's it!

If you want to terminate emacs outside emacs, you'll have to kill it by hand (AFAIK) -- something like

$ pkill -TERM -u $USER emacs

Make sure you have no unsaved data, and no other emacsen running...

You also might want to set some influential environment variables in your .~/.bashrc, ~/.zshenv, ..., so other programs will automatically use an emacsclient when available; e.g.,

EDITOR="emacsclient -c"
VISUAL="emacsclient -c"

With emacs --daemon there's no longer any need to use vi for quick-editing some file. In other words: alias vi="emacsclient -c"...

That sounds like a nice addition although I don't think it will help with my (perhaps unusual) requirement to have multiple emacs instances with a different environment. I propose a different solution: launching a cutdown emacshere that allows me to avoid using vi.

I've used variations of emacsclient - gnuserv, gnuclient, gnudoit - for years. I especially like gnudoit because you can pass a lisp expression on the command line for Emacs to evaluate. I also have a modified version of gnuclient that does not wait for you to close the buffer. Is there a -daemon version that supports gnudoit and/or gnuclient like operations ?

I have a problem. After I start the emacs daemon using the --daemon option, when I start emacsclient with -c option, an emacs frame just flickers and goes away in a fraction of a second and emacsclient exits. The only output is "Waiting for Emacs..." Does anyone have any idea how to solve this? the -t option works without a problem!

@Jérémie: had the same problem under ubuntu 9.10 with the packaged emacs23 it all started working perfectly once i left out the "-c" option. this is fine for me since i have no need to differentiate between terminal and gui use.

I used update-alternatives to create an entry for emacsclient as an alternative for editor. I then set emacsclient as the default editor and set EDITOR="editor" in my .bashrc in order to get git on board with the change.I also have emacsclient -c -a '' bound to the global shortcut Super-C. Since emacs makes a better terminal and screen than all the other "consoles" and terminals.