The scratch buffer is useful for “notes you don’t want to save, and for Lisp evaluation”.

The scratch buffer is handy but we tend to kill it to get rid of its content.

V. L. Simpson [] just pointed out in, that “GNU Emacs 21 on up will recreate the scratch buffer if you just revisit that buffer name: ‘C-x b’ (or ‘M-x switch-to-buffer’) *scratch*”. – DrewAdams

A very simple function to recreate the scratch buffer:

    (defun create-scratch-buffer nil
       "create a scratch buffer"
       (switch-to-buffer (get-buffer-create "*scratch*"))

Another solution consist in recreating it everytime it is killed. To do this add in your .emacs:

    ;; If the *scratch* buffer is killed, recreate it automatically
    ;; FROM: Morten Welind
      (set-buffer (get-buffer-create "*scratch*"))
      (make-local-variable 'kill-buffer-query-functions)
      (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer))

    (defun kill-scratch-buffer ()
      ;; The next line is just in case someone calls this manually
      (set-buffer (get-buffer-create "*scratch*"))
      ;; Kill the current (*scratch*) buffer
      (remove-hook 'kill-buffer-query-functions 'kill-scratch-buffer)
      (kill-buffer (current-buffer))
      ;; Make a brand new *scratch* buffer
      (set-buffer (get-buffer-create "*scratch*"))
      (make-local-variable 'kill-buffer-query-functions)
      (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer)
      ;; Since we killed it, don't let caller do that.

Note that

  (set-buffer ...))

used above is equivalent to


If you want exactly the same initial scratch buffer you can add

    (insert initial-scratch-message)

in the above functions ;-)

Finally you can modify the initial message with ‘M-x customize-option RET initial-scratch-message RET’

See also ProtectingBuffers.

I also wrote a command to create or switch to scratch buffer. This is also helpful when you want switch to a certain buffer frequently at some time. I bound this command to key C-c b.

(defvar ywb-scratch-buffer "*scratch*")
(defun ywb-create/switch-scratch (arg)
  (interactive "P")
  (when arg
    (setq ywb-scratch-buffer (read-buffer "Set scratch to: " (buffer-name))))
  (let ((buf (get-buffer ywb-scratch-buffer)))
    (if (null buf)
          (or arg
              (setq ywb-scratch-buffer (if (y-or-n-p "The buffer does not exist! Create *scratch*? ")
                                         (read-buffer "Set scratch to: " (buffer-name)))))
          (switch-to-buffer ywb-scratch-buffer)
      (switch-to-buffer ywb-scratch-buffer))))

Ye Wenbin

I like to toggle between scratch and current buffer, so I write this lisp code. If scratch buffer does not exist, emacs will create it then switch to scratch buffer.

(defun switch-to-scratch-and-back ()
    "Toggle between *scratch* buffer and the current buffer.
     If the *scratch* buffer does not exist, create it."
    (let ((scratch-buffer-name (get-buffer-create "*scratch*")))
        (if (equal (current-buffer) scratch-buffer-name)
            (switch-to-buffer (other-buffer))
            (switch-to-buffer scratch-buffer-name (lisp-interaction-mode)))))


Just disallow killing of scratch and delete its content instead:

(defun unkillable-scratch-buffer ()
	(if (equal (buffer-name (current-buffer)) "*scratch*")
	      (delete-region (point-min) (point-max))

(add-hook 'kill-buffer-query-functions 'unkillable-scratch-buffer)