Run one Emacs daemon and open frames with emacsclient
Keep one Emacs process running and open terminal or GUI frames on demand with the built-in server and emacsclient.
31 Jul 2025, 01:36 UTC

Start Emacs once, reuse it everywhere
If you close Emacs to free memory, you pay for it on the next launch: init.el runs again, packages reload, and your open buffers disappear. The practical alternative is to start one Emacs process as a daemon and open new frames on demand with emacsclient. All clients share buffers, major modes, and package state, so switching between a terminal frame and a graphical frame feels like opening a new window in the same session.
How the built-in server works
Emacs includes a built-in server. When you run emacs --daemon or evaluate (server-start), Emacs creates a server socket owned by your user account. Later, emacsclient connects to that socket instead of starting a new editor process. A frame in Emacs terminology is a window; emacsclient -c opens a graphical frame, while emacsclient -t runs inside your current terminal.
Worked configuration
Add this to your Emacs initialization file, usually ~/.emacs.d/init.el or ~/.emacs.el:
(server-start)
Then export editor variables in your shell startup file, such as ~/.bashrc or ~/.zshrc:
export EDITOR="emacsclient -c -a ''"
export VISUAL="$EDITOR"
export ALTERNATE_EDITOR=''
The -a '' argument tells emacsclient to start the daemon automatically if no server is running. Because every Emacs launch registers as a server after (server-start), external tools such as git or crontab open files in the existing session instead of spawning a fresh editor.
Start the daemon
Run this in an ordinary user shell; root permissions are not required:
emacs --daemon
Expected result: the command returns to the prompt with no output. A socket file is created under your runtime directory, typically inside $TMPDIR or /tmp. Risk: if the graphical display environment variable $DISPLAY is unset or wrong when the daemon starts, later emacsclient -c calls may fail to open a graphical frame until the daemon is restarted with the correct environment.
Connect from the terminal or GUI
| Command | What it starts | Best for |
|---|---|---|
| emacs | New process, reads init file | First launch or isolated session |
| emacsclient -c | New graphical frame on existing daemon | GUI editing without restart |
| emacsclient -t | New terminal frame on existing daemon | Quick edits inside SSH or terminal |
emacsclient -c: opens a new graphical frame in the running session.emacsclient -t: opens a frame inside the current terminal.emacsclient filename.txt: opens the file in an existing frame or creates one.
If you set ALTERNATE_EDITOR='', you can omit -a '' on individual commands.
Verify the server is running
Run this from any shell:
emacsclient --eval '(server-running-p)'
Expected output: t. If it returns nil, no server is active. You can also open a file with emacsclient -t, press C-x C-b, and confirm that buffers from another client are still listed.
Limits and common mistakes
- One process is a single point of failure. Running
M-x kill-emacsor hitting a fatal Lisp error closes every connected frame and can discard unsaved edits across all clients. - Shared state leaks between clients. Background processes, temporary buffers, and dynamic variable bindings belong to the daemon, not to an individual frame. A long-running async job in one terminal frame can block or affect others.
- Socket ownership restricts access. The server socket belongs to the user who started the daemon. Other users on the same machine cannot connect without explicit permissions or a separate instance.
- Display configuration matters. A daemon started without the correct
$DISPLAYor Wayland socket may not open graphical frames until restarted with the right environment.
Turn the server off
Because starting the daemon changes system state, you can reverse it by telling the running Emacs to shut down:
emacsclient --eval '(kill-emacs)'
Warning: this closes every frame and may discard unsaved work. To prevent the server from starting automatically, remove (server-start) from your init file and restart Emacs normally.
Practical check
Start the daemon, open emacsclient -c, edit a buffer, then open emacsclient -t in another terminal and run C-x C-b. The buffer you edited should appear in the list. If it does not, the clients are not attached to the same server.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.