Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 38 ยท Configuration and Elisp

Emacs Lisp Expressions, Lists and Variables

Learn the core of Emacs Lisp: prefix expressions, quoting, lists and cons cells, strings, symbols, setq, let, and how to evaluate code inside Emacs.

Emacs Lisp (Elisp) is the language Emacs is written in and configured with. Almost every command you press is an Elisp function. You do not need to become a programmer to configure Emacs, but a small amount of Lisp makes your init file far easier to understand and change. This lesson covers the building blocks: expressions, symbols, lists, quoting and variables.

Everything here is real Emacs Lisp. Because Emacs cannot run in the page, the runnable blocks use a tiny JavaScript stand-in that parses and evaluates a small subset of Lisp, so you can see the evaluation rules in action. The stand-in is not Emacs; it only mirrors the rules described.

Everything is an expression

Lisp code is made of S-expressions. A parenthesized list means "call the first element as a function with the remaining elements as arguments":

ELISP
(+ 1 2 3)            ; => 6
(* 2 (+ 3 4))        ; => 14
(concat "foo" "bar") ; => "foobar"
(upcase "emacs")     ; => "EMACS"

The function always comes first (prefix notation), and arguments are evaluated before the call. A number or string evaluates to itself. A bare name such as fill-column is a symbol; evaluating it looks up its variable value.

Try evaluating in Emacs

Emacs lets you evaluate code anywhere:

  • C-x C-e (eval-last-sexp): put the cursor after a closing parenthesis and the result appears in the echo area.
  • C-u C-x C-e inserts the result into the buffer.
  • M-: (eval-expression) asks for an expression in the minibuffer.
  • M-x eval-buffer and M-x eval-region run larger pieces.
  • The *scratch* buffer (in Lisp Interaction mode) is a playground; C-j evaluates the expression before the cursor and prints the result below it.
JS Stand-in: a tiny Lisp evaluator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last line shows the real Emacs error text for an unset variable (Symbol's value as variable is void). Real Emacs prints exactly that wording.

Quoting: code versus data

If a list is evaluated, its first element is called. To treat a list as plain data, quote it with ' (a shorthand for (quote ...)):

ELISP
'(a b c)          ; => (a b c)   -- the list itself
(list 'a 'b 'c)  ; => (a b c)   -- built by calling list
(a b c)           ; error: Symbol's function definition is void: a

Quoting a symbol ('fill-column) gives the symbol rather than its value. You will see quoted symbols everywhere in configuration: (load-theme 'modus-vivendi), (add-hook 'prog-mode-hook ...). In those calls you pass the name of a theme or hook, not its value.

Lists, cons cells and nil

A list is a chain of cons cells, each holding a value (car) and the rest (cdr). The empty list and "false" are the same thing, nil.

ELISP
(car '(1 2 3))        ; => 1
(cdr '(1 2 3))        ; => (2 3)
(cons 0 '(1 2 3))     ; => (0 1 2 3)
(nth 2 '(a b c d))    ; => c
(append '(1 2) '(3))  ; => (1 2 3)
(length '(1 2 3))     ; => 3

Two further data shapes appear constantly in configuration. An association list (alist) maps keys to values: '((tab-width . 4) (fill-column . 80)). A property list (plist) alternates keys and values: '(:height 130 :weight bold). Many variables, such as auto-mode-alist and default-frame-alist, are alists; faces use plists.

Variables: setq and let

setq assigns a global (or buffer-visible) variable:

ELISP
(setq fill-column 72)
(setq name "Ada" langs '("Lisp" "C"))   ; several pairs at once

let creates local bindings that vanish when the body ends:

ELISP
(let ((a 2) (b 3))
  (* a b))   ; => 6, and a and b no longer exist

let* makes bindings sequential, so a later one can use an earlier one. Prefer let for local values and setq only for real settings; accidental global variables are a classic source of bugs.

Strings, numbers and symbols

Strings are written in double quotes. Useful functions: format (like printf, (format "%d items" 3)), concat, substring, split-string, string-join, string-trim, string-prefix-p. A trailing -p in a name marks a predicate that returns t or nil: stringp, listp, numberp, null. A symbol is a name; symbols compare with eq, while strings compare with string= or equal.

JS Stand-in: how nil, t and equality behave

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In Elisp both 0 and "" are true, which surprises people coming from JavaScript or Python. Only nil (which is also the empty list) is false.

Docstrings and comments: a line comment starts with ;. By convention, ;; is used for comments on their own line and ;;; for section headings in a file. Check any function with C-h f (describe-function) and any variable with C-h v; both show a documentation string and a link to the source.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does (+ 1 (* 2 3)) evaluate to?

    The inner (* 2 3) is 6 and (+ 1 6) is 7. Option 2 is wrong because arguments are evaluated first.
  2. 2. Which expression returns the list (a b c) as data, without calling a?

    The quote mark stops evaluation, so the list is returned as is.
  3. 3. Which values are false in Emacs Lisp?

    Only nil is false; 0 and the empty string are true.
  4. 4. What is the difference between setq and let?

    let gives temporary local bindings; setq changes the variable's value for good.

Summary

  • Lisp code is prefix S-expressions: (function arg1 arg2).
  • Quote (') turns code into data; symbols in settings are usually quoted.
  • Lists are chains of cons cells; nil is both false and the empty list.
  • Use setq for settings, let for temporary local values, C-x C-e and M-: to try things.
  • Names ending in -p are predicates; C-h f and C-h v document anything.

Next: Functions, Control Flow and Binding in Elisp