Is there a way to enable auto-pairing Python triple quotes in electric-pair-mode?
This can be configured in autopair-mode using autopair-python-triple-quote-action
. Is there a similar way to enable this in electric-pair-mode?
Note: Triple quotes, according to official Python documentation are docstrings, or multi-line docstrings and are not considered comments. Anything inside triple quotes is read by the interpreter. When the interpreter encounters the hash symbol, it ignores everything after that. That is what a comment is defined to be.
The syntax for triple quotes consists of three consecutive single or double quotes.
Escaping the quote characters with a backslash always works.
You can do the following:
(defun python-electric-pair-string-delimiter ()
(when (and electric-pair-mode
(memq last-command-event '(?\" ?\'))
(let ((count 0))
(while (eq (char-before (- (point) count)) last-command-event)
(setq count (1+ count)))
(= count 3)))
(save-excursion (insert (make-string 3 last-command-event)))))
(add-hook 'python-mode-hook
(lambda ()
(add-hook 'post-self-insert-hook
#'python-electric-pair-string-delimiter 'append t)))
It will be included in the next release of Emacs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With