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.

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.
(your cursor needs to be near top level.)
Ctrl+Alt+x
(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 to Increase Productivity

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

I never liked programing in a shell interface, so i never used ielm. I prefer a notebook-style interface. With notebook interface, as in emacs buffer, you can use the full power of editor to edit any part of your code, and evaluate any part you want dynamically, with many different ways to evaluate them.

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