26.4.3 Matching Parentheses
Emacs has a number of parenthesis matching features, which make it easy to see how and whether parentheses (or other 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 ist
to enable it. Set it tojump
to make indication work by momentarily moving the cursor to the matching opening delimiter. Set it tojump-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
. To
customize it, 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 areparenthesis
(show the matching paren),expression
(highlight the entire expression enclosed by the parens), andmixed
(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 isnil
. -
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.
Electric Pair mode, a global minor mode, provides a way to easily insert matching delimiters: parentheses, braces, brackets, etc. Whenever you insert an opening delimiter, the matching closing delimiter is automatically inserted as well, leaving point between the two. Conversely, when you insert a closing delimiter over an existing one, no insertion takes places, and that position is simply skipped over. If the region is active (see The Mark and the Region), insertion of a delimiter operates on the region: the characters in the region are enclosed in a pair of matching delimiters, leaving point after the delimiter you typed.
These variables control additional features of Electric Pair mode:
-
electric-pair-preserve-balance
, when non-nil
, makes the default pairing logic balance out the number of opening and closing delimiters. -
electric-pair-delete-adjacent-pairs
, when non-nil
, makes backspacing between two adjacent delimiters also automatically delete the closing delimiter. -
electric-pair-open-newline-between-pairs
, when non-nil
, makes inserting a newline between two adjacent pairs also automatically open an extra newline after point. -
electric-pair-skip-whitespace
, when non-nil
, causes the minor mode to skip whitespace forward before deciding whether to skip over the closing delimiter.
To toggle Electric Pair mode, type M-x electric-pair-mode
. To
toggle the mode in a single buffer, use M-x electric-pair-local-mode
.