I use the following function, which I obtained here, for this purpose:
(defun bounce-paren ()
"Will bounce between matching parens just like % in vi"
(interactive)
(let ((prev-char (char-to-string (preceding-char)))
(next-char (char-to-string (following-char))))
(cond ((string-match "[[{(<]" next-char) (forward-sexp 1))
((string-match "[\]})>]" prev-char) (backward-sexp 1))
(t (error "%s" "Not on a paren, brace, or bracket")))))
Of note is that it only supports single-character pairs, and that modifying it to support multi-character delimiters would require essentially an entire rewrite. I have it bound to F2 (and rarely use it), but you could of course just as well bind it to %.
I commend to your attention the Smartparens mode, which supports arbitrary pairs of single- or multi-character delimiters, including cases where both delimiters are the same string. Once you have your pairs configured, a fairly trivial Lisp function, which depending on context calls one of the navigation functions sp-beginning-of-sexp
or sp-end-of-sexp
, should give you the behavior you desire.
Consider also highlight-parentheses-mode, which colors braces surrounding point; I find that using this mode gives me 99.4% of what I used to get from bouncing between braces, without needing to move point at all.