Last updated on December 10, 2016
In GNU Emacs, electric-pair-mode
is a minor mode for auto closing pairs of (curly) braces/brackets/quotes, which was first introduced in Emacs 24. However, up till now, it still has not provided an easy option to disable a certain pair—we need to make use of electric-pair-inhibit-predicate
. To disable a certain pair, such as double quotes, we can add the following to ~/.emacs
or ~/.emacs.d/init.el
:
(setq electric-pair-inhibit-predicate
`(lambda (c)
(if (char-equal c ?\") t (,electric-pair-inhibit-predicate c))))
To disable a certain pair for a specific mode, for example to disable the pairing of {}
in web-mode
(to allow web-mode
to better handle the auto pairing of the template tags {% %}
), we can add the following to ~/.emacs
or ~/.emacs.d/init.el
:
;; disable {} auto pairing in electric-pair-mode for web-mode
(add-hook
'web-mode-hook
(lambda ()
(setq-local electric-pair-inhibit-predicate
`(lambda (c)
(if (char-equal c ?{) t (,electric-pair-inhibit-predicate c))))))
For more information, please move onto the documentation of the variable electric-pair-inhibit-predicate
.
This blog really helps me. May I use your code in my repo?
Sure, no problem!
Pingback: 2016-11-28 Emacs News - sacha chua :: living an awesome life
You want to use `add-function`, in case the major mode (e.g. web-mode) already modifies `electric-pair-inhibit-predicate`.
Updated. Although `add-function` is still not used, your concern should be fixed now. Thanks!