I use windows batch file to open files in an already-running instance of Emacs using emacsclientw.exe. However, any file opened that way is opened in server mode, which means that I have to use C-x # to kill it, instead of the usual C-x k. How do I change this behavior?

Was it helpful?



D:\> emacsclientw -n foo.txt

the -n says "no wait". This is in GNU Emacs 22.2.1 (i386-mingw-nt5.1.2600) of 2008-03-26 on RELEASE (and many prior versions, IIRC).


My solution was to rebind it (well M-w actually) to:

(lambda ()
  (if server-buffer-clients

[edit: having now read the code for server-edit, it might be better to use server-done (depending on what you want). server-edit will switch you to a server-edited buffer (if any still exist) but server-done will simply switch you to the next buffer. You could also use the output of server-done to see if the buffer was actually killed (not the case if the file was open before invoking emacsclient) and then kill it if not. Or use server-kill-buffer as suggested elsewhere.]

Here is what i put in my .emacs to do this :

(add-hook 'server-switch-hook 
  (lambda ()
    (local-set-key (kbd "C-x k") '(lambda ()
                                    (if server-buffer-clients

Like this C-x k work the usual way when i'm not finding a file from emacsclient (which for me is ido-kill-buffer), and if i'm using emacsclient, C-x k does server-edit if the client is waiting, or run ido-kill-buffer otherwise (if i used emacsclient -n).

You know, I hate to suggest workarounds instead of a real solution... but after reading the server code, I am confused as to how emacs even determines that a buffer is a server buffer.

With that in mind, why not open up files like:

emacsclient --eval '(find-file "/path/to/file")'

This way emacs doesn't know that your buffer was opened via emacsclient, which sounds like what you really want.


I'm not really happy with this, but it seems to work:

(global-set-key (kbd "C-x k") (lambda () (interactive) (server-kill-buffer (current-buffer))))

Okay, THIS did work for me:

(global-set-key (kbd "C-x k") '(lambda ()
  (if server-buffer-clients

(this is the code from IvanAndrus's answer with the explicit changes from edits and comments, and using jrockway's keybinding.)

And, yes -- I am rebinding a standard keybinding. BUT it's a functional replacement NOT something completely different (eg, replacing kill-ring-save with kill-buffer stuff).

BTW, EmacsWiki has a couple of pages on this topic-- KillKey and KillingBuffer -- neither of which shed better light for me than the above (although the first-function on KillKey also used "server-edit"...).

I am not sure if that will work on windows, but on linux, emacs -daemon is just great. With it, you don't have to run a different program, and your bindings are the same. I guess there are other advantages, but since I could never learn those emacsclient bindings, I never really used it, and can't say.

I don't think -daemon had been released yet, I am using from CVS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow