52

I'd like to use emacsclient to edit emails in Mutt.

I added this in .emacs

(server-start)

And in .muttrc I added

set editor="emacsclient -nw %s"

It seems they work. When I start a second Emacs, it complains there is already a server running so it issues errors. How to make sure to do (server-start) only if the server isn't already started?

Thanks

Meng Lu
  • 13,726
  • 12
  • 39
  • 47

5 Answers5

72

This code starts the server only if it's not running:

(load "server")
(unless (server-running-p) (server-start))
Philipp
  • 48,066
  • 12
  • 84
  • 109
  • 2
    This is the only option that works on Windows currently since `emacs --daemon` says _This platform does not support the -daemon flag._ – legends2k Jan 06 '15 at 09:59
  • 1
    @legends2k, on Windows you can also do `emacs --funcall server-start`, but I agree this is better and works cross-platform. – harpo Sep 01 '16 at 16:13
  • When I this lines to init.el, `M-x` returns `Error reading from stdin`. Is there any way to fix this problem? – alper Feb 23 '21 at 20:21
  • I found that `(load "server")` resulted in errors when trying to use magit (in particular, on Windows). Using `(require 'server)` instead prevents these errors. – Mark Aug 26 '22 at 18:34
54

The emacs daemon can be started automatically in a very simple manner. Just add this to your .bashrc/.zshrc/whatever

export ALTERNATE_EDITOR=""

Now when you invoke emacsclient (using either --tty or --create-frame) the server will be started (with emacs --daemon) if it's not already running.

I also find this shell alias handy:

alias e='emacsclient --tty'

Note that since Emacs 23 this is the preferred way to use Emacs in daemon mode. (start-server) is now mostly deprecated.

bignose
  • 30,281
  • 14
  • 77
  • 110
Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
  • This is a nice solution. `emacsclient -c` also works, and supports graphical clients. However, how would you go about opening a file in an existing emacs frame (say, in another terminal). Currently, I have a bash script called `e` that checks to see if the first argument is `-o` (for other) and runs `emacsclient` without `-c` if it does. Is there a better way, using the alias, that you can override the `-t` and cause an existing frame to open the file? – edam Oct 23 '12 at 14:18
  • @edam not sure if you found your answer, but it sounds like you want the -t switch to emacsclient. It's a bit hard to tell because your comment seems to blur the distinction between a non-graphical emacs instance in a terminal, with a windowed graphical instance. -t is for the former, -c is for the latter. – Daniel Mar 09 '15 at 17:02
  • @Daniel: I got it sussed. [Here is my script that I start emacs with](http://pastebin.com/bxmMTTjr). You can run it like this: `$ e -o /path/to/file` to send the file to another emacs already running elsewhere (which is useful). And it detects when it's being run inside a shell in emacs (the results are bad otherwise!). It always runs emacs in a terminal, although you can easily remove the `-nw` at the end to change this. – edam May 01 '15 at 10:19
  • 6
    Can you provide some reference for the claim that "this is the preferred way to use Emacs in daemon mode" and that "`(start-server)` is now mostly deprecated"? I don't see that mentioned in the most recent documentation, nor anywhere else. Thanks. – harpo Sep 01 '16 at 16:00
  • Agreed i am not sure this is true, when i start emacs with -c or -t it actually tells me to start the server. "To start the server in Emacs, type "M-x server-start"." so that makes it sound like thats the preferred way. – Oly Apr 25 '17 at 07:48
  • To add to the request for reference of best practice, I would love to have some more information about exactly why this works the way it does. – trey-jones Oct 15 '19 at 12:51
16

A bit of a late answer, but here is the solution that works for me. Whenever I start emacsclient, I use emacsclient -a '' -c The -a '' tells emacsclient to attempt to connect to an existing server, and if no server exists, start one then connect to it.

Eldritch Cheese
  • 1,177
  • 11
  • 21
  • 1
    The `-a ''` option is great. Note that `-c` always create a new frame. If typically want to edit the file in the existing emacs frame, so I omit this option – Fred Schoen Oct 22 '17 at 21:52
8

Avoid the problem alltogether via

emacs --daemon

in any shell or terminal so that Emacs runs in the background. That way emacsclient is always happy as there is always an Emacs server to connect to.

This being Emacs, there is also a function that starts the server only when needed but I can't quite recall its name right now. I use the --daemon option happily quite happily myself.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 1
    I cannot agree more, the beauty of `emacs --daemon` shines when I put this in my standard Startup Applications Prefs in Ubuntu `/usr/bin/emacs --daemon` and whenever I startup my machine I can swiftly jot down a floating idea to my capture template in emacs by hitting `Super + I` (a keyboard shortcut to `emacsclient -c --eval '(progn (org-capture) (delete-other-windows) (setq-local kill-buffer-hook (lambda () (delete-frame))))'`) and I get a nice emacs frame telling me what annoying ideas you want to get rid of off my head. – doctorate Aug 27 '18 at 10:48
  • should we run `emacs --daemon` as `emacs --daemon &` on the background? – alper Feb 21 '21 at 23:20
  • I think it disappears by itself so if you say `emacs --daemon` you do get your prompt back. Try it, and follow-up with `emacsclient -nw` ! – Dirk Eddelbuettel Feb 21 '21 at 23:25
  • `emacsclient -nw` keep returns `emacsclient: connect: Connection refused` error message not sure why – alper Feb 23 '21 at 20:22
  • Then you have no daemon runnning. – Dirk Eddelbuettel Feb 23 '21 at 20:24
1

Add this to your .bashrc/.zshrc

if ! ps -e -o args | grep -q '^emacs --daemon$'; then
  emacs --daemon
else
  echo "Emacs server Online"
fi

Update: now I prefer to use this line instead:

if ! ps -e -o args | grep -i 'emacs' | grep 'daemon'; then

because the process name will depend on your machine or the way you installed emacs.


Now your shell will start the deamon on startup, but only if it is not already running. Less wait time when you first run emacsclient -t, and it is faster than letting emacs --daemon check if it's already running.

As an alternative you could simply add:

eval 'emacsclient -e "(server-running-p)"'
Jose V
  • 1,655
  • 1
  • 17
  • 31
  • 1
    I needed to use `ps -xe -o args | grep -q '^emacs --daemon'` on FreeBSD, because without the x option ps would not show emacs --daemon, and it also prints the version number, so I dropped the $ on the end. – peterhil Nov 18 '21 at 00:32