Emacs: Insert Parenthesis/Brackets by Pair

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page shows you ways to insert bracket characters in pairs.


Emacs 24 has a new minor mode electric-pair-mode. When on, typing any left bracket automatically insert the right matching bracket. 〔➤ New Features in Emacs 24.1

You can have it on always. Put this in your emacs init:

(electric-pair-mode 1)

Brackets includes ASCII and Unicode brackets or quotation marks: "double", 'single', “curly”, ‘curly single’ () {} [] «» ‹› 「」 『』 〈〉 《》 〔〕 【】…. 〔➤ Matching Brackets in Unicode

Deleting one bracket doesn't delete the other.

Exactly which brackets are auto-closed depends on the current major mode's syntax table. (You can call describe-syntax to see current syntax table. (info "(elisp) Syntax Tables"))

If you always want certain brackets be inserted in pairs, you can customize the variable electric-pair-pairs. Its value should be a Association List ((info "(elisp) Association Lists")).

For example, the curly bracket {} isn't auto-closed when in emacs-lisp-mode. You can make it do so, like this:

;; make electric-pair-mode work on more brackets
(setq electric-pair-pairs '(
                            (?\" . ?\")
                            (?\{ . ?\})
                            ) )

Create Your Own Brackets Pairs System

Another way to insert brackets by pair is to define them yourself. Here's the advantage:

Define Bracket Insertion Commands

First, define the command to insert bracket pairs.

(require 'xeu_elisp_util)

(defun xah-insert-bracket-pair (φleft-bracket φright-bracket)
  "Insert a matching bracket.

If there's a text selection, insert brackets around it.
If there's no text selection:
  If cursor has alphanumeric char before or after, insert brackets around current word.
  else, insert brackets.

Alphanumeric char here includes hyphen and underscore.

The argument φleft-bracket φright-bracket are strings."
  (if (use-region-p)
        (let (
              (p1 (region-beginning))
              (p2 (region-end))
          (goto-char p2)
          (insert φright-bracket)
          (goto-char p1)
          (insert φleft-bracket)
          (goto-char (+ p2 2))
    (progn ; no text selection
      (if (or
           (looking-at "[_-A-Za-z0-9]")
           (looking-back "[_-A-Za-z0-9]")
            (let* (
                   (bds (unit-at-cursor 'word))
                   (p1 (elt bds 1))
                   (p2 (elt bds 2))
              (goto-char p2)
              (insert φright-bracket)
              (goto-char p1)
              (insert φleft-bracket)
              (goto-char (+ p2 (length φleft-bracket)))
          (insert φleft-bracket φright-bracket)
          (search-backward φright-bracket ) )) )))

here's the actual commands:

(defun xah-insert-paren () (interactive) (xah-insert-bracket-pair "(" ")") )
(defun xah-insert-bracket () (interactive) (xah-insert-bracket-pair "[" "]") )
(defun xah-insert-brace () (interactive) (xah-insert-bracket-pair "{" "}") )
(defun xah-insert-greater-less () (interactive) (xah-insert-bracket-pair "<" ">") )

(defun xah-insert-single-angle-quote‹› () (interactive) (xah-insert-bracket-pair "‹" "›") )
(defun xah-insert-double-angle-quote«» () (interactive) (xah-insert-bracket-pair "«" "»") )
(defun xah-insert-double-curly-quote“” () (interactive) (xah-insert-bracket-pair "“" "”") )
(defun xah-insert-curly-single-quote‘’ () (interactive) (xah-insert-bracket-pair "‘" "’") )
(defun xah-insert-ascii-double-quote () (interactive) (xah-insert-bracket-pair "\"" "\"") )
(defun xah-insert-ascii-single-quote () (interactive) (xah-insert-bracket-pair "'" "'") )
(defun xah-insert-emacs-quote () (interactive) (xah-insert-bracket-pair "`" "'") )
(defun xah-insert-corner-bracket「」 () (interactive) (xah-insert-bracket-pair "「" "」") )
(defun xah-insert-white-corner-bracket『』 () (interactive) (xah-insert-bracket-pair "『" "』") )
(defun xah-insert-angle-bracket〈〉 () (interactive) (xah-insert-bracket-pair "〈" "〉") )
(defun xah-insert-double-angle-bracket《》 () (interactive) (xah-insert-bracket-pair "《" "》") )
(defun xah-insert-white-lenticular-bracket〖〗 () (interactive) (xah-insert-bracket-pair "〖" "〗") )
(defun xah-insert-black-lenticular-bracket【】 () (interactive) (xah-insert-bracket-pair "【" "】") )
(defun xah-insert-tortoise-shell-bracket〔〕 () (interactive) (xah-insert-bracket-pair "〔" "〕") )

Setting Up Keys

Now you can set any key to call your commands. 〔➤ Emacs: How to Define Keys

Here's a key setup example, using 【▤ Menu ‹letter›】.

(global-set-key (kbd "<menu> f") 'insert-pair-brace)    ; {}
(global-set-key (kbd "<menu> d") 'insert-pair-paren)    ; ()
(global-set-key (kbd "<menu> s") 'insert-pair-bracket)  ; []

Key choices are based on home-row and frequency of the bracket. The code above is for QWERTY layout. You'll need different keys for Dvorak Keyboard Layout.

If you don't have the ▤ Menu key, you can use other keys. See: Emacs: How to Define Keys and Emacs: How to Define Super & Hyper Keys.

Deleting Brackets by Pair

If you want to automatically delete matched bracket when you delete a single one, you need to install autopairs package. 〔➤ Emacs: How to Install Packages Using ELPA, MELPA, Marmalade

Highlighting Brackets

See: How to Edit Lisp Code with Emacs

Navigating Brackets

See: Emacs: Move Cursor Around Brackets

Like it?
Buy Xah Emacs Tutorial
or share
blog comments powered by Disqus