GNU Emacs — Foundations of Emacs Lisp

Foundations of Emacs Lisp

Emacs Lisp is the language that powers Emacs. Learning it transforms Emacs from "that text editor with strange shortcuts" into a programmable environment you can shape to fit your own workflow.

Beginner Friendly Practical Emacs-Themed

Contents

What Is Emacs Lisp?

Emacs Lisp (often shortened to ELisp) is the programming language used to customize and extend Emacs.

Unlike most applications that expose limited configuration files, Emacs exposes nearly everything through Lisp.

Key idea: Emacs isn't just configurable. It's reprogrammable.

Why Does Emacs Use Lisp?

This allows you to experiment, iterate, and build tools directly inside your editor.

Thinking in Lisp

Lisp uses prefix notation:

(+ 2 3)

Instead of:

2 + 3

Nested expressions become very natural:

(+ (* 2 3) 7)
Read this as: "Take 2 times 3, then add 7."

Evaluating Code

Evaluation is central to Emacs Lisp.

Shortcut Purpose
M-: Evaluate an expression from the minibuffer.
C-x C-e Evaluate the expression before point.
M-x eval-buffer Evaluate an entire buffer.
(message "Hello from Emacs Lisp!")

Basic Data Types

Numbers

42
3.14

Strings

"Hello, Emacs"

Symbols

foo

Booleans

t
nil
In Emacs Lisp, nil means both "false" and the empty list.

Variables

Variables store values.

(setq name "David")
(setq age 30)

Reading them is simple:

name
age
(message "Hello %s" name)

Functions

Functions are created with defun.

(defun say-hello ()
  (message "Hello!"))

Functions can accept parameters:

(defun greet (person)
  (message "Hello %s" person))
(greet "Alice")

The Magic of Interactive Commands

This is where Emacs Lisp becomes special.

Add interactive and your function becomes available via M-x.

(defun my-command ()
  (interactive)
  (message "I can be called with M-x"))
Interactive functions turn your ideas into editor commands.

Lists and Quoting

Lists are everywhere in Lisp.

'(1 2 3)
Function Description
car First element.
cdr Rest of list.
cons Add element to front.
length Count elements.
(car '(1 2 3))    ;; 1
(cdr '(1 2 3))    ;; (2 3)

Practical Examples

Insert Today's Date

(defun insert-date ()
  (interactive)
  (insert (format-time-string "%Y-%m-%d")))

Open Your Init File

(defun open-init-file ()
  (interactive)
  (find-file user-init-file))

Simple Greeting

(defun greet-user ()
  (interactive)
  (message "Welcome to Emacs!"))

Common Beginner Mistakes

Forgetting Parentheses
Most ELisp errors come from unmatched parentheses.
Confusing Symbols and Strings
foo is not the same as "foo".
Forgetting interactive
Without it, your function cannot be called through M-x.
Ignoring Evaluation
Remember to evaluate your changes.

Emacs Lisp Cheat Sheet

Concept Example
Variable (setq x 10)
Function (defun hello () ...)
Interactive Command (interactive)
List '(1 2 3)
Evaluate Expression C-x C-e
Eval Minibuffer M-:

Once you understand these fundamentals, you already know enough to start customizing Emacs and automating small tasks.

Where to Go Next

  1. Read the built-in Emacs Lisp manual.
  2. Explore your init.el.
  3. Study packages you use daily.
  4. Create your own commands.
  5. Learn about buffers, windows, and hooks.
  6. Eventually explore macros.
The journey from "Emacs user" to "Emacs wizard" starts with a few lines of Lisp and the willingness to experiment.
ELisp Intro All UTF-8 Fundamental (Emacs Lisp)