[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

U.4.1 Expressions with Balanced Parentheses

These commands deal with balanced expressions, also called sexps(9).

Move forward over a balanced expression (forward-sexp).
Move backward over a balanced expression(backward-sexp).
Kill balanced expression forward (kill-sexp).
Kill balanced expression backward (backward-kill-sexp).
Transpose expressions (transpose-sexps).
Put mark after following expression (mark-sexp).

Each programming language major mode customizes the definition of balanced expressions to suit that language. Balanced expressions typically include symbols, numbers, and string constants, as well as any pair of matching delimiters and their contents. Some languages have obscure forms of expression syntax that nobody has bothered to implement in Emacs.

By convention, the keys for these commands are all Control-Meta characters. They usually act on expressions just as the corresponding Meta characters act on words. For instance, the command C-M-b moves backward over a balanced expression, just as M-b moves back over a word.

To move forward over a balanced expression, 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 balanced expression. The detailed rules are like those above for C-M-f, but with directions reversed. If there are prefix characters (single-quote, backquote and comma, in Lisp) preceding the expression, C-M-b moves back over them as well. The balanced expression 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 balanced expression 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. On some machines, C-M-DEL typed on the console is a command to reboot; when that is so, you cannot use it as an Emacs command. This conflict is rare, though: usually the DEL key for Emacs is really BACKSPACE, and the reboot command is C-M-DELETE, so there is no conflict.

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

To set the region around the next balanced expression 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 balanced expression.

In languages that use infix operators, such as C, it is not possible to recognize all balanced expressions as such because there can be multiple possibilities at a given position. For example, C mode does not treat `foo + bar' as a single expression, even though it is one C expression; instead, it recognizes `foo' as one expression and `bar' as another, with the `+' as punctuation between them. Both `foo + bar' and `foo' are legitimate choices for "the expression following point" when point is at the `f', so the expression commands must perforce choose one or the other to operate on. Note that `(foo + bar)' is recognized as a single expression in C mode, because of the parentheses.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on April 2, 2002 using texi2html