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‘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).‘C-u C-x C-e’. This can be useful if you are writing some code that uses the result.C-M-x*.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‘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‘C-h v’ (command ‘describe-variable’). This prints the variable’s doc string and value.M-x set-variable‘M-x set-variable’. This can be quicker than evaluating an assignment sexp: `M-: (setq my-variable some-value)’.M-:‘M-:’ (command ‘eval-expression’, by default).‘C-x C-e’ to evaluate them.(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
In addition, you can use library pp+.el to enhance pretty-print evaluation by redefining ‘pp-eval-expression’ as follows:
‘emacs-lisp-mode-hook’.*Pp Eval Output*’.‘pp-read-expression-map’. You can use:‘C-M-q’ to indent a sexp‘C-M-x’ to eval a sexp‘TAB’ to Lisp-indent‘M-TAB’ to complete a Lisp symbol‘pp-eval-expression-print-length’ and ‘pp-eval-expression-print-level’, new options that control the print length and print level. Respects standard option ‘eval-expression-debug-on-error’.Note: If you use Icicles, then the same behavior is available in Icicle mode without library pp+.el. – DrewAdams