Linux

  1. Home
  2. Computing & Technology
  3. Linux

Expressions

Emacs Documentation

By Juergen Haas, About.com

Expressions with Balanced Parentheses

These commands deal with balanced expressions, also called sexps1.

  • C-M-f
    Move forward over a balanced expression (forward-sexp).
  • C-M-b
    Move backward over a balanced expression(backward-sexp).
  • C-M-k
    Kill balanced expression forward (kill-sexp).
  • C-M-<DEL>
    Kill balanced expression backward (backward-kill-sexp).
  • C-M-t
    Transpose expressions (transpose-sexps).
  • C-M-@
    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

* Emacs Manual Index

Explore Linux

About.com Special Features

Linux

  1. Home
  2. Computing & Technology
  3. Linux
  4. Linux Software
  5. Editors
  6. Emacs
  7. Emacs Documentation - Expressions

©2009 About.com, a part of The New York Times Company.

All rights reserved.