3

I am trying to add the verbatim character ~ (tilde) as an electric-pair character in org-mode. Here is my attempt:

(with-eval-after-load 'org
 (modify-syntax-entry ?/ "(/" org-mode-syntax-table)
 (modify-syntax-entry ?= "(=" org-mode-syntax-table)
 (modify-syntax-entry ?~ "(~" org-mode-syntax-table))

This works for the / and = characters, but not for ~. What do I need to do to add ~?

Matthew Piziak
  • 5,958
  • 3
  • 29
  • 77

1 Answers1

5

The syntax of ~ is changed by org-mode itself: as part of the definition of the mode, the file org.el contains

(modify-syntax-entry ?~ "_")

inside (define-derived-mode org-mode...). Since the command org-mode is not necessarily run at the time the file is loaded, you need to put your modification in to org-mode-hook instead, e.g.

(add-hook 'org-mode-hook (lambda ()  (modify-syntax-entry ?~ "$~" org-mode-syntax-table)))

so this run after the built-in syntax entry. Your other definitions work, because org does not set the syntax for the those characters.

Note, as ~ is meant to match with itself, you should use the syntax class $ rather than (. The syntax class ( indicates that the given character is paired with a different symbol.

Andrew Swann
  • 3,436
  • 2
  • 15
  • 43