efaq: Matching parentheses
5.27 How do I show which parenthesis matches the one I’m looking at?
====================================================================
Call ‘show-paren-mode’ in your ‘.emacs’ file:
(show-paren-mode 1)
You can also enable this mode by selecting the ‘Paren Match
Highlighting’ option from the ‘Options’ menu of the Emacs menu bar at
the top of any Emacs frame.
Alternatives to this mode include:
• If you’re looking at a right parenthesis (or brace or bracket) you
can delete it and reinsert it. Emacs will momentarily move the
cursor to the matching parenthesis.
• ‘C-M-f’ (‘forward-sexp’) and ‘C-M-b’ (‘backward-sexp’) will skip
over one set of balanced parentheses, so you can see which
parentheses match. (You can train it to skip over balanced
brackets and braces at the same time by modifying the syntax
table.)
• Here is some Emacs Lisp that will make the <%> key show the
matching parenthesis, like in ‘vi’. In addition, if the cursor
isn’t over a parenthesis, it simply inserts a % like normal.
;; By an unknown contributor
(global-set-key "%" 'match-paren)
(defun match-paren (arg)
"Go to the matching paren if on a paren; otherwise insert %."
(interactive "p")
(cond ((looking-at "\\s(") (forward-list 1) (backward-char 1))
((looking-at "\\s)") (forward-char 1) (backward-list 1))
(t (self-insert-command (or arg 1)))))