The VikiD Truth

Knowledge base / Computing / Lisp family

Computing | computingFreedom | Build | Picolisp | Picolisp Machine | Pil Sources | Linux | BASH | C-Programmming | Operating Systems | Javascipt | Emacs | vim | Pharo Smalltalk | Computer Security | Python | The Compiler | Identifiers | Literals | storage_linkage | Networking

Emacs Lisp Notes

Some Functions to explore:

    (switch-to-buffer-other-window "buffer-name")
    (erase-buffer)
    (search-forward)
    (re-search-forward)
    (replace-match "Some-text")
    (other-window 1)
    (goto-char (point-max))
    (add-text-properties)
    (match-beginning)
    (match-end)
    (other-window)

The plain idea

Emacs Lisp (Elisp) is the language Emacs is written in and the language you speak to it in. Every key you press runs a Lisp function. C-f runs forward-char; C-x C-s runs save-buffer. Elisp is a Lisp: code is written as lists in parentheses, the first item is the function, and the rest are its arguments. (+ 1 2) is 3. (insert "hello") puts “hello” in the current buffer.

What makes Elisp unusual is that it is a language built around editing. Its basic objects include buffers (the text you are working on), points (cursor positions), markers, windows and frames. The functions listed above, erase-buffer, search-forward, replace-match, goto-char, are the verbs of that world.

Why it matters

Without Elisp, Emacs is a capable editor. With it, Emacs is whatever you need. Small bits of Elisp remove the repetitive chores from your day: renaming things across a file, inserting templates, tidying a log, opening the three files you always open together. And because the whole editor is written in the same language, you can read the source of any command (C-h f, then follow the link) and learn by example from the people who built it.

It has also improved a lot. Lexical binding, switched on by the cookie ;;; -*- lexical-binding: t -*- at the top of a file (Emacs 30 warns when a file lacks it), makes closures behave the way they do in other modern languages. Native compilation turns Elisp into machine code for speed, and cl-lib supplies Common Lisp conveniences such as cl-loop.

An everyday analogy: talking to a very literal assistant

Imagine an assistant sitting at your desk who knows exactly where your cursor is and can follow simple instructions: go to the end, search for this word, replace it with that, switch to the other window. Elisp is how you write those instructions down so the assistant can repeat them forever without you.

A worked example: clean a buffer in one keystroke

Suppose you often paste text with trailing spaces and runs of blank lines. Here is a command that fixes both, using several of the functions from my list:

(defun vikid/tidy-buffer ()
  "Remove trailing spaces and collapse runs of blank lines."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "[ \t]+$" nil t)
      (replace-match ""))
    (goto-char (point-min))
    (while (re-search-forward "\n\\{3,\\}" nil t)
      (replace-match "\n\n"))))

(global-set-key (kbd "C-c t") #'vikid/tidy-buffer)

Read it line by line:

  1. defun defines a function; the string is its documentation, shown by C-h f.
  2. (interactive) makes it a command you can run with M-x or a key.
  3. save-excursion remembers where the cursor was and puts it back afterwards.
  4. goto-char (point-min) jumps to the start of the buffer.
  5. re-search-forward finds the next match of a regular expression; nil t means “no limit, and don’t complain if nothing is found”.
  6. replace-match swaps the matched text for the new text.

Put the cursor after the last parenthesis and press C-x C-e to evaluate it. It is now part of your editor.

Common misconceptions

How it connects

The truth revealed: you start writing Elisp to fix one small annoyance, and a few years later the editor is a portrait of how you think.

Last edited · 3 revisions

Tags: #Computing