Next: Defuns Prev: Program Modes Up: Programs

Lists and Sexps

   By convention, Emacs keys for dealing with balanced expressions are
usually `Control-Meta-' characters.  They tend to be analogous in
function to their `Control-' and `Meta-' equivalents.  These commands
are usually thought of as pertaining to expressions in programming
languages, but can be useful with any language in which some sort of
parentheses exist (including English).

   The commands fall into two classes.  Some commands deal only with
"lists" (parenthetical groupings).  They see nothing except
parentheses, brackets, braces (depending on what must balance in the
language you are working with), and escape characters that might be used
to quote those.

   The other commands deal with expressions or "sexps".  The word `sexp'
is derived from "s-expression", the term for a symbolic expression in
Lisp.  In Emacs, the notion of `sexp' is not limited to Lisp.  It
refers to an expression in the language  your program is written in.
Each programming language has its own major mode, which customizes the
syntax tables so that expressions in that language count as sexps.

   Sexps typically include symbols, numbers, and string constants, as
well as anything contained in parentheses, brackets, or braces.

   In languages that use prefix and infix operators, such as C, it is
not possible for all expressions to be sexps.  For example, C mode does
not recognize `foo + bar' as an sexp, even though it is a C expression;
it recognizes `foo' as one sexp and `bar' as another, with the `+' as
punctuation between them.  This is a fundamental ambiguity: both `foo +
bar' and `foo' are legitimate choices for the sexp to move over if
point is at the `f'.  Note that `(foo + bar)' is a sexp in C mode.

   Some languages have obscure forms of syntax for expressions that
nobody has bothered to make Emacs understand properly.

     Move forward over an sexp (`forward-sexp').

     Move backward over an sexp (`backward-sexp').

     Kill sexp forward (`kill-sexp').

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

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

     Move forward over a list (`forward-list').

     Move backward over a list (`backward-list').

     Transpose expressions (`transpose-sexps').

     Put mark after following expression (`mark-sexp').

   To move forward over an 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.  If the character after point is a closing
delimiter, `C-M-f' just moves past it.  (This last is not really moving
across an sexp; it is an exception which is included in the definition
of `C-M-f' because it is as useful a behavior as anyone can think of
for that situation.)

   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,
back quote, and comma, in Lisp) preceding the sexp, `C-M-b' moves back
over them as well.

   `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.

   In languages such as C where the comment-terminator can be
recognized, the sexp commands move across comments as if they were
whitespace.  In Lisp and other languages where comments run until the
end of a line, it is very difficult to ignore comments when parsing
backwards; therefore, in such languages the sexp commands treat the
text of comments as if it were code.

   Killing an sexp at a time can be done with `C-M-k' (`kill-sexp').
`C-M-k' kills the characters that `C-M-f' would move over.

   The "list commands", `C-M-n' (`forward-list') and `C-M-p'
(`backward-list'), move over lists like the sexp commands but skip over
any number of other kinds of sexps (symbols, strings, etc).  In some
situations, these commands are useful because 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
is 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.

   `C-M-t' (`transpose-sexps') 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 make the region be the next sexp in the buffer, use `C-M-@'
(`mark-sexp') which sets the 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.
Note: Syntax.

automatically generated by info2www