I would like to use DeskTop for basic project management, i.e. opening a set of buffers and restore histories depending on the project I am working on. Is this possible, i.e. having one desktop file in a project directory and how can I achieve this?
2 Answers
I needed to manage the desktop files just like you; have a separate desktop file for each project and save buffers, Emacs variables, etc independently for each.
I was able to achieve that using a package called bookmark+
.
bookmark+
Library Bookmark+ manages different types of bookmarks, one of those is Desktop Bookmarks.
After installing the package,
- You need to have
(require 'bookmark+)
in yourinit.el
- To create a bookmark for each project, set up the buffers you'd like for each project and do
M-x bmkp-set-desktop-bookmark
or C-x p K. That will ask you where you want to save the desktop file and you can choose to save it in that project's folder. - Once you have set the desktop bookmarks for all projects, you can jump to different bookmarks using
M-x bmkp-desktop-jump
or C-x j K.
The Bookmark+ doc on Emacs Wiki is very informative if you want to learn more about this package.
desktop.el
In addition to that, I have the following to set up the desktop
package where I can choose what all I want to save per desktop
(desktop-save-mode 1)
;; Source: https://github.com/purcell/emacs.d/blob/master/lisp/init-sessions.el
; save a bunch of variables to the desktop file
;; for lists specify the len of the maximal saved data also
(setq desktop-globals-to-save
(append '((comint-input-ring . 50)
(compile-history . 30)
desktop-missing-file-warning
(dired-regexp-history . 20)
(extended-command-history . 30)
(face-name-history . 20)
(file-name-history . 100)
(grep-find-history . 30)
(grep-history . 30)
(ido-buffer-history . 100)
(ido-last-directory-list . 100)
(ido-work-directory-list . 100)
(ido-work-file-list . 100)
(magit-read-rev-history . 50)
(minibuffer-history . 50)
(org-clock-history . 50)
(org-refile-history . 50)
(org-tags-history . 50)
(query-replace-history . 60)
(read-expression-history . 60)
(regexp-history . 60)
(regexp-search-ring . 20)
register-alist
(search-ring . 20)
(shell-command-history . 50)
tags-file-name
tags-table-list)))
Saving project-specific desktops when quitting emacs
I find it useful to bind the below function to C-x C-c so that the desktops get saved automatically when I quit emacs.
(defun save-desktop-save-buffers-kill-emacs ()
"Save buffers and current desktop every time when quitting emacs."
(interactive)
(desktop-save-in-desktop-dir)
(save-buffers-kill-emacs))
At times, I wouldn't want to save the desktop when quitting emacs. For those occasions, I use this other function and have bound that to C-x M-c.
;; Kill emacs when running in daemon mode or not
;; Source: http://lists.gnu.org/archive/html/emacs-devel/2011-11/msg00348.html
(defun tv-stop-emacs ()
(interactive)
(if (daemonp)
(save-buffers-kill-emacs)
(save-buffers-kill-terminal)))

- 75,699
- 9
- 109
- 225

- 25,203
- 3
- 74
- 179
-
Thanks - that looks VERY promising. I found the combination of bookmarks+ and DeskTop before, but could not find a concise way of doing it. This sounds great. I'll try it out latest Mondays! – Rainer Sep 26 '14 at 18:51
-
1Note that, unlike the case for the vanilla Emacs desktop commands, **Bookmark+** desktop bookmarks are intended for switching among multiple desktops. IOW, you can have multiple desktop files. And your desktop files can be anywhere. They need not be in different directories. Vanilla desktop use presumes that you have only one desktop file per directory. – Drew Sep 26 '14 at 22:47
-
@kaushalmodi Thanks - brilliant. And now I have a lot to read about bookmark+ - but it does exactly want I want. – Rainer Sep 30 '14 at 09:21
-
@Drew this is exactly what I was looking for - and it works like a charm. – Rainer Sep 30 '14 at 09:21
-
@kaushalmodi I assume that you are using your exit function to be able to quit emacs without saving the desktop - or is there a difference, when wanting to always save the desktop, to using the variable ```desktop-eve``` to ``ask-if-new```? – Rainer Sep 30 '14 at 09:46
-
@Rainer You mean the `desktop-save` variable. Sometimes I just need to quit without emacs asking me if I need to save the desktop. By default I want to save an existing desktop and be asked if I want to save a new desktop. So I also have `desktop-save` set to `ask-if-new`. – Kaushal Modi Sep 30 '14 at 15:19
-
For `(setq desktop-globals-to-save` , emacs 27 says: `assignment to free variable ‘desktop-globals-to-save` would it be any problem? Should I use different variable to set? – alper Jul 18 '21 at 18:52
I tend to use the following setup to save and load/read the desktop file from the local directory of the respective projects:
(require 'desktop)
(setq desktop-path (list "./"))
(desktop-save-mode 1)
(desktop-read)
This is not without issues as switching projects via e.g., projectile or other project management utilities does not load any desktop files but one can utilize the projectile-after-switch-project-hook
function to call a private function to do the needful

- 103
- 4