Lisp programs intended to be run in Emacs should be edited in Emacs-Lisp mode; this happens automatically for file names ending in .el. By contrast, Lisp mode itself is used for editing Lisp programs intended for other Lisp systems. To switch to Emacs-Lisp mode explicitly, use the command M-x emacs-lisp-mode.
For testing of Lisp programs to run in Emacs, it is often useful to evaluate part of the program as it is found in the Emacs buffer. For example, after changing the text of a Lisp function definition, evaluating the definition installs the change for future calls to the function. Evaluation of Lisp expressions is also useful in any kind of editing, for invoking noninteractive functions (functions that are not commands).
eval-expression) is the most basic command for evaluating
a Lisp expression interactively. It reads the expression using the
minibuffer, so you can execute any expression on a buffer regardless of
what the buffer contains. When the expression is evaluated, the current
buffer is once again the buffer that was current when M-: was
In Emacs-Lisp mode, the key C-M-x is bound to the command
eval-defun, which parses the defun containing or following point
as a Lisp expression and evaluates it. The value is printed in the echo
area. This command is convenient for installing in the Lisp environment
changes that you have just made in the text of a function definition.
defvar expressions specially. Normally,
defvar expression does nothing if the variable it
defines already has a value. But C-M-x unconditionally resets the
variable to the initial value specified in the
defcustom expressions are treated similarly.
This special feature is convenient for debugging Lisp programs.
Typing C-M-x on a
defface expression reinitializes
the face according to the
The command C-x C-e (
eval-last-sexp) evaluates the Lisp
expression preceding point in the buffer, and displays the value in the
echo area. It is available in all major modes, not just Emacs-Lisp
mode. It does not treat
When the result of an evaluation is an integer, you can type C-x C-e a second time to display the value of the integer result in additional formats (octal, hexadecimal, and character).
If C-x C-e, or M-: is given a numeric argument, it inserts the value into the current buffer at point, rather than displaying it in the echo area. The argument's value does not matter. C-M-x with a numeric argument instruments the function definition for Edebug (see Instrumenting for Edebug).
The most general command for evaluating Lisp expressions from a buffer
eval-region. M-x eval-region parses the text of the
region as one or more Lisp expressions, evaluating them one by one.
M-x eval-buffer is similar but evaluates the entire
buffer. This is a reasonable way to install the contents of a file of
Lisp code that you are ready to test. Later, as you find bugs and
change individual functions, use C-M-x on each function that you
change. This keeps the Lisp world in step with the source file.
The two customizable variables
eval-expression-print-length control the maximum depth and length
of lists to print in the result of the evaluation commands before
whether evaluation errors invoke the debugger when these commands are
used; its default is