GNU Emacs Manual - Lisp Eval

Go to the first, previous, next, last section, table of contents.

## Evaluating Emacs-Lisp Expressions

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).

`M-:`
  Read a single Lisp expression in the minibuffer, evaluate it, and print the value in the echo area (`eval-expression`).
`C-x C-e`
  Evaluate the Lisp expression before point, and print the value in the echo area (`eval-last-sexp`).
`C-M-x`
  Evaluate the defun containing or after point, and print the value in the echo area (`eval-defun`).
`M-x eval-region`
  Evaluate all the Lisp expressions in the region.
`M-x eval-current-buffer`
  Evaluate all the Lisp expressions in the buffer.

`M-:` (`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 typed.

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.

`C-M-x` treats `defvar` expressions specially. Normally, evaluating a `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 `defvar` expression. This special feature is convenient for debugging Lisp programs.

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 `defvar` specially.

If `C-M-x`, `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.

The most general command for evaluating Lisp expressions from a buffer is `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-current-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 just 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.

Go to the first, previous, next, last section, table of contents.