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:
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
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-
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),
(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-
highlighting also when point is inside of the parentheses. The
show-paren-when-point-in-periphery, when non-
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-
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
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
electric-pair-delete-adjacent-pairs, when non-
backspacing between two adjacent delimiters also automatically delete
the closing delimiter.
electric-pair-open-newline-between-pairs, when non-
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.