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.
‘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.
*.el’), you can use
‘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’. 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.
‘describe-variable’). This prints the variable’s doc string and value.
‘M-x set-variable’. This can be quicker than evaluating an assignment sexp: `
M-: (setq my-variable some-value)’.
‘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)
‘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.
In addition, you can use library pp+.el to enhance pretty-print evaluation by redefining
‘pp-eval-expression’ as follows:
*Pp Eval Output*’.
‘pp-read-expression-map’. You can use:
‘C-M-q’to indent a sexp
‘C-M-x’to eval a sexp
‘M-TAB’to complete a Lisp symbol
‘pp-eval-expression-print-level’, new options that control the print length and print level. Respects standard option