If I accidentally closed the scratch buffer in Emacs, how do I create a new scratch buffer?


5 Answers 5


Just re-create the buffer by switching to it: C-x b *scratch* RET. Since Emacs 21.2, if you create a buffer called *scratch*, it's automatically put in Lisp Interaction mode.

  • Why do you need a special function? For me, if I kill *scratch* and then switch back to it, it's set to lisp-interaction mode automatically. Sep 23, 2014 at 21:09
  • 1
    @TikhonJelvis Ah, indeed. I have this function because it didn't do that in older versions (the change is from 21.2, which is not recent in absolute terms but more recent than the bulk of my .emacs). Sep 23, 2014 at 21:13
  • Yeah. Emacs 21.2 was released around the time I first used a computer at all. That's a pretty large absolute difference ;). Sep 23, 2014 at 21:16

It's arguably better to re-create the *scratch* buffer automatically whenever it gets deleted, and avoid having to retype its name.

(defun prepare-scratch-for-kill ()
    (set-buffer (get-buffer-create "*scratch*"))
    (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer t)))

(defun kill-scratch-buffer ()
  (let (kill-buffer-query-functions)
    (kill-buffer (current-buffer)))
  ;; no way, *scratch* shall live
  ;; Since we "killed" it, don't let caller try too


GNU Emacs default bindings:


or, more verbosely

  1. M-xswitch-to-buffer
  2. Typing: *scratch*
  3. And then hitting the RET key.

The *scratch* buffer is the buffer selected upon startup, and has the major mode Lisp Interaction. Note: the mode for the *scratch* buffer is controlled by the variable initial-major-mode.

In general you can create as many "scratch" buffers as you want, and name them however you choose.


switches to buffer NAME, creating it if it doesn't exist. A new buffer is not associated with a file on disk until you use C-xC-w (or M-xwrite-file) to choose a file where it should be saved.

M-xtext-mode changes the current buffer's major mode to Text mode.

To find all the modes available (that is, without requiring any new packages), you can get a list by typing:

  1. M-xapropos-command
  2. Then type: -mode$
  3. And then press RET

I like to use scratch buffers for throw-away stuff. Having only one is somewhat limiting though, therefore I have scratch as an interactive command for opening a new scratch buffer (no matter how many exist already):

(defun scratch ()
  "create a new scratch buffer to work in. (could be *scratch* - *scratchX*)"
  (let ((n 0)
    (while (progn
             (setq bufname (concat "*scratch"
                                   (if (= n 0) "" (int-to-string n))
             (setq n (1+ n))
             (get-buffer bufname)))
  (switch-to-buffer (get-buffer-create bufname))
  (if (= n 1) initial-major-mode))) ; 1, because n was incremented

adopted from: http://everything2.com/index.pl?node_id=1038451

  • I have been using this for six weeks. It's great, thanks! But... Last night, my windows computer restarted itself. I lost data from my scratch buffers, because M-x recover-session doesn't recover those. Has anyone modified this to use something like make-temp-file?
    – daveloyall
    Apr 12, 2018 at 15:33

May I suggest to create an empty buffer? The interesting thing being that, you create the buffer first, and you save it after, if you really want to.

(defun my-empty-buffer () 
  "Open a new empty buffer."
  (let ((buf (generate-new-buffer "untitled")))
    (switch-to-buffer buf)
    (funcall (and initial-major-mode))
    (setq buffer-offer-save t)))

You will have to set initial-major-mode to lisp in your init file and you should bind the my-empty-buffer to a shortcut like C-n.

Courtesy of ergoemacs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.