ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

Emacs: Insert Parenthesis/Brackets by Pair

Xah Lee, , …,

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

electric-pair-mode (emacs 24)

Emacs 24 (2012) 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

You can have it always on. 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 (i.e. a list of cons pairs. (info "(elisp) Association Lists")).

For example, the curly bracket {} isn't auto-closed when in emacs-lisp-mode. But if you put the following in your emacs init:

;; setting for auto-close brackets for electric-pair-mode regardless of current major mode syntax table
(setq electric-pair-pairs '(
                            (?\" . ?\")
                            (?\{ . ?\})
                            ) )

Now, {} will be inserted when you type just the opening one.

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 commands to insert bracket pairs.

(defun insert-bracket-pair (leftBracket rightBracket)
  "Insert a matching bracket.
If region is not active, place the cursor between them.
If region is active, insert around the region, place the cursor after the right bracket.

The argument leftBracket rightBracket are strings."
  (if (region-active-p)
      (let (
            (p1 (region-beginning))
            (p2 (region-end))
            )
        (goto-char p2)
        (insert rightBracket)
        (goto-char p1)
        (insert leftBracket)
        (goto-char (+ p2 2))
        )
    (progn
      (insert leftBracket rightBracket)
      (backward-char 1) ) )
  )
(defun insert-pair-paren () (interactive) (insert-bracket-pair "(" ")") )
(defun insert-pair-bracket () (interactive) (insert-bracket-pair "[" "]") )
(defun insert-pair-brace () (interactive) (insert-bracket-pair "{" "}") )

(defun insert-pair-double-straight-quote () (interactive) (insert-bracket-pair "\"" "\"") )
(defun insert-pair-single-straight-quote () (interactive) (insert-bracket-pair "'" "'") )

(defun insert-pair-single-angle-quote‹› () (interactive) (insert-bracket-pair "‹" "›") )
(defun insert-pair-double-angle-quote«» () (interactive) (insert-bracket-pair "«" "»") )
(defun insert-pair-double-curly-quote“” () (interactive) (insert-bracket-pair "“" "”") )
(defun insert-pair-single-curly-quote‘’ () (interactive) (insert-bracket-pair "‘" "’") )

(defun insert-pair-corner-bracket「」 () (interactive) (insert-bracket-pair "「" "」") )
(defun insert-pair-white-corner-bracket『』 () (interactive) (insert-bracket-pair "『" "』") )
(defun insert-pair-angle-bracket〈〉 () (interactive) (insert-bracket-pair "〈" "〉") )
(defun insert-pair-double-angle-bracket《》 () (interactive) (insert-bracket-pair "《" "》") )
(defun insert-pair-white-lenticular-bracket〖〗 () (interactive) (insert-bracket-pair "〖" "〗") )
(defun insert-pair-black-lenticular-bracket【】 () (interactive) (insert-bracket-pair "【" "】") )
(defun insert-pair-tortoise-shell-bracket〔〕 () (interactive) (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

Deleting Brackets by Pair

If you want to automatically delete matched bracket when you delete a single one, you need to install autopairs package. 〔☛ A Guide on Emacs Package System

Highlighting Brackets

See: How to Edit Lisp Code with Emacs

Navigating Brackets

See: Emacs: Keys to Navigate Brackets

blog comments powered by Disqus