29.10 Lisp Interaction Buffers

When Emacs starts up, it contains a buffer named *scratch*, which is provided for evaluating Emacs Lisp expressions interactively. Its major mode is Lisp Interaction mode. You can also enable Lisp Interaction mode by typing M-x lisp-interaction-mode.

If you kill the *scratch* buffer, you can recreate it with the M-x scratch-buffer command.

In the *scratch* buffer, and other Lisp Interaction mode buffers, C-j (eval-print-last-sexp) evaluates the Lisp expression before point, and inserts the value at point. Thus, as you type expressions into the buffer followed by C-j after each expression, the buffer records a transcript of the evaluated expressions and their values. All other commands in Lisp Interaction mode are the same as in Emacs Lisp mode.

At startup, the *scratch* buffer contains a short message, in the form of a Lisp comment, that explains what it is for. This message is controlled by the variable initial-scratch-message, which should be either a documentation string, or nil (which means to suppress the message).

An alternative way of evaluating Emacs Lisp expressions interactively is to use Inferior Emacs Lisp mode, which provides an interface rather like Shell mode (see Shell Mode) for evaluating Emacs Lisp expressions. Type M-x ielm to create an *ielm* buffer which uses this mode. For more information, see that command’s documentation.

By default, the *scratch* buffer is not associated with any file. Therefore, any text in the buffer is lost when you quit Emacs. If you prefer, you can use Remember mode’s notes buffer feature to make Emacs save and reload the contents of *scratch*. Customize the variable initial-buffer-choice to the symbol remember-notes and the variable remember-notes-buffer-name to the string "*scratch*" to achieve this. See also See Quick Start in the Remember mode manual.