The Lumber Room

"Consign them to dust and damp by way of preserving them"

Using Emacsclient

with 10 comments

Emacsclient, essentially, says “use a running emacs”. With it, you can have only copy of Emacs running, and every time you need to edit something, you can call “emacsclient filename” to use the existing instance of Emacs without having to wait several seconds for a new one to start. To use it, simply put (server-start) in ~/.emacs, or type M-x server-start from within Emacs.

There are two (minor, irritating) problems with it, though:

  1. When you are done with a file, you have to close it by hitting C-x #. Closing it normally (hitting C-x k, or exiting Emacs) brings up an annoying
    Buffer `buffername' still has clients; kill it?

    message. The rationale for this is in server.el:

    ;; Ask before killing a server buffer.
    ;; It was suggested to release its client instead,
    ;; but I think that is dangerous--the client would proceed
    ;; using whatever is on disk in that file. -- rms.

    I don’t see what’s dangerous about that, but then again, I’m not RMS! …
    One could put something like (global-set-key (kbd "C-x C-k") 'server-edit) to replace C-x # by an easier keystroke, but I found a better solution to be to remove the thing altogether, assuming that you’re going to be careful against whatever it is that is dangerous:

    (remove-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function)

    Remember to put this after the (server-start) line in .emacs (If you’re doing M-x server-start manually, you’ll have to call the above remove-hook manually too.) After doing this, you can simply save and close the buffer as usual (or even quit Emacs), and everything will work fine — you won’t get the annoying message any more.

  2. The second problem is that when you call emacsclient from a shell, although the file gets loaded in Emacs, the Emacs window rarely pops up, at least on GNOME and KDE (and, on hearsay, OS X as well). It was suggested to put (add-hook 'server-switch-hook 'raise-frame) somewhere, but that doesn’t work either. (In KDE, it only flashes the Emacs window in the task bar, but that is better than nothing, as Ctrl-Alt-A is KDE’s shortcut for “Activate window demanding attention”.)
    At least it appears that people arewere discussing it of late, in a thread “raise frame no go”, and related discussions here, and also a GNOME bug here. These discussions are specific to Metacity (GNOME’s default WM), but it also doesn’t work on KDE and Cygwin.

Oh, and if you’re on OS X, you can turn emacsclient into a .app with Platypus. (Although you probably don’t really need it…)

Update: Emacsclient just got a lot more friendly. You no longer have to go find where the original emacs “server” was started — you can simply start up a new frame for your new buffer, wherever you want (all such Emacs “instances” share the same buffers). This makes the second problem above entirely irrelevant. (It seems to be an Emacs tradition to give up on fixing things that don’t work and implement something better instead :P) Read more about it.


Written by S

Sat, 2007-01-06 at 17:07:57

Posted in Uncategorized

Tagged with , , ,

10 Responses

Subscribe to comments with RSS.

  1. I find that “still has clients” message annoying too. Google led me to your remove-hook command, but isn’t working for me. Or rather, it doesn’t work if I put it in my .emacs. If I execute it manually once Emacs has started up (using C-x C-e), it works. Any ideas?


    Sat, 2007-02-24 at 04:17:46

    • I have had the same problem with Emacs23. Putting (server-start) etc… in my .emacs hasn’t helped, but running manually after launch has fixed it. Instead, I beat this problem with the following dirty trick:

      (defalias ‘server-kill-buffer-query-function ‘(lambda () t))


      Tue, 2013-05-28 at 05:40:37

  2. The most likely bug is that your (server-start) comes after the remove-hook thing. Try switching them around? (Edited the post to mention this.)


    Sat, 2007-02-24 at 09:05:23

  3. Hmm, I don’t have a server-start command in my .emacs. I’m using the EmacsW32 ( binaries, which must be taking care of the server stuff for me. I’ll look into your server-start-hook suggestion.


    Wed, 2007-02-28 at 06:13:12

  4. Hmm… maybe you can try explicitly putting a (server-start) in your .emacs. The description on that EmacsW32 page says “Emacsclient is patched so that it can start Emacs and emacs server automatically when needed.”, so maybe the invocation of server-start is happening only when you call emacsclient… and your .emacs would already have been read by then.


    Wed, 2007-02-28 at 09:16:50

  5. Hah! That did it! Thanks for your help.


    Fri, 2007-03-02 at 02:11:16

  6. Thanks, that worked.


    Sun, 2007-03-18 at 14:44:18

  7. That resolves not only the problem of killing just *one* buffer.
    When I had many files open and did a C-x C-b to kill several in a bulk, this annoying message was bugging me for each and everyone. I guess that was part of Windows-like behaviour.

    Big Thank You!


    Fri, 2007-03-23 at 18:53:55

  8. […] […]

  9. […] courtesy of The Lumber Room, in ~/.bashrc and others: export SVN_EDITOR='c:/emacs/bin/emacsclientw.exe -a […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: