Evaluating Lisp Expressions

There are several ways to explicitly evaluate Lisp expressions.

  1. You can put the Lisp expressions in a file, then load the file. Suppose you place the expressions in a file named rebind.el. (Emacs Lisp filenames customarily end in .el.) You could then type M-x load-file RET rebind.el RET to cause Emacs to evaluate the contents of that file.

    If you placed those expressions into your .emacs file, you could load .emacs in the same way. But after you've been using Emacs for a while, your .emacs tends to grow, and if it's very large, loading it could be slow. In that case, you wouldn't want to load the entire file just to get the effect of a couple of small changes. That brings us to our next option.

  2. You can use the command eval-last-sexp, which is bound to[4] C-x C-e. (Sexp[5] is an abbreviation for S-expression, which in turn is short for symbolic expression, which is another name for "Lisp expression.") This command evaluates the Lisp expression to the left of the cursor. So what you'd do is position the cursor at the end of the first line:

    (global-set-key "\M-?" 'help-command)▌
    (global-set-key "\C-h" 'delete-backward-char)

    and press C-x C-e; then move to the end of the second line:

    (global-set-key "\M-?" 'help-command)
    (global-set-key "\C-h" 'delete-backward-char)▌

    and press C-x C-e again. Note that each time you press C-x C-e, the result of evaluating global-set-key—the special symbol nil (which we'll see again later)—is shown in Emacs's message area at the bottom of the screen.

  3. You can use the command eval-expression, which is bound to M-:[6]. This command prompts you in the minibuffer (the area at the bottom of the screen) for a Lisp expression, then evaluates it and shows the result.

    eval-expression is one of a few commands considered by the makers of Emacs to be dangerous for novice users to try. Hogwash, I say; nevertheless, the command is initially disabled, so when you try to use it, Emacs tells you "You have typed M-:, invoking disabled command eval-expression." Then it displays a description of eval-expression and prompts you as follows:

    You can now type
    Space to try the command just this once,
              but leave it disabled,
    Y to try it and enable it (no questions if you use it again),
    N to do nothing (command remains disabled).

    If you choose Y, Emacs adds the following Lisp expression to your .emacs:

    (put 'eval-expression 'disabled nil)

    (The put function relates to property lists, which we'll see in the section on Symbol Properties in Chapter 3.) My advice is to put this in your .emacs yourself before you ever get this message from Emacs, so you'll never have to bother with the "disabled command" warning. As soon as you put the put function in .emacs, of course, it's a good idea to evaluate it so it takes effect in the present session, using eval-last-sexp as described above.

  4. You can use the *scratch* buffer. This buffer is automatically created when Emacs starts. The buffer is in Lisp Interaction mode. In this mode, pressing C-j invokes eval-print-last-sexp, which is like eval-last-sexp except that the result of the evaluation is inserted into the buffer at the location of the cursor. Another feature of Lisp Interaction mode is its ability to complete a partially typed Lisp symbol when you press M-TAB (which invokes lisp-complete-symbol). Lisp Interaction mode is particularly useful for testing and debugging Lisp expressions that are too long to type into the minibuffer, or that yield complicated data structures when evaluated.

Whichever method you use, evaluating the global-set-key expression results in the new bindings being used.



[4] Technically, one should only speak of keysequences being bound to commands, not commands being bound to keysequences. (To say that a keysequence is "bound" to a command correctly signifies that there's just one thing it can do—invoke that command. To say that a command is "bound" to a keysequence would mean that only one keysequence can invoke the command, but that's never the case.) But this misuse of "bound to" is as common as the correct use, and rarely causes confusion.

[5] Pronounced "sex pee." Unfortunately.

[6] This keybinding is new in Emacs 19.29. In prior versions, eval-expression was bound to M-ESC by default.

Get Writing GNU Emacs Extensions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.