10

I am trying to set up the emacs server daemon and use emacsclient on MS Windows so that files can be opened quickly without the delay and overhead associated with starting additional instances of emacs for each file opened. I am following the specific instructions provided in emacswiki for MS Windows which involves:

  1. Running emacs as a daemon, runemacs.exe --daemon
  2. Followed by invoking emacsclientw.exe with the appropriate options.

Starting the daemon succeeds (I see emacs.exe in the process list), but invoking emacsclient produces errors. The wiki says to create a shortcut to emacsclientw.exe and change the target to

X:\path\to\emacs\bin\emacsclientw.exe -na "X:\path\to\emacs\bin\emacsclientw.exe" -c -n

Is this correct? It looks like the client is invoked twice. Using the target as shown does not run the program and produces the error "file name or argument required". Using only the first part of the target shown above

X:\path\to\emacs\bin\emacsclientw.exe -na

produces a generic error. Using only the second part

X:\path\to\emacs\bin\emacsclientw.exe -c -n

produces the error "No socket or alternate editor." (The official emacsclient options are described here.)

Any suggestions? Emacs runs fine in standard mode, that is, not in daemon mode. Version 25.1. I would prefer to run the daemon rather than starting the server in my initialization file.

4

I am not so sure about the advice in the emacswiki page. What you want to do is to the following:

  • Launch an emacs client connected to the running emacs server.
  • If the emacs server is not running, start it, and then connect the emacs client.
  • Don't wait for the client to finish (if you're starting from a command line).

The command I use is

C:\emacs\bin\emacsclientw.exe -n -c -a ""

The -n means don't wait for the client to finish when you're launching from command line. The -a allows you to specify an alternative editor if the emacs server isn't running, but if you give it an empty string, it will try to start the emacs server and connect to it. Finally, if you're putting this in a windows shortcut, you want to also add a -c which will open a new frame (see emacsclient options).

Here's my shortcut, it works for me (on windows 7, emacs 25.3). I don't know why the emacswiki page put another invocation of emacsclientw as the alternative editor that makes no sense.

I should add that when I try to omit the -c option when running as a shortcut, it gives an error.

emacsclientw shortcut

  • Configuring the emacsclientw.exe shortcut in the way shown produces: Error: Cannot connect even after starting the Emacs daemon Confirmed this shortcut does launch the daemon process, but files still open in their own emacs instance. Tested with and without administrator priveleges. Emacs 25.1, Windows 7. – Snelephant Feb 4 '18 at 1:14
  • @Snelephant, hmm, I don't know what could be wrong. I even downgraded to emacs 25.1 and my shortcut still worked. How are you opening files through a context menu or with the command in the shortcut followed by the filename? – Angelo Feb 4 '18 at 14:07
  • The error occurs both when I click the shortcut to emacsclientw.exe itself with no file specified and when I click a shortcut to a text file with the emacsclientw.exe and file name specified in the Target field. Actually, I am not clear on the intended method for opening files into the client on Windows? I would like to just click the file in Windows Explorer. Clicking a file in OSX seems to open the file in an existing instance, but clicking a file in Windows creates a separate instance. Note I have configured Windows to open text files using the runemacs.exe provided with emacs. – Snelephant Feb 5 '18 at 2:39
  • If you have configured Windows to open text files with runemacs.exe, then they will open in separate Emacs processes. You'd want to change that association to use emacsclientw instead. – glucas Jun 1 '18 at 5:03
  • @glucas Changed text file associations to the shortcut to emacsclientw.exe as Angelo described. Clicking a text file produces the same Error: Cannot connect even after starting the Emacs daemon. – Snelephant Jun 13 '18 at 17:56
1

The command I use is:

emacsclientw.exe --create-frame --no-wait --alternate-editor runemacs.exe --server-file %APPDATA%\.emacs.d\server\server

(If the Emacs bin folder is not on your PATH, use the full paths for emacsclientw.exe and runemacs.exe.)

or with the short option names and the empty string trick,

emacsclientw.exe -c -n -a "" -f %APPDATA%\.emacs.d\server\server

Note that the empty string trick for -a will start the daemon if it isn't running, but this isn't supposed work until Emacs 26 (I'm still on 25). Before 26 you have to specify the alternative editor explicitly. I used runemacs.exe, but my Emacs is configured to start the server and leave it running.

Because Windows lacks the local Unix domain sockets emacsclient normally would have used to connect to the server, it has to fall back to using a network TCP socket. For obvious security reasons, you don't want just anyone on your network to access your whole file system via the Emacs server. Thus, emacsclient must know the right (ephemeral) port and security token written in the server file before the server will allow it to connect.

If the server is already running, then you shouldn't need the -a (but it doesn't hurt). You could set the ALTERNATE_EDITOR environment variable instead of using -a.

If you set the EMACS_SERVER_FILE environment variable, then you don't need to specify it again with -f.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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