1

If I set alternate_editor to an empty string, emacsclient runs "emacs --daemon" and then tries to reconnect. However, I want to run it a specific emacs executable that is not in my path. If I set alternate_editor to "/path/to/my/emacs --daemon" it runs the daemon but does not try to reconnect to it. How can I tell emacsclient that it should try to reconnect to the deamon?

Background: I want to create a bash file named emacs in /usr/local/bin that calls "emacsclient -c" so that when other applications try to start emacs they start the client instead. However, I have to make sure that this script calls /usr/bin/emacs and not itself as the alternate editor.

2
  • Are you sure that you do not just want to start the emacs-server at startup (e.g. using systemd)? Otherwise, FYI here when I set some alternate-editor either as a bash argument (within my test script), or by exporting the environment variable, then emacsclient does also connect to it (when starting it. It does not open the file I pass to it, which might be because I am using Spacemacs, but anyway it does connect succesfully). Commented Nov 30, 2021 at 11:32
  • I tried to start emacs-server at startup using SysV init. However, it only works for the non-X version. The issue probably is that I don't start X by default. But even if I would, I'm not sure how I can start the emacs-server after X is running.
    – M0M0
    Commented Dec 30, 2021 at 10:40

1 Answer 1

0

Since this question was not answered so far, I present my hacky workaround here. If the "real" emacs is /usr/bin/emacs a script in your $PATH called emacs with the following content will work:

#!/bin/sh
#
#Starts emacs-server if it is not running. Afterwards opens file with emacsclient in new frame.
#
ps -A -o cmd | grep -q -x "/usr/bin/emacs --daemon" || /usr/bin/emacs --daemon
[ -z "$1" ] && emacsclient --create-frame --no-wait || emacsclient --create-frame --no-wait "$1"

However, if your init system or another user has started the emacs-server with a command different from the string /usr/bin/emacs --daemon it will try to start it again, which might cost a few seconds.

A less elegant workaround could be something like this:

In your $PATH is a shell script named emacs with the following content:

#!/bin/sh
#
#Starts emacs-server if it is not running. Afterwards opens file with emacsclient in new frame.
#
emacsclient --alternate-editor="emacs-server $1" --create-frame --no-wait $1

If the server is not running, it starts another shell script emacs-server with the following content:

#!/bin/sh
/usr/bin/emacs --daemon && emacs $1

This script first starts the daemon by calling the "real" emacs executable, and then calls the first script again and passes its argument (the file we want to open) back to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.