EvaluatingExpressions

This page is about evaluating EmacsLisp S-expressions, or sexps. A sexp is any readable Lisp expression – code enclosed in parentheses, a string enclosed in double quotes, a variable name, a quoted expression, a numeral, and so on.

See also the sections “How to Evaluate” in the EmacsLispIntro and “Evaluating Emacs Lisp Expressions” in the EmacsManual.

C-x C-e
When looking at EmacsLisp code, put the cursor after a sexp, and hit ‘C-x C-e’ (command ‘eval-last-sexp’). This prints the value of the sexp in the echo area. If you need to look at the result again later, you can find it in buffer ‘*Messages*’ (for a while).
You can insert the result of an evaluation into the current buffer at point by using a prefix argument: ‘C-u C-x C-e’. This can be useful if you are writing some code that uses the result.
C-M-x
In EmacsLispMode (e.g., when in a file buffer `*.el’), you can use ‘C-M-x’ (command ‘eval-defun’) which evaluates the defun at point. If the cursor is inside or immediately after a variable declaration, then this function, unlike ‘C-x C-e’, resets the variable to the value contained in the declaration.
M-x eval-region
You can evaluate the EmacsLisp code in the region using ‘M-x eval-region’. To evaluate all of the code in the current buffer, mark it using ‘C-x h’, then do ‘M-x eval-region’. For information about loading EmacsLisp library files, see InstallingPackages.
C-h v
If you just want to check the current value of a variable, the easiest way is to use ‘C-h v’ (command ‘describe-variable’). This prints the variable’s doc string and value.
M-x set-variable
You can set user options using ‘M-x set-variable’. This can be quicker than evaluating an assignment sexp: `M-: (setq my-variable some-value)’.
M-:
You can evaluate any sexp using ‘M-:’ (command ‘eval-expression’, by default).
This prompts you to type a sexp in the minibuffer. The sexp is evaluated and the result is shown in the echo area (or inserted into the current buffer if you use a prefix argument).
This is especially useful when you are coding and testing. It lets you quickly invoke non-interactive functions (non-commands). It saves you from typing them into a buffer and using ‘C-x C-e’ to evaluate them.

To make ‘M-:’ pretty-print:

      (global-set-key [remap eval-expression] 'pp-eval-expression)

or for Emacs before 22

      (substitute-key-definition 'eval-expression 'pp-eval-expression global-map)

To make ‘C-x C-e’ pretty-print:

      (global-set-key [remap eval-last-sexp] 'pp-eval-last-sexp)

or for Emacs before 22

      (substitute-key-definition 'eval-last-sexp 'pp-eval-last-sexp global-map)

These replace all ‘global-map’ bindings for ‘eval-’* by the corresponding ‘pp-eval-’*. This means that the result of a sexp evaluation is pretty-printed in a separate buffer, `*Pp Eval Output*’. You can edit the definition there, or copy it to other EmacsLisp code.

Some people like to use InferiorEmacsLispMode‘M-x ielm’. This creates a buffer much like a shell buffer. Whatever Lisp expressions you type are evaluated and the result is printed into the buffer.

Similarly, you can use buffer `*scratch*’ much as you would a buffer in EmacsLispMode. However, by default the mode of `*scratch*’ is LispInteractionMode, which is slightly different from EmacsLispMode. In particular, ‘C-j’ acts quite differently.

If you want to evaluate a region and insert the result of each top-level form as a comment (similar to code snippets in the EmacsLispManual), use le-eval-and-insert-results.el

Enhance Pretty-Printing Evaluation, `pp-eval-expression'

In addition, you can use library pp+.el to enhance pretty-print evaluation by redefining ‘pp-eval-expression’ as follows:

Note: If you use Icicles, then the same behavior is available in Icicle mode without library pp+.el. – DrewAdams


CategoryCode