Emacs: Navigate Lisp Code as Tree

By Xah Lee. Date: . Last updated: .

Lisp code with its nested parenthesis syntax represents a tree structure.

Emacs has many commands that are very helpful in moving around nested brackets, analogous to navigating a tree.

(For historical reasons, lisp code are called “sexp”, short for Symbolic EXPression.)

The following is lisp source code laid out in a way to show its tree structure. You should try the above commands on it. It is very helpful to understand how sexp corresponds to a tree, and how the commands move the cursor exactly.

  (f x li)
  "Applies (f x ele) recursively to the list li …"
      (li2 li)
      (x2 x)
      (setq ele (pop li2))
      (setq x2 (funcall f x2 ele))

Place your cursor at the beginning of the left bracket. Now, try to move your cursor, by using ONLY 【Ctrl+Alt+arrow】, to the “pop” , then move it to “let”, then “funcall”.

Moving to Previous/Next Sibling that Has Children

Use backward-list and forward-list to jump to prev/next sibling that has children. (i.e. skip siblings that does not have children.)

For example, suppose you have (a (b) ▮ c d (e f)) and your cursor is before “c”. Then:

Emacs Lisp Basics

  1. Elisp Basics
  2. ELisp Text-Processing Overview
  3. Elisp Examples
  4. Evaluate Elisp Code
  5. Doc Lookup
  6. Search Doc
  7. How to Edit Lisp Code

Working with Brackets

  1. Highlight Brackets ()[]{}
  2. Insert Brackets by Pair
  3. Delete Brackets ()[]{} by Pair
  4. Select Line, between Quotes, Extend Selection
  5. Move Cursor to Bracket/Quote
  6. Change Brackets/Quotes

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp