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?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityIf 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.
remember-notes-bury-on-kill
(which is t
by default). It does what the name suggests – which seems quite relevant for the original question.
– Harald Hanche-Olsen
Jan 28 '16 at 8:59
notes
(which opens on startup) and it doesn't hijack *scratch*
(I tried with and without escapes to the asterix)
– fommil
Jan 28 '16 at 10:02
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))
*scratch*
? If not - if you use it essentially for Emacs-Lisp code and you could just as easily use Emacs-Lisp mode, then consider not using*scratch*
for your Lisp fiddling. Just useC-x f
with a file buffer that you can save or toss at will. – Drew Jan 5 '16 at 0:06*scratch*
—Emacs will create it if it doesn't exist—and entering Lisp Interaction Mode. – user227 Jan 5 '16 at 5:37~/.emacs.d/scratch.el
. But It just doesn't feel right, I don't know why. – fommil Jan 12 '16 at 23:04tossa.el
,tossb
,..., anywhere. It's trivial to do%m ^toss
to mark all such files in a Dired buffer, thenD
to delete them all. – Drew Jan 13 '16 at 1:18scratch.el
approach another try. Maybe if I can clean up flycheck it'll be nice emacs.stackexchange.com/questions/19506 – fommil Jan 13 '16 at 12:07