Intro

If you run Emacs as a daemon (and you should), you know the drill: you update your config, install a new package, or tweak some settings, and now you need to restart. The typical approach is to kill the daemon from the terminal and start it again manually. It works, but it’s clunky.

There’s a better way. A simple Emacs function that saves your work, restarts the daemon cleanly, and gets you back up and running without leaving Emacs. Below is the function and how to use it.

The Function

(defun my/emacs-restart-daemon ()
  "Restart the Emacs daemon cleanly."
  (interactive)
  (save-some-buffers t)
  (let ((cmd (concat invocation-directory invocation-name)))
    (call-process-shell-command
     (format "%s --daemon &" cmd))
    (kill-emacs)))

Add this to your Emacs config (typically ~/.emacs.d/init.el or your Spacemacs config). Once loaded, you can call it with M-x my/emacs-restart-daemon.

Use Case

This is particularly useful when:

  • You’ve updated packages and need a clean restart
  • You’ve modified your init file and want the changes to take effect
  • You’ve installed system-level dependencies (fonts, libraries) that Emacs needs to reload
  • You’re debugging config issues and need to restart frequently

Before this function, I’d drop to the terminal, run pkill emacs, wait a moment, then run emacs --daemon again. Now it’s just M-x my/emacs-restart-daemon. Much faster.

A Word of Caution

This function kills your current Emacs session. If you have unsaved buffers, (save-some-buffers t) will save them automatically without asking. Make sure you’re comfortable with that behavior. If you prefer to be prompted, change t to nil:

(save-some-buffers nil)

This will ask you about each modified buffer before saving.

Final Thoughts

Running Emacs as a daemon is one of the best workflow improvements you can make. Near-instant startup times, persistent state, and the ability to connect from multiple clients make it indispensable. This function just makes managing that daemon a bit easier.

If you’re not running Emacs in daemon mode yet, start with emacs --daemon and connect with emacsclient -c. You won’t go back.