If I accidentally closed the scratch buffer in Emacs, how do I create a new scratch buffer?
5 Answers
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.

- 22,318
- 4
- 56
- 92
GNU Emacs default bindings:
C-xb*scratch*
or, more verbosely
- M-x
switch-to-buffer
- Typing:
*scratch*
- 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.
C-x bNAME
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:
- M-x
apropos-command
- Then type:
-mode$
- And then press RET

- 7,194
- 10
- 42
- 63
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 ()
(save-excursion
(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
(prepare-scratch-for-kill)
;; Since we "killed" it, don't let caller try too
nil)
(prepare-scratch-for-kill)

- 4,520
- 22
- 27
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*)"
(interactive)
(let ((n 0)
bufname)
(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

- 1,962
- 20
- 26
-
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 likemake-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."
(interactive)
(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

- 6,521
- 2
- 23
- 35
*scratch*
and then switch back to it, it's set to lisp-interaction mode automatically. – Tikhon Jelvis Sep 23 '14 at 21:09.emacs
). – Gilles 'SO- stop being evil' Sep 23 '14 at 21:13