24.3. List And Sexp Commands

C-M-f

Move forward over a sexp (forward-sexp).

C-M-b

Move backward over a sexp (backward-sexp).

C-M-k

Kill sexp forward (kill-sexp).

C-M-DEL

Kill sexp backward (backward-kill-sexp).

C-M-u

Move up and backward in list structure (backward-up-list).

C-M-d

Move down and forward in list structure (down-list).

C-M-n

Move forward over a list (forward-list).

C-M-p

Move backward over a list (backward-list).

C-M-t

Transpose expressions (transpose-sexps).

C-M-@

Put mark after following expression (mark-sexp).

To move forward over a sexp, use C-M-f (forward-sexp). If the first significant character after point is an opening delimiter (( in Lisp; (, [ or { in C), C-M-f moves past the matching closing delimiter. If the character begins a symbol, string, or number, C-M-f moves over that.

The command C-M-b (backward-sexp) moves backward over a sexp. The detailed rules are like those above for C-M-f, but with directions reversed. If there are any prefix characters (single-quote, backquote and comma, in Lisp) preceding the sexp, C-M-b moves back over them as well. The sexp commands move across comments as if they were whitespace in most modes.

C-M-f or C-M-b with an argument repeats that operation the specified number of times; with a negative argument, it moves in the opposite direction.

Killing a whole sexp can be done with C-M-k (kill-sexp) or C-M-DEL (backward-kill-sexp). C-M-k kills the characters that C-M-f would move over, and C-M-DEL kills the characters that C-M-b would move over.

The list commands move over lists, as the sexp commands do, but skip blithely over any number of other kinds of sexps (symbols, strings, etc.). They are C-M-n (forward-list) and C-M-p (backward-list). The main reason they are useful is that they usually ignore comments (since the comments usually do not contain any lists).

C-M-n and C-M-p stay at the same level in parentheses, when that's possible. To move up one (or n) levels, use C-M-u (backward-up-list). C-M-u moves backward up past one unmatched opening delimiter. A positive argument serves as a repeat count; a negative argument reverses direction of motion and also requests repetition, so it moves forward and up one or more levels.

To move down in list structure, use C-M-d (down-list). In Lisp mode, where ( is the only opening delimiter, this is nearly the same as searching for a (. An argument specifies the number of levels of parentheses to go down.

A somewhat random-sounding command which is nevertheless handy is C-M-t (transpose-sexps), which drags the previous sexp across the next one. An argument serves as a repeat count, and a negative argument drags backwards (thus canceling out the effect of C-M-t with a positive argument). An argument of zero, rather than doing nothing, transposes the sexps ending after point and the mark.

To set the region around the next sexp in the buffer, use C-M-@ (mark-sexp), which sets mark at the same place that C-M-f would move to. C-M-@ takes arguments like C-M-f. In particular, a negative argument is useful for putting the mark at the beginning of the previous sexp.

The list and sexp commands' understanding of syntax is completely controlled by the syntax table. Any character can, for example, be declared to be an opening delimiter and act like an open parenthesis. Section 32.6.