I am trying to setup emacs as server so that emacs (same instance) can be invoked from a file browser like nautilus

Results not satisfactory

Attempt 1

I have the foll. in ~/.local/share/applications/emacs.desktop

[Desktop Entry]
GenericName=Text Editor
Comment=Edit text

Exec=emacsclient -n -a emacs %F

Along with (server-start) in my init

When I try that I get two icons for emacs in ubuntu launcher -- one is 'fake' ie it does not respond to click


Attempt 2

  1. emacs started with --daemon
  2. emacs.desktop as earlier described

However now closing a window with the 'X' does not prompt to save files Nor at system shutdown time

Attempt 3

Following https://stackoverflow.com/questions/1167484/how-to-gracefully-shutdown-emacs-daemon

tried adding this to my init

 (defun shutdown-emacs-server () (interactive)
   (when (not (eq window-system 'x))
   (message "Initializing x windows system.")
   (when (not x-display-name) (setq x-display-name (getenv "DISPLAY")))
   (select-frame (make-frame-on-display x-display-name '((window-system . x)))))
 (let ((last-nonmenu-event nil)(window-system "x"))(save-buffers-kill-emacs))) 

And then an explicit shutdown command needs to be issued:

$ emacsclient -e '(shutdown-emacs-server)'

But this is unreliable -- sometimes it pops up the save dialog, sometimes not

So any suggestions on how to use emacs as a server?

[Ubuntu xenial running unity]

3 Answers 3


Closing The Client

Keep in mind that when you are running an emacs daemon, the frame in which you are actually editing is simply a client of the server. The file is not buffered in the client, it is buffered in the server. When you close the client you are not killing the server process, nor is the file closed. The file you were editing remains open in the server process.

When you open a new emacs client you can see this is the case by changing buffers C-x b to the file you were editing earlier. Even so, when I attempt to kill a client using C-x C-c I am still prompted to save my work if I have not done so already. If I don't save on exit, the work is still there when I open a new client, which is the important thing here.

Unsaved Work

When the server is killed with unsaved work in one or more buffers, you can usually recover at least some of that work by recovering the file. Emacs will typically auto save your documents for you while you are working. To recover a file from an auto save you essentially have two options:

  1. M-x recover-file prompts you for a file name from which to recover.
  2. M-x recover-this-file searches for an auto save for the file currently read into the buffer.

I realize these two functions are not a fix for your problem, but they have served me well in the past.

As an aside, if you take care to save your work regularly, this becomes less of a problem, especially if your daemon is a long running process. My kids used to accidentally turn my server off regularly before I disconnected the power button. There was a time or two that a file had not been saved and I was able to recover my work with the above functions.

A Couple Of Suggestions

  • You could set up the emacs daemon to run as a service on your system if you always want it available. This would also give you the flexibility of using a script as the exec command for restarting and shutting down. You could add code there to ensure that everything saves before shutdown or restart. That strikes me as more of a fun project than a practical one, though. I very rarely kill my server process.

  • A more practical suggestion I can offer is to issue the emacs client command with an empty string as the alternate editor like so: emacsclient -ca"". This causes emacs to attempt to create a daemon if one does not exist, then connect to it. The 'c' option forces emacs to create a new frame, which can be helpful if you are using Xemacs. From the man page regarding the -a option:

If the value of EDITOR is the empty string, run `emacs --daemon' to start Emacs in daemon mode, and try to connect to it.

  • If that last suggestion works for you, you can alias the command to something like emax, as I have seen suggested elsewhere.


Emacs 26.1 now comes packaged with a systemd service file on some systems. This eliminates the need to use the alternate-editor argument on the command line. Simply make sure you start and enable the emacs service.


I think that your 2nd attempt is correct. You should, however, use C-x C-c to quit Emacs and not close the frame created by emacsclient directly through the GUI. I use C-x C-c to quit Emacs and it always asks me whether I want to save my modified buffers even if they aren't the ones I'm using.

On the other hand, it's to be expected that Emacs won't prompt you to save your files at system shutdown because it exists as a daemon but even if it doesn't, it still won't prompt you. If you do close the frame directly, you can always open it again and check whether you have saved the changes in your buffers.

  • The cost of losing files is bigger than the cost of a bogus icon. So for now staying with first
    – Rusi
    Commented Jun 15, 2016 at 13:08
  • You can do that but you'll have to start Emacs every time you open a new file if Emacs isn't already running. Some people do it and never close Emacs once they've opened it.
    – Libre Arch
    Commented Jun 15, 2016 at 13:16
  • 1
    Avoiding losing unsaved changes is what autosaves are for.
    – phils
    Commented Jun 15, 2016 at 14:41

The easiest way to do this is just to set up Emacs to start when you log in. On distros that use systemd this can be done in a way described here: https://wiki.archlinux.org/index.php/Emacs#As_a_systemd_unit

I set up my .desktop file for Emacs to use emacsclient -nc, but you shouldn't use the "n" flag in the EDITOR environment variable if you want to use Emacs when some program from command line wants you to edit some text (like git or something.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.