Next: , Previous: , Up: Running   [Contents][Index]

22.4 Evaluating Emacs-Lisp Expressions

Lisp programs intended to be run in Emacs should be edited in Emacs-Lisp mode; this will happen automatically for file names ending in .el. By contrast, Lisp mode itself should be used for editing Lisp programs intended for other Lisp systems. Emacs-Lisp mode can be selected with the command M-x emacs-lisp-mode.

For testing of Lisp programs to run in Emacs, it is useful to be able to evaluate part of the program as it is found in the Emacs buffer. For example, if you change the text of a Lisp function definition and then evaluate the definition, Emacs installs the change for future calls to the function. Evaluation of Lisp expressions is also useful in any kind of editing task for invoking non-interactive functions (functions that are not commands).


Read a Lisp expression in the minibuffer, evaluate it, and print the value in the minibuffer (eval-expression).

C-x C-e

Evaluate the Lisp expression before point, and print the value in the minibuffer (eval-last-sexp).


Evaluate the defun containing point or after point, and print the value in the minibuffer (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 evaluation is complete, 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 function eval-defun, which parses the defun containing point 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.

The command C-x C-e (eval-last-sexp) performs a similar job but is available in all major modes, not just Emacs-Lisp mode. It finds the sexp before point, reads it as a Lisp expression, evaluates it, and prints the value in the echo area. It is sometimes useful to type in an expression and then, with point still after it, type C-x C-e.

If C-M-x or C-x C-e are given a numeric argument, they print the value by inserting it into the current buffer at point, rather than in the echo area. The argument 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 it 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. After finding and fixing a bug, use C-M-x on each function that you change, to keep the Lisp world in step with the source file.

Next: , Previous: , Up: Running   [Contents][Index]