Emacs: Jump to Matching Bracket 📜

By Xah Lee. Date: . Last updated: .

put this in your Emacs Init File:

(defvar xah-brackets '( "“”" "()" "[]" "{}" "<>" "<>" "()" "[]" "{}" "⦅⦆" "〚〛" "⦃⦄" "‹›" "«»" "「」" "〈〉" "《》" "【】" "〔〕" "⦗⦘" "『』" "〖〗" "〘〙" "「」" "⟦⟧" "⟨⟩" "⟪⟫" "⟮⟯" "⟬⟭" "⌈⌉" "⌊⌋" "⦇⦈" "⦉⦊" "❛❜" "❝❞" "❨❩" "❪❫" "❴❵" "❬❭" "❮❯" "❰❱" "❲❳" "〈〉" "⦑⦒" "⧼⧽" "﹙﹚" "﹛﹜" "﹝﹞" "⁽⁾" "₍₎" "⦋⦌" "⦍⦎" "⦏⦐" "⁅⁆" "⸢⸣" "⸤⸥" "⟅⟆" "⦓⦔" "⦕⦖" "⸦⸧" "⸨⸩" "⦅⦆")
 "A list of strings, each element is a string of 2 chars, the left bracket and a matching right bracket.
Used by `xah-select-text-in-quote' and others.
URL `http://xahlee.info/emacs/emacs/emacs_navigating_keys_for_brackets.html'
")

(defconst xah-left-brackets
  (regexp-opt (mapcar (lambda (x) (substring x 0 1)) xah-brackets))
  "Regex string of left bracket chars. Generated from `xah-brackets'.
URL `http://xahlee.info/emacs/emacs/emacs_navigating_keys_for_brackets.html'
")

(defconst xah-right-brackets
  (regexp-opt (mapcar (lambda (x) (substring x 1 2)) xah-brackets))
  "Regex string of right bracket chars. Generated from `xah-brackets'.
URL `http://xahlee.info/emacs/emacs/emacs_navigating_keys_for_brackets.html'
")

(defun xah-goto-matching-bracket ()
  "Move cursor to the matching bracket.
If cursor is not on a bracket, call `backward-up-list'.
The list of brackets to jump to is defined by `xah-left-brackets' and `xah-right-brackets'.

URL `http://xahlee.info/emacs/emacs/emacs_navigating_keys_for_brackets.html'
Created: 2016-11-22
Version: 2026-07-09"
  (interactive)
  (if (nth 3 (syntax-ppss))
      (backward-up-list 1 t t)
    (cond
     ((eq (char-after) ?\") (forward-sexp))
     ((eq (char-before) ?\") (backward-sexp))
     ((looking-at xah-left-brackets)
      (forward-sexp))
     ((if (eq (point-min) (point))
          nil
        (prog2
            (backward-char)
            (looking-at xah-right-brackets)
          (forward-char)))
      (backward-sexp)
      (while (looking-at "\\s'") (forward-char)))
     (t (backward-up-list 1 t t)))))

You need to give them a pair of keys for them to be useful. [see Emacs Keys: Define Key ⌨]

Emacs, Work with Brackets