Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 38 ยท Workflows

The Emacs Server, emacsclient and Session Persistence

Start Emacs once and connect with emacsclient, run it as a daemon or service, use it as your EDITOR, and persist history, recent files and sessions.

A heavily configured Emacs can take a second or more to start. Emacs users rarely notice, because they start it once and keep it running for days. This lesson shows two complementary techniques: running Emacs as a server that new windows connect to instantly with emacsclient, and persistence features that remember your history, recent files and window layout across restarts.

Commands and Lisp are real. The runnable blocks are JavaScript stand-ins modelling emacsclient argument handling and a recent-files list.

The server

Emacs contains a small server. Start it inside a running Emacs with M-x server-start, or automatically in your init file:

ELISP
(require 'server)
(unless (server-running-p) (server-start))

Now a terminal command can hand files to that same Emacs:

Text
emacsclient notes.txt              # open in the running Emacs (shows it in the existing frame)
emacsclient -n notes.txt           # --no-wait: return to the shell immediately
emacsclient -c notes.txt           # --create-frame: open a new graphical window
emacsclient -t notes.txt           # --tty: open in the current terminal
emacsclient -e '(+ 1 2)'           # --eval: evaluate Lisp, print the result
emacsclient -a "" -c               # --alternate-editor "": start a daemon if none is running

When you finish editing a file opened with emacsclient (without -n), press C-x # (server-edit) to mark it done and return control to the waiting program. This is how Git or crontab -e can use Emacs: set export EDITOR="emacsclient -t" (terminal) or export VISUAL="emacsclient -c" in your shell profile. Git then opens commit messages in your running Emacs.

The daemon

emacs --daemon (or emacs --bg-daemon) starts Emacs with no window, only the server, ready to open frames on demand:

Text
emacs --daemon                 # start once at login
emacsclient -c                 # a new window appears in a few milliseconds
emacsclient -e '(kill-emacs)'  # stop the daemon

Keep a daemon running by launching it at login. On Linux with systemd, Emacs ships a user unit: systemctl --user enable --now emacs.service. On macOS a launchd agent or a login item can run emacs --daemon; on Windows the emacsclientw.exe program with --alternate-editor=runemacs.exe works as a shortcut target, so the first click starts Emacs and later ones reuse it.

You can run several named servers: emacs --daemon=work, and emacsclient -s work -c. Frames created by emacsclient -c share the same buffers, so a file opened in one window is the same buffer in the others. When the last frame closes the daemon stays alive, which is the point, though loaded buffers accumulate: occasionally use M-x clean-buffer-list or kill-some-buffers.

Gotchas: environment variables are inherited from when the daemon started, so PATH changes you make later are not visible; fix by using exec-path-from-shell or restarting. Themes and fonts for daemon frames should be set in default-frame-alist (not by calling functions that need a window at startup).

JS Stand-in: what emacsclient flags mean

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Remembering things across sessions

Even if you restart Emacs, you do not have to start from nothing. Built-in modes to switch on:

Mode Remembers
savehist-mode Minibuffer history (commands, searches, file names)
recentf-mode Recently opened files (M-x recentf-open, or consult-recent-file)
save-place-mode Your cursor position in each file
desktop-save-mode Open buffers, frames and window layout (the "desktop")
auto-save-visited-mode Saves your buffer to its file after a short idle time (Emacs 26+)
global-auto-revert-mode Reloads files changed on disk by other programs
bookmark Named places (see macros and registers)
ELISP
(savehist-mode 1)
(setq history-length 1000)
(recentf-mode 1)
(setq recentf-max-saved-items 200)
(save-place-mode 1)
(global-auto-revert-mode 1)
(setq desktop-restore-eager 10)          ; restore 10 buffers now, rest lazily
(desktop-save-mode 1)
(setq make-backup-files nil
      auto-save-default t)               ; keep #file# crash recovery

Auto-save files (#file#) protect you from crashes; recover with M-x recover-this-file. Backups (file~) are a different mechanism; you can send them to one folder: (setq backup-directory-alist '(("." . "~/.emacs.d/backups"))). For named sessions, M-x desktop-save in a project folder and M-x desktop-read later, or use the built-in M-x tab-bar-mode and window-configuration-to-register.

JS Stand-in: a recent-files list

Press Run (or Ctrl + Enter) to see the console output here.

No output.

If you use a daemon, desktop-save-mode is less important; the daemon already keeps buffers alive. Persistence modes still help after reboots.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does emacsclient -c file.txt do?

    -c creates a new frame connected to the running server.
  2. 2. How do you tell the waiting program (such as Git) that you have finished editing in emacsclient?

    server-edit marks the file as done and lets the caller continue.
  3. 3. Which mode remembers where your cursor was in each file?

    save-place-mode restores point in files; recentf lists files; savehist keeps minibuffer history.
  4. 4. Why might a command work in a terminal but not in a daemon-started Emacs?

    A daemon keeps the environment from when it started, so later PATH changes are not seen.

Summary

  • server-start or emacs --daemon plus emacsclient -c gives instant windows that share buffers.
  • -n returns immediately, -t uses the terminal, -e evaluates Lisp, -a "" starts a daemon if needed; finish with C-x #.
  • Set EDITOR="emacsclient -t" so other tools use your Emacs.
  • savehist, recentf, save-place, desktop-save-mode and global-auto-revert-mode carry state across restarts.
  • Auto-save files rescue you after crashes (recover-this-file).

Next: Writing and Note-taking Workflows