If I ever close the *scratch*
buffer it is always an accident.
I have persistent-scratch
so it's as easy as a persistent-scratch-reload
but it'd be nice if the scratch couldn't be killed. How can I do that?
If I ever close the *scratch*
buffer it is always an accident.
I have persistent-scratch
so it's as easy as a persistent-scratch-reload
but it'd be nice if the scratch couldn't be killed. How can I do that?
You can (ab-)use kill-buffer-query-functions
for this purpose:
(add-hook 'kill-buffer-query-functions #'my/dont-kill-scratch)
(defun my/dont-kill-scratch ()
(if (not (equal (buffer-name) "*scratch*"))
t
(message "Not allowed to kill %s, burying instead" (buffer-name))
(bury-buffer)
nil))
In my old Emacs configuration I used this to protect a bunch of important buffer like *Messages*
.
Note that my function uses bury-buffer
to achieve the effect of killing a buffer—doing the buffer away—without actually killing the buffer. Emacs will switch to a different buffer just as if you had killed scratch, but keep scratch alive and just put it at the end of the buffer list.
Or, simply
(add-hook 'kill-buffer-query-functions
(lambda() (not (equal (buffer-name) "*scratch*"))))
A new feature has been introduced for persistent scratch called "remember"
From https://www.masteringemacs.org/article/whats-new-in-emacs-24-4
The new command ``remember-notes`` creates a buffer which is saved
on ``kill-emacs``.
You may think of it as a \*scratch\* buffer whose content is preserved.
In fact, it was designed as a replacement for \*scratch\* buffer and can
be used that way by setting ``initial-buffer-choice`` to
``remember-notes`` and ``remember-notes-buffer-name`` to “\*scratch\*”.
Without the second change, \*scratch\* buffer will still be there for
notes that do not need to be preserved.
ok, this whole discussion has prompted me to return to an approach I've tried to setup but @Drew has rekindled an interest in.
Create a file like this in ~/.emacs.d/scratch.el
;;; scratch.el --- Emacs Lisp Scratch -*- lexical-binding: t -*-
;; Local Variables:
;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
;; byte-compile-warnings: (not free-vars unresolved)
;; End:
;;; scratch.el ends here
thanks to https://emacs.stackexchange.com/a/19507/5142 for the Local Variables
.
And then add the following to ~/.emacs.d/init.el
as per @lunaryorn's answer:
;; *scratch* is immortal
(add-hook 'kill-buffer-query-functions
(lambda () (not (member (buffer-name) '("*scratch*" "scratch.el")))))
(find-file (expand-file-name "scratch.el" user-emacs-directory))