Discussion on how to start Emacs Server

Original link: https://emacstalk.github.io/post/020/

On Unix-like operating systems, the default text editor for command-line tools (such as git commit) can be configured by setting the EDITOR environment variable, for example:

 export EDITOR = vim export EDITOR = emacs

In this way, a new editor instance will be created every time, which is fine for a lightweight editor like Vim, but a bit heavy for a Swiss Army Knife like Emacs, who is proficient in all kinds of martial arts. The ideal state is to use Emacs as a resident process similar to a browser. When needed, a new frame/window is just fine. At this time, the Emacs Server mode is used.

As the name suggests, the Server mode only needs to be started once, and the connection can be made through emacsclient in subsequent use. In general, add the following command to the configuration file:

 (unless (server-running-p) (server-start))

Or you can execute emacs --daemon on the command line, it will automatically call server-start after loading the user configuration. Different from the former, an error will be reported when repeated in this way:

 Starting Emacs daemon. Unable to start the daemon. Another instance of Emacs is running the server, either as daemon or interactively. You can use emacsclient to connect to that Emacs process. Error: server did not start correctly

After some exploration, the author found that the server mode can be started directly through emacsclient. The command is as follows:

 emacsclient -a "" -c -n " $@ "
  • -a is short for alternate-editor, which means an alternative editor when connecting to the server fails. When it is empty, it will automatically execute emacs --daemon , and then connect again. This solves the perfect solution to the error when calling daemon repeatedly, and can also start server mode on the first call

  • -c means to create a new frame instead of reusing the existing one

  • -n means to exit immediately after executing the command without waiting for the server to return. This is also very important, otherwise the terminal will not return immediately.

By making the above command an alias (such as e ), it can be easily invoked on the command line, such as e ~/.bashrc , which is more convenient than vim.

This article is reprinted from: https://emacstalk.github.io/post/020/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment