Emacs: Insert Parenthesis by Pair: electric-pair-mode

By Xah Lee. Date: . Last updated: .

This page shows you ways to insert brackets in pairs.

electric-pair-mode

Emacs 24 has a new minor mode electric-pair-mode. When on, typing any left bracket automatically insert the right matching bracket. [see Emacs 24.1 New Features (released 2012-06)]

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

;; auto close bracket insertion. New in emacs 24
(electric-pair-mode 1)

Brackets includes ASCII and Unicode brackets or quotation marks: " ' () {} [] «» and more [see 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. ( Alt+x 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.

[see Elisp: Association List]

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 '(
                            (?\" . ?\")
                            (?\{ . ?\})
                            ) )

Better Brackets Pairs Insert Command

Emacs: Insert Brackets by Pair

Working with Brackets Topic

  1. Emacs: Highlight Brackets ({[]})
  2. Emacs: Insert Brackets by Pair
  3. Emacs: Delete Parenthesis/Brackets ()[]{} by Pair
  4. Emacs: Select Line, between Quotes, Extend Selection
  5. Emacs: Move Cursor to Bracket/Quote
  6. Emacs: Change Brackets and Quotes

Enhanced Editing Commands Topic

  1. Emacs: Select Line, between Quotes, Extend Selection
  2. Emacs: Copy/Cut Current Line If No Selection
  3. Emacs: Copy/Cut All or Selection
  4. Emacs: Paste or Paste Previous
  5. Emacs: Delete Whitespace around Cursor
  6. Emacs: Move Cursor to Beginning of Line/Paragraph
  7. Emacs: Move Cursor by Text Block
  8. Emacs: Move Cursor to Bracket/Quote
  9. Emacs: Move Cursor to Punctuation
  10. Emacs: Delete Text Block
  11. Emacs: Toggle Comment Current Line
  12. Emacs: Insert Date Time

Emacs Lisp Basics Topic

  1. Emacs Lisp Basics
  2. Overview of Text-Processing in Emacs Lisp
  3. Emacs Lisp Examples
  4. How to Evaluate Emacs Lisp Code
  5. Elisp: Documentation Lookup
  6. Elisp: Search Documentation
  7. How to Edit Lisp Code with Emacs
Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

Ask me question on patreon