Emacs Optionally Saving Desktop on Exit With Confirmation
I recently read an article about connecting different Emacs functions; unfortunately I can’t recall the title or url of the article.
After having the idea of that article in the back of my mind for a bit, it connected with one of the things I wanted to do with my Emacs: give myself the option1 to save the state of Emacs upon exiting.
I knew about Saving Emacs Sessions.
The only remaining thing was connecting desktop-save
to killing Emacs.
The first thought crossing my mind was advising save-buffers-kill-terminal
function,
bound to C-x C-c
which is the way I quit Emacs.
Visuwesh was quick to point me towards kill-emacs-hook
and thus the final solution is the following hook:
(use-package emacs
:ensure nil
:hook
(kill-emacs . (lambda () (if (y-or-n-p "Save desktop?")
(desktop-save (expand-file-name "~/.emacs.d/") t)))))
I don’t want the session being saved automatically; nor do I want it loaded automatically. ↩︎