Previous: Moving in the Parenthesis Structure, Up: Commands for Editing with Parentheses [Contents][Index]
28.4.3 Matching ParenthesesEmacs has a number of parenthesis matching features, which make it easy to see how and whether parentheses (or other paired delimiters) match up.
Whenever you type a self-inserting character that is a closing delimiter, Emacs briefly indicates the location of the matching opening delimiter, provided that is on the screen. If it is not on the screen, Emacs displays some of the text near it in the echo area. Either way, you can tell which grouping you are closing off. If the opening delimiter and closing delimiter are mismatched—such as in ‘[x)’—a warning message is displayed in the echo area.
Three variables control the display of matching parentheses:
blink-matching-paren
turns the feature on or off: nil
disables it, but the default is t
to enable it. Set it to jump
to make indication work by momentarily moving the cursor to the matching opening delimiter. Set it to jump-offscreen
to make the cursor jump, even if the opening delimiter is off screen.blink-matching-delay
says how many seconds to keep indicating the matching opening delimiter. This may be an integer or floating-point number; the default is 1.blink-matching-paren-distance
specifies how many characters back to search to find the matching opening delimiter. If the match is not found in that distance, Emacs stops scanning and nothing is displayed. The default is 102400.Show Paren mode is a minor mode that provides a more powerful kind of automatic matching. Whenever point is before an opening delimiter or after a closing delimiter, the delimiter, its matching delimiter, and optionally the text between them are highlighted. To toggle Show Paren mode globally, type M-x show-paren-mode. To toggle it only in the current buffer, type M-x show-paren-local-mode.
By default, this mode is switched on in all buffers that are meant for editing, but is not enabled in buffers that show data. This is controlled by the show-paren-predicate
user option.
To customize the mode, type M-x customize-group RET paren-showing. The customizable options which control the operation of this mode include:
show-paren-highlight-openparen
controls whether to highlight an open paren when point is just before it, and hence its position is marked by the cursor anyway. The default is non-nil
(yes).show-paren-style
controls whether just the two parens, or also the text between them get highlighted. The valid options here are parenthesis
(show the matching paren), expression
(highlight the entire expression enclosed by the parens), and mixed
(highlight the matching paren if it is visible in the window, the expression otherwise).show-paren-when-point-inside-paren
, when non-nil
, causes highlighting also when point is inside of the parentheses. The default is nil
.show-paren-when-point-in-periphery
, when non-nil
, causes highlighting also when point is in whitespace at the beginning of a line and there is a paren at the first or last non-whitespace position on the line, or when point is at the end of a line and there is a paren at the last non-whitespace position on the line.show-paren-context-when-offscreen
, when non-nil
, shows some context in the echo area when point is in a closing delimiter and the opening delimiter is offscreen. The context is usually the line that contains the opening delimiter, except if the opening delimiter is on its own line, in which case the context includes the previous nonblank line.Electric Pair mode is a minor mode that provides a way to easily insert pairs of matching delimiters: parentheses, braces, brackets, quotes, etc. (what counts as matching delimiters depends on the major mode). To toggle Electric Pair mode globally, type M-x electric-pair-mode. To toggle it only in the current buffer, type M-x electric-pair-local-mode.
When this mode is enabled, typing an opening delimiter inserts both that character and, immediately following it, the matching closing delimiter, leaving point between the two. This makes it unnecessary to type a matching closing delimiter in most cases. If you type one nonetheless, Emacs simply inserts that character, unless point is immediately before a closing delimiter of the same type; in that case, point moves to immediately after the closing delimiter and no additional closing delimiter is inserted. Thus, typing the sequence <opening delimiter>, <matching closing delimiter> is a perhaps more convenient alternative to the sequence <opening delimiter>, C-f.
With an active region (see The Mark and the Region), Electric Pair mode operates differently: inserting either an opening or a closing delimiter encloses the characters in the region within the resulting pair of matching delimiters, leaving point after the delimiter you typed (this facilitates continuing to type either before the text following the opening delimiter or after the closing delimiter).
There are several user options for modifying the behavior of Electric Pair mode:
electric-pair-preserve-balance
, when non-nil
(the default), makes typing a delimiter preserve the balance between opening and closing delimiters. Thus, if you type an opening delimiter and there is an unpaired matching closing delimiter later in the buffer, then only the opening delimiter gets inserted (and not a matching closing delimiter immediately following it); likewise, if there is an unpaired opening delimiter, then typing a matching closing delimiter later in the buffer inserts this character even when the following character is another matching closing delimiter.
When set to nil
, typing an opening delimiter inserts only this character, but only when point is either immediately before or immediately after a matching opening delimiter, or immediately before a letter or digit; in all other positions inserting an opening delimiter automatically inserts a matching closing delimiter immediately following it, even if there is an unpaired matching closing delimiter later in the buffer. And typing a closing delimiter immediately before another closing delimiter of the same type does not insert that character but moves point as described above, even when there is an unpaired matching opening delimiter earlier in the buffer.
If there is an active region, this variable has no effect.
electric-pair-delete-adjacent-pairs
, when non-nil
(the default), makes deleting an opening delimiter by typing the DEL key (which is normally the BACKSPACE key; see If DEL Fails to Delete) automatically also delete an immediately following matching closing delimiter (but not if there are any characters—including just whitespace—between the paired delimiters). When set to nil
, typing BACKSPACE deletes only the opening delimiter. (Typing BACKSPACE to delete a closing delimiter always deletes only this character.)electric-pair-open-newline-between-pairs
is non-nil
(the default) and point is between an opening delimiter and an immediately following matching closing delimiter, then typing a newline automatically inserts an extra newline after point (possibly indenting the empty line point is on, depending on the major mode). When set to nil
, typing a newline inserts only one newline before point, as usual.electric-pair-skip-whitespace
has its default non-nil
value and point is separated from a closing delimiter only by whitespace, then typing a closing delimiter of the same type does not insert that character but instead moves point to immediately after the already present closing delimiter. You can also set this option to additionally delete any whitespace that point moves over. When set to nil
, typing a closing delimiter simply inserts that character (even when this makes the following closing delimiter of the same type unbalanced).RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4