Making Emacs Work for You: Setting up Your Edit Server and Bolstering Your Editing Workflow

Today our goal is to set up Emacs as an edit server and integrate it efficiently in a Linux based workflow.

In UNIX systems one would normally set one's text editor as the default by specifying the environment variable EDITOR.

export EDITOR=ed

Setting 'emacs' as the editor in this way does not produce the best results since Emacs would then start on demand but as a new instance!

If you already had a version of emacs open, the new instance would not share share buffers, a command history, or other kinds of information with the existing Emacs process (see Emacs Manual 40).

The better option is to set up emacs as an edit server. This way, you have a single Emacs instance waiting for requests from the system to edit text files and it handles all your editing requests.

The term "server" is somewhat misleading, since we tend to think of servers in the context of large-scale, networked environments where they manage requests from multiple clients over a network. In the case of Emacs, however, the "server" is just a single program running on your local machine, essentially "serving" your editing needs on demand. Think "serve ya" rather than "traditional network server".

The Emacs server is not a separate executable with a distinct name like many traditional server programs. Instead, the server functionality is integrated within Emacs itself once Emacs is started as a server. When you enable server mode in Emacs, the main Emacs process takes on the role of a server.

There are various ways to start an Emacs server:

From within Emacs:

M-x server-start

M-: (server-start)

Add (server-start) to dot emacs.

From outside Emacs

Start Emacs as a background daemon:

emacs --daemon

When Emacs starts this way, it calls server-start after initialization and does not open an initial frame. It will then waits for edit requests from its emacsclient.

The daemon runs in the background, independent of user interfaces.

In ancient Greek mythology a daemon could be either a good or evil force that influenced human events for good or ill. In Christianity the term "demon" has a pejorative force. But "daemon" can be positive. Socrates is said to have been guided by a daemon spirit.

When emacs is started with the –daemon option, it is both a daemon (running in the background) and a server (providing editing services). The terms daemon and emacs are thus related but not to be confused.

Run the emacsclient command with the "alternative editor" option

This will start the daemon if it is not already started and run emacsclient.

emacsclient --alternate-editor""=

This approach is convenient because it automates the decision-making process. You don't have to remember to start Emacs as a daemon beforehand; the emacsclient command handles it for you.

System wide

If your Linux distro uses systemd to manage startup, you can automatically start Emacs in daemon mode when you login using the supplied systemd unit file. To activate this:

systemctl --user enable emacs

My approach

if ! pgrep -x "emacs" > /dev/null
then
    emacs --daemon
else
    echo "Emacs daemon is running."
fi

function Open-With-EmacsClient {
    emacsclient --no-wait --alternate-editor="" -c "$1"
}
alias eo='Open-With-EmacsClient'

alias erc='emacsclient -cn ~/.bashrc &'

This script check if Emacs is already running. If not, it starts it in daemon mode.

It also has a function for starting the emacsclient from the terminal. The --no-wait option makes sure that the terminal is freed for a new command after you invoke the emacsclient.

--alternate-editor""= ensures that if, for some reason, the daemon is not running at that moment, it will start Emacs as a daemon.

-c instructs emacs to open a new graphical frame.

Opening files with emacsclient

With the above in place, one would like then to associate emacsclient with certain file types, especially if you frequently open files from a file manager or desktop environment. The best way to do this is to right-click on a file type (.txt, .md, .org), go to properties and associate that file with the emacsclient. Emacs will then open up all files externally in lightening speed.

Final consideration

In some desktop environments you would want to configure the default behavior of emacsclient when opening files from the file manager. For instance, whether it opens in a new frame -c or in an existing frame, and whether it should wait for you to finish editing before returning control to the file manager --no-wait.

My desktop environment "Cinnamon on Arch Linux" seems to manage this automatically.

Return to Home