Table of Contents

Emacs scratch buffers have place oriented design.

A simple alternative: make files.

Musings about persistent scratch technology.


If you ever typed something in a buffer called *scratch* you will inevitably have found yourself in this situation:

  1. Just typed some awesome code.
  2. Kill scratch buffer accidentally or unthinkingly.
  3. That awesome text is gone forever… and it hurts.

I did try persistent-scratch, in its simplest form, it persists your scratch buffer across sessions. You still end up having one place for your "current" scratch. It just did not click with me.

The whole idea of a scratch buffer might be obsolete (Xah Lee 2008).

What I want:

  1. Convenient way of writing some code interactively.
  2. Persist it.

scratching in space

Our disk space is vast, and emacs is great at creating files.

We can be like Sherlock Holmes and never throw our notes away.

Horde those scratches. Put them in git. Now you can grep through them, see what you were scratching years in the past.

I have a dir called ~/scratches with all my scratches. This is a bit similar to denote, which I use for note-taking.

here is my current hack for this:

(defvar mememacs/scratch-dir (expand-file-name "~/scratch"))
(defun mememacs/latest-scratch (suffix)
  (unless (file-exists-p mememacs/scratch-dir)
    (make-directory mememacs/scratch-dir))
          (lambda (it)
            (and (string-suffix-p suffix it)
                 (not (string-match-p "#" it))))
    (expand-file-name f mememacs/scratch-dir)))

(defun mememacs/new-scratch-name (suffix)
  (unless (file-exists-p
   (format "%s.%s" (make-temp-name "scratch-") suffix)

(defun mm/scratch
    (&optional create-new suffix)
  "Visit the latest scratch file with `suffix` (a file extension).
With prefix arg make a new file."
   (list current-prefix-arg
         (completing-read "scratch filetype: " '("cljs" "clj"))))
  (let* ((latest (mememacs/latest-scratch suffix))
           (if (or create-new (not latest))
               (mememacs/new-scratch-name suffix)
    (pop-to-buffer-same-window buff)
    (when (eq major-mode 'emacs-lisp-mode)

(defun mm/scratch-el (&optional arg)
  (interactive "P")
  (mm/scratch arg "el"))
leader bs #'mm/scratch-el
leader bS #'mm/scratch

With a nice keybind (leader b s) I go to my current scratch file - the latest modified file in my scratch dir. With prefix-arg, it makes a new file. A second bind will ask me to make a clj or cljs file.

clojure scratch repl?

Whatever. I just do this:

echo '{}' > ~/scratch/bb.edn

Now I can start a cider repl with bb in my scratch dir. If you want to be asked for bb or jvm:

echo '{}' > ~/scratch/deps.edn

Also nbb: A nice way to scratch some cljs + javascript + node: It currently requires some workaround that you can find here.

Further reading

inspiring talks

Date: 2022-10-04 Tue 16:04

Email: Benjamin.Schwerdtner@gmail.com