If I accidentally closed the scratch buffer in Emacs, how do I create a new scratch buffer?
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.
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)
GNU Emacs default bindings:
or, more verbosely
- And then hitting the RET key.
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-x
write-file) to choose a file where it should be saved.
text-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:
- Then type:
- 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*)" (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
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
Courtesy of ergoemacs