I had made some random notes in my scratch buffer.
By mistake, I pressed C-x C-c and killed the Emacs session.
All my content in the scratch buffer was gone.


When I try to close Emacs when the scratch buffer had been edited, I want Emacs to ask for a confirmation (offer to save or something) before it exits the current session. How can this be done? I'm not conversant with eLisp.

I searched the manual and there seems to be no way to change this behaviour.

Not duplicate:

My question is not duplicate to the question "never close scratch" in that, the other user is referring to killing the scratch buffer intentionally using kill-buffer C-x C-k. In my case, I unexpectedly exit out of Emacs either by closing the window or giving C-x C-c command.

  • that's a nature of scratch buffer - it's not linked to any file. and there is no need to change it, just create normal buffer (like scratch.el or something) and when you try exit emacs, you will be asked as usual what to do with changes. – rsm Nov 2 '17 at 14:02
  • Possible duplicate of never close scratch – Drew Nov 2 '17 at 14:48
  • @Drew It may not be a duplicate in that, I would not be killing the scratch buffer individually, but quiting Emacs kills it (even if modified,without warnings). All I'm looking for is a way, in which, Emacs informs the user that scratch buffer is modified and request for save if needed – Prasanna Nov 2 '17 at 17:02
  • This is not an answer to your question, but it is generally a good idea to have a safeguard against inadvertently quitting emacs. The first sexp sets a short form of answering to all confirmation requests. Add these to your emacs config: (fset 'yes-or-no-p 'y-or-n-p) (setq confirm-kill-emacs 'y-or-n-p) – Heikki Nov 3 '17 at 7:12

persistent-scratch is another package which saves and restores the contents of the *scratch* buffer. The following is a possible setup.

(use-package persistent-scratch
  :ensure t
  :commands persistent-scratch-setup-default
  :hook (after-init . persistent-scratch-setup-default))
  • This is what i was looking for. Thank you! – Prasanna Jan 23 at 17:12

This is a commonly asked question. It might be a duplicate here (dunno). There are various tweaks or libraries that some users use to provide what you are asking - you will likely get some answers pointing to them.

But my suggestion is to not use *scratch* the way you are using it - maybe not use it at all. Instead, use C-x C-f foo.el or whatever, which tells Emacs that you might want to save whatever you're doing in that buffer. Emacs will also automatically save your work periodically after you do save it, if you do. And you need not save if you don't ever want to.

IOW, you get all of the liberty of buffer *scratch* (throw it away when done), but with the advantage that Emacs will do just what you requested: let you know before you exit that you have unsaved changes in that buffer. And you can put the buffer in any mode you like - it need not be emacs-lisp-mode. And emacs-lisp-mode gives you as much Lisp support as the default mode for *scratch* provides (in my opinion).

*scratch* really is for stuff that you intend to toss.

  • 1
    "*scratch* really is for stuff that you intend to toss." Couldn't agree more. But I use it like scratch pad to keep a temporary copy of things, before they get better shape and go into some file. I don't want to lose it before my work is done - because of a mistake. I will have to resort to creating an extra file (foo), if I don't get a good solution – Prasanna Nov 2 '17 at 17:09
  • No, you don't have to "resort to creating an extra file (foo). That's the point. Use C-x C-f foo.el and never bother to save your scratch work to disk. (You can even have Emacs create such a buffer for you by default.) Because you visit it as a file buffer, even though there is no file backing it up, Emacs will let you know that your work is not saved when you kill the buffer or you quit Emacs. This is the Emacs Way. Your habit of using *scratch* and expecting Emacs to warn you about not saving your work is a bad habit - nothing more. – Drew Nov 2 '17 at 17:31
  • Got it. Just now I tried it. Good idea and it would work for me. Edited you answer as there was a missing "C". To make up for the edit requirements - i had to change -IMHO to in my opinion – Prasanna Nov 2 '17 at 17:47

You could use the (bundled) savehist package to save and restore the scratch contents:

(use-package savehist
  (defun my-savehist-scratch ()
    (with-current-buffer (startup--get-buffer-create-scratch)
      (setq initial-scratch-message (buffer-string))))
  (add-hook 'savehist-save-hook #'my-savehist-scratch)
  (add-to-list 'savehist-additional-variables 'initial-scratch-message))

then you don't have to worry about it being killed, it'll be saved in ~/.emacs.d/history anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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