I would like to keep distinct desktop files in order to be able resume sessions based on the mode.

When I open Emacs it should load the desktop file for the mode it starts with and when I run, say, M-x latex-mode it should load (and later overwrite) another desktop file. In my noobness I had hoped a

(setq desktop-path '("~/.emacs.d/desktops/plain" ~/.emacs.d/desktops/latex-mode"))
(defun my-TeX-hook ()
  (desktop-change-dir "~/.emacs.d/desktops/latex-mode"))
(add-hook 'TeX-mode-hook 'my-TeX-hook)

in my .emacs would work, but it didn't.

1 Answer 1


This is simple to do, if you use library Bookmark+.

You can easily create any number of desktop bookmarks, including multiple such in the same directory. And you can then easily switch to any of them just by "jumping" to its bookmark.

And you can of course jump to a bookmark, including a desktop bookmark, from a mode hook. E.g., use something like this on your mode hook, where my-desktop-bmk-42 is the name of one of your desktop bookmarks:

(add-hook 'TeX-mode-hook (lambda () (bookmark-jump "my-desktop-bmk-42")))

If you want to be sure your bookmark-file is already loaded (so my-desktop-bmk-42 is defined), then use this:

(add-hook 'TeX-mode-hook (lambda ()
                           (bookmark-jump "my-desktop-bmk-42")))

You can of course define that function as a named function, or even as a command:

(defun switch-to-desktop-42 ()
  "Jump to desktop bookmark `my-desktop-bmk-42'."
  (bookmark-jump "my-desktop-bmk-42"))
  • Thanks, this is better than ideal. Now I'd like to ask you some questions :-) 1) Is bookmarks+ compatible with desktop? I mean, can I tweak it by setting desktop-save, desktop-path and so on? 2) Can I set it up so that it auto-saves? Last thing, I tried (add-hook 'kill-emacs-hook 'bmkp-desktop-save-as-last) but it didn't work. Did I do something wrong or what? Commented Sep 20, 2016 at 13:48
  • Yes. Bookmark+ makes no changes to desktop.el code. You can autosave Emacs in a desktop, but you probably won't want to use that desktop for a bookmark. Use bmkp-set-desktop-bookmark (C-x p K) to set (create or update) a desktop as a particular bookmark. For your question about bmkp-desktop-save-as-last you will need to provide more detail: a step-by-step recipe to repro, explaining what you saw and what you expected to see instead. Send that to me as a bug report (that's not what emacs.SE is for): M-x customize-group bookmark-plus, then click the button/link Send Bug Report.
    – Drew
    Commented Sep 20, 2016 at 17:16

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.