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

  • Please don't make questions only for the purpose of answering your own questions, especially if they are easy to answer ones. – Edgar Aroutiounian Sep 23 '14 at 20:54
  • 9
    @EdgarAroutiounian It's ok to ask and answer your own question! However, asking easy questions should be avoided during the private beta: the private beta sets the tone for the site. – Gilles 'SO- stop being evil' Sep 23 '14 at 20:58
  • 1
    @Gilles Of course answering your own questions is fine, but its rather suspect when you make 3 questions back to back, each with an answer immediately after asking the question. – Edgar Aroutiounian Sep 23 '14 at 20:59
  • 2
    @EdgarAroutiounian, Jon Ericson wants us to answer the simple A-Z questions.Welcome – Luke Shimkus Sep 23 '14 at 21:00
  • 2
    I think it's fair enough - for a new-ish user it's often not obvious that there's nothing particularly special about the scratch buffer, and @Sigma's answer is quite neat. – Mark Aufflick Sep 24 '14 at 8:00

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.

| improve this answer | |
  • 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. – Tikhon Jelvis Sep 23 '14 at 21:09
  • @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). – Gilles 'SO- stop being evil' Sep 23 '14 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 ;). – Tikhon Jelvis Sep 23 '14 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

| improve this answer | |

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
| improve this answer | |

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

| improve this answer | |
  • 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 '18 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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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