ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

How to Edit Lisp Code with Emacs

Xah Lee, , …,

This page gives you some tips about editing emacs lisp code in emacs. Most tips will also apply to editing any lisp code or code with many nested matching brackets (⁖ Common Lisp, Scheme Lisp, Clojure, newLISP, XML, even C syntax languages that have brackets.).

Convenient Highlighting Setup

Put the following code in your emacs init file:

;; highlight text selection (on by default since emacs 23.2)
(transient-mark-mode 1)

;; make typing overwrite text selection
(delete-selection-mode 1) ; this turns on transient-mark-mode automatically

Turn on show-paren-mode

You also want to turn on highlighting of matching brackets.

;; turn on highlight matching brackets when cursor is on one
(show-paren-mode 1)

show-paren-mode has 2 styles of highlighting brackets.

emacs show-paren-mode 1
Highligh just the brackets.
emacs show-paren-mode 2
Highligh the whole expression.

By default, emacs highlights just the brackets. You can change it with the following code:

(setq show-paren-style 'parenthesis) ; highlight just brackets
(setq show-paren-style 'expression) ; highlight entire bracket expression

Note: show-paren-mode will highlight any brackets, including () [] {} 「」 『』 【】 〖〗 〈〉 《》 ‹› «» …. 〔☛ Matching Brackets in Unicode

Navigating Nested Code

Lisp code with its nested parenthesis syntax represents a tree structure. Emacs has several commands that are very helpful in moving around nested syntax, analogous to navigating a tree. The following is a table showing the names, keys, and purpose. (For historical reasons, lisp code units are sometimes called “sexp”, short for Symbolic EXPression.)

ShortcutCommand namePurpose
Ctrl+Alt+backward-sexpMove to previous sibling
(move to the (beginning of) previous sexp unit)
Ctrl+Alt+forward-sexpMove to next sibling
(move to the (end of) next sexp unit)
Ctrl+Alt+backward-up-listMove to parent
(move to the (beginning of) outer paren pair)
Ctrl+Alt+down-listMove to first child
(move into the (beginning of) first inner paren pair)

The following is a lisp 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.

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

Place your cursor at the beginning of the left bracket. Now, try to move your cursor, by using ONLY 【Ctrl+Alt+‹arrows›】, 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:

Freely Moving Cursor to Brackets

It's also very convenient to have keys that moves to any prev/next opening/closing bracket characters, without regarding the tree structure. See: Emacs: Commands and Keys to Navigate Brackets.

Selecting a Sexp Unit

You can use the command mark-sexpCtrl+Alt+Space】 to select a complete sexp. However, your cursor must be on the left paren.

To select a complete sexp, type 【Ctrl+Alt+】 then 【Ctrl+Alt+Space】.

Inserting Brackets by Pairs

Call electric-pair-mode (new in emacs 24. 〔☛ New Features in Emacs 24〕).

When on, typing any left bracket automatically insert the right matching bracket. electric-pair-mode has some flaws. For detail and alternative solutions, see: Emacs: Insert Brackets by Pair.

Change Paren Match Background Color

You can change the default background color for highlighting the paren. You can do so by typing 【Alt+x customize-face Enter ↵ show-paren-match Enter ↵】. Then, move your cursor to the “Attributes” section, “Background” item, in the value section type “azure2” replacing the default “turquoise”. Then, click the button “Save for Future Sessions” at the top. This will save the lisp code in your emacs init file. For example, the following shows in my emacs init file:

(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(completions-common-part ((t (:inherit default :foreground "red"))))
 ;; '(diredp-ignored-file-name ((t (:foreground "#bebebe"))))
 '(diredp-compressed-file-suffix ((t (:foreground "#7b68ee"))))
 '(diredp-ignored-file-name ((t (:foreground "#aaaaaa"))))
 '(isearch ((((class color) (min-colors 88) (background light)) (:background "black" :foreground "white"))))
 '(show-paren-match ((((class color) (background light)) (:background "azure2")))))

To see a list of available colors and their names, call list-colors-display.

blog comments powered by Disqus