HomeMathComputingArtsWordsLiteratureMusictwitter facebook webfeed

Emacs: How to Eval Elisp Code, Find Functions, Search Documentation

Advertise Here For Profit

Xah Lee, 2007-12, …, 2010-08-13

This page shows you how to evaluate a emacs lisp expression, and how to find the function you want, and how to lookup function's documentation.

Evaluating Lisp Expressions

To evaluate a single lisp expression, move your cursor to the right of the last closing parenthesis, and type 【Ctrl+x Ctrl+e】 (eval-last-sexp).

To evaluate a text selection, call “eval-region”.

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

Command NameActing AreaShortcut
eval-last-sexpthe complete lisp expression to the left of cursorCtrl+x Ctrl+e
eval-defunthe function definition block (defun) the cursor is in
eval-regiontext selection
eval-bufferwhole file in current window
load-filepromps you for a file name
eval-expressionpromps you to type code

For commands that do not have a shortcut, you can assign them a shortcut, or you can give them a alias. For example, i have the following aliases defined:

(defalias 'eb 'eval-buffer)
(defalias 'er 'eval-region)
(defalias 'ed 'eval-defun)
(defalias 'ee 'eval-expression)
(defalias 'elm 'emacs-lisp-mode)

Also, emacs has a interactive emacs lisp shell: 【Alt+x ielm】.

Documentation Lookup

Inline Doc Lookup

Type 【Ctrl+h f】 (describe-function), then type the function name. If the word the cursor is on is a valid elisp function name, emacs will use that by default.

Type 【Ctrl+h v】 (describe-variable).

If you are using emacs 23, you can use the asterisk “*” as a wildcard when looking up function or variable doc. For example, type 【Ctrl+h f *file Tab】 and emacs will list all functions whose name ends in “file”.

Once the function's inline doc string page comes up, you can jump to the function's location in source code by clicking on underlined file name (or move your cursor there and press Enter).

Searching for Functions

To search command names using a regex, type 【Ctrl+h a】 (apropos-command).

To search both function and command name spaces, call apropos-command with a empty argument, like this: 【Ctrl+u Ctrl+h a】.

(Commands are a subset of functions. Commands are functions that can be called interactively, meanig that you can call them by typing 【Alt+x ‹name›】. A function is a command when its definition contains the (interactive …) clause.)

To search all symbols space (commands, functions, variables), type 【Alt+x apropos】.

Finding a Function's Documentation in Elisp Manual

Use “elisp-index-search” to find a function's documentation in the emacs lisp manual. For emacs manual, use “emacs-index-search”.

If you use elisp-index-search often, you can assign it a alias.

(defalias 'eis 'elisp-index-search)
blog comments powered by Disqus