I would like to use a standard desktop save file when using Emacs as a server (with Emacsclient) and use no desktop save file (or optionally, another desktop save file) when running a regular Emacs instance (no server-start command).

Is it possible to use a fixed desktop save directory name with desktop-save-mode?

If I use:

(desktop-save-mode 1)

I have no direct control over where the desktop is saved (though, I think it is saved in ~/.emacs.d/.emacs.desktop).

I tried to add the following to my Emacs init file:

(require 'desktop)
(defvar my-desktop-dir "~/emacs-server")
(when (file-exists-p (desktop-full-file-name my-desktop-dir))
  (desktop-read my-desktop-dir))
(add-hook 'kill-emacs-hook (lambda () (desktop-save my-desktop-dir))))

while this seems to work, I am not sure if desktop auto save will be enabled.. From the manual :

When desktop-save-mode is active and the desktop file exists, Emacs auto-saves it every desktop-auto-save-timeout seconds, if that is non-nil and non-zero.

so in order to make desktop-save-mode active, I have to run (desktop-save-mode 1) in my init file. The problem is that this command then will revert to the desktop stored in ~/.emacs.d, which is not desired.

2 Answers 2


Using (setq desktop-path (list my-desktop-dir)) before calling (desktop-save-mode 1) seems to solve the issue:

(require 'desktop)
(setq desktop-path (list "~/emacs-server"))
(desktop-save-mode 1)

Note: You will now get a question Save desktop? (y or n) when killing Emacs server and this is the first time you save the desktop..

  • emac 27. This isn't working.
    – RichieHH
    Commented Dec 30, 2020 at 9:16

You should use desktop-change-dir to instruct emacs to load the desktop file at a given location, and remember it for auto-save later.

(desktop-change-dir "~/emacs-server")
(desktop-save-mode 1)
  • Thanks @Francesco. But for some reason, this leaves me with a lock in directory "~/emacs-server" after I exit Emacs, such that the next time I run Emacs as a server, I get a warning: desktop file appears to be in use. Using it may cause conflicts. Use it anyway? (y or n) Commented Feb 9, 2015 at 10:31
  • 1
    It seems like using (setq desktop-path (list "~/emacs-server")) instead of (desktop-change-dir "~/emacs-server") solves the issue.. Commented Feb 9, 2015 at 10:33
  • If you could incorporate my last comment somehow in your answer, I will accept it. Commented Feb 9, 2015 at 11:34
  • Yep, desktop-path is the way to go. Commented Feb 9, 2015 at 12:21
  • 1
    I could of course incorporate your comment about desktop-path in my answer, but it looks to me like it is a different solution. It would probably be clearer if you put it in an answer of your own, don't you think? Commented Feb 9, 2015 at 12:32

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.