Emacs: How to Evaluate Emacs Lisp Code

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page shows you how to evaluate emacs lisp code.

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

To evaluate all elisp code in a text selection, call eval-region.

eval emacs lisp basics 2015-09-17
eval emacs lisp basics.

Here's a list of other ways, roughly in order of usefulness:

Command NameActing AreaKey
eval-last-sexpthe complete lisp expression to the left of cursorCtrl+x Ctrl+e
eval-defunthe function definition block (defun) the cursor is in.
(the elisp code must be well-indended, otherwise emacs may have problem finding function.)
(only when in lisp modes)
eval-regiontext selection
eval-bufferwhole file in current window
load-fileprompts you for a file nameL】 in dired.
eval-expressionprompts you to type codeAlt+:】 or 【Esc :

If a command does not have key, you can give it a key. 〔➤ Emacs: How to Define Keys〕 Or you can give them a alias for convenience, ⁖ “eb” for eval-buffer. 〔➤ Emacs: Use Alias for Fast M-x

Interactive Command Line Interfaces

Emacs has a interactive emacs lisp shell. Call ielm to start.

emacs lisp interactive command line interface REPL ielm
emacs lisp interactive command line interface (REPL) ielm

Note: coding lisp in a command line interface may not be the most convenient. I recommend you work in a buffer instead. That way, you can use the full power of emacs to edit any part of your code, and evaluate any part you want.

Like it? Buy Xah Emacs Tutorial.
blog comments powered by Disqus