Custom Scratch Buffers

2021, August

It can be useful to create temporary scratch buffers in specific languages. A number of packages exist to help you do this. I, however, prefer writing code over installing packages, so I came up with the following:

(defvar scratch-org-initial-message "# Org Mode Scratch Buffer\n" "Message to be inserted in org scratch buffer.") (setq scratch-org-initial-message (concat "# Scratch Buffer for Org Mode\n" "# " (format-time-string "[%Y-%m-%d %a %H:%M]\n\n"))) (defvar scratch-org-buffer "*scratch-org*" "Name of org-mode scratch buffer.") (defun scratch-buffer-org () "Create a *scratch* buffer in Org Mode and switch to it." (interactive) (let ((buf scratch-org-buffer)) (if (get-buffer buf) (switch-to-buffer buf) (progn (switch-to-buffer buf) (org-mode) (with-current-buffer buf (setq-local buffer-confirm-kill t) (setq-local buffer-offer-save t) (insert scratch-org-initial-message) (not-modified))))))

The above function sets the variable buffer-confirm-kill which I wrote a separate note about.