Evaluating your Emacs Lisp code when editing your GNU Emacs configuration or working on some package is best way to see what are you doing.


When you start GNU Emacs, one of the open buffers is *scratch* buffer. When you open it you'll see following message, that explain it:

;; This buffer is for notes you don't want to save, and for Lisp evaluation.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer.

You can disable this message by setting:

(setq initial-scratch-message nil)

You can type some Emacs lisp code in *scratch*, for example (+ 1 2) and place cursor after ')'. Then press C-j and '4' will appear in *scratch* buffer. If you want to display result in minibuffer you can press C-x C-e (eval-last-sexp).


You can evaluate Emacs lisp code in minibuffer. Just press M-: and you'll get prompt to enter elisp code in minibuffer. Result is displayed in echo area and in *Messages* buffer.

Inferior Emacs Lisp mode

Both *scratch* and minibuffer have their limitations and they are useful only for small code chunks. They can not replace real REPL like a Inferior Emacs Lisp mode.

You can start Inferior Emacs Lisp mode it with M-x ielm. It behaves like a any REPL you are familiar with. Just type some Emacs lisp code and press enter. Great for exploring Emacs lisp!

Evaluating Emacs lisp files

When you work on Emacs lisp files (.el), you can use C-x C-e same as in *scratch* buffer. Limitations are the same, you can not for example change variables made with defvar, etc…

One of the most handy evaluation command is M-x eval-defun or C-M-x. It evaluates any Emacs lisp form where point (cursor) is located or form before point. Despite defun in name, you can evaluate any Emacs lisp form: defvar, defface, etc… This evaluation displays the result value in echo area. Just be aware that running this on defvar will reset that variable to value of that form.

You can evaluate only part of the buffer by marking specific part (same way you do copying in GNU Emacs) and by typing M-x eval-region. I can't remember when I used this one.

Other handy evaluation command is eval-buffer. You can run it with M-x eval-buffer. As the name says, it evaluate whole buffer. I bind this to C-c C-b in my configuration files:

(global-set-key (kbd "C-c C-b") 'eval-buffer)

You can find value of an variable by typing C-h v. You will then get prompt to type variable name. If your point (cursor) is placed on an variable in Emacs lisp buffer, that variable will be default, so you can just press Enter. You then get *Help* popup window with variable value and description.

Sometimes you'll need to set variable value in GNU Emacs. You can do it in minibuffer, as described above, with M-: (setq your-variable t). However, if you see that variable in buffer, place point on it and change it using M-x set-variable. It's just a bit faster.

That's is. Whole Emacs lisp evaluation is basically a few commands. Easy to remember and use.

Share on: