Emacs: Evaluate Elisp Code

By Xah Lee. Date: . Last updated: .

To evaluate a single lisp expression, move cursor to the right of the last closing parenthesis, and Alt+x eval-last-sexpCtrl+x Ctrl+e】.

To evaluate all elisp code in a text selection, Alt+x eval-region.

emacs lisp eval 2021-11-01 wgWV
eval emacs lisp code

Here's ways to eval elisp code, roughly in order of usefulness:

Alt+x eval-last-sexpCtrl+x Ctrl+e
Eval lisp expression to the left of cursor.
Alt+x eval-region
Eval text selection.
Alt+x eval-buffer
Eval whole buffer.
Alt+x load-file
Eval a file. It prompts.
Alt+x eval-defun
Eval function definition block the cursor is in. (the elisp code must be well-indended, otherwise emacs may have problem finding function.) [see Emacs: narrow-to-defun, eval-defun, bug]
Alt+x eval-expression
Prompts you to type code.

Emacs Lisp Command Line Interface (REPL)

Alt+x ielm to start emacs interactive emacs lisp shell.

emacs elisp REPL ielm 2017 01 04
emacs lisp interactive command line interface (REPL) ielm

I recommend working in a buffer instead of REPL.

Working in a buffer (sometimes called notebook inteface) allows you to use full editing power, and eval any expression, in any order, anytime, anywhere. [see Interactive Shell/REPL vs Notebook Interface]

Practical Emacs Lisp


Basic Functions

Writing Command

Writing Script