tags:

views:

3089

answers:

7

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

+7  A: 

C-x b *scratch* RET y RET with iswitchb-mode enabled.

Just C-x b *scratch* RET otherwise.

Steven Huwig
With default bindings, the 'y RET' is not needed and just inserts a 'y' and a newline into the newly created *scratch* buffer.
Trey Jackson
Ooops, maybe that's from iswitchb-mode. Sorry about that. On another topic, try out iswitchb-mode ;)
Steven Huwig
One should not, also, there's nothing special about the name *scratch*. One can use C-x b to create any number of "scratch" buffers, with arbitrary names.
Chris Conway
Replace `y RET` by `y`.
J.F. Sebastian
A: 
C-xC-b and then type *scratch*

to create a new buffer which is in lisp interaction mode also.

stephanea
This does not work with default Emacs bindings.
Trey Jackson
+28  A: 

GNU Emacs default bindings:

C-x b *scratch* RET

or, more verbosely

M-x switch-to-buffer *scratch* RET

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 b NAME RET

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

M-x 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:

M-x apropos-command -mode$ RET
Trey Jackson
Note that there is something special about *scratch*, at least in GNU Emacs 21 and later: switching to a new *scratch* buffer will put it back into Lisp Interaction mode.
Peter S. Housel
+2  A: 

I found this years ago when I first started using emacs; I have no idea where now but it has always had a home in my personal .el files. It does pop up in google searches.

;;; Prevent killing the *scratch* buffer -- source forgotten
;;;----------------------------------------------------------------------
;;; Make the *scratch* buffer behave like "The thing your aunt gave you,
;;; which you don't know what is."
(save-excursion
  (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*"))
  (lisp-interaction-mode)
  (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.
  nil)
;;;----------------------------------------------------------------------
dwj
+5  A: 

I add following in my .emacs:


;; bury *scratch* buffer instead of kill it
(defadvice kill-buffer (around kill-buffer-around-advice activate)
  (let ((buffer-to-kill (ad-get-arg 0)))
    (if (equal buffer-to-kill "*scratch*")
        (bury-buffer)
      ad-do-it)))

If I don't want to see scratch buffer I press C-x C-k , but it doesn't kill it, just place in the end of buffer list, so then I need it next time I don't have to create new one.

+1  A: 

This is what I use - I have this bound to a convenient keystroke. It sends you to the *scratch* buffer, regardless of whether or not it already exists, and sets it to be in lisp-interaction-mode

(defun eme-goto-scratch () 
  "this sends you to the scratch buffer"
  (interactive)
  (let ((eme-scratch-buffer (get-buffer-create "*scratch*")))
    (switch-to-buffer eme-scratch-buffer)
    (lisp-interaction-mode)))
Edric
+2  A: 

I used to use dwj's solution, and I was quite happy about it, until the day I realized that it failed when you actually rename the scratch buffer (for example by saving it).

Then I adopted this, which works well for me :

  (run-with-idle-timer 1 t
    '(lambda () (get-buffer-create "*scratch*")))
Gyom