Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

31.4.1 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-t
Transpose expressions (transpose-sexps).
[email protected]
C-M-<SPC>
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). C-M-k kills the characters that C-M-f would move over.

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, moving the previous expression over that many following ones. 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 [email protected] (mark-sexp), which sets mark at the same place that C-M-f would move to. [email protected] 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. The alias C-M-<SPC> is equivalent to [email protected]. When you repeat this command, or use it in Transient Mark mode when the mark is active, it extends the region by one sexp each time.

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.


Footnotes

[1] The word “sexp” is used to refer to an expression in Lisp.



 
 
  Published under the terms of the GNU General Public License Design by Interspire