I tried using abbrev-mode
in Emacs to automatically expand things like --
into —
and ->
into →
. I defined the abbreviations and enabled
abbrev-mode
, but nothing happened.
The issue is that symbol characters like -
, <
, and =
aren’t considered
part of words by default, so Emacs doesn’t recognize them as valid abbreviation
triggers.
The Fix
To get around this, you can make those symbols word constituents using
modify-syntax-entry
:
(defun my/make-symbols-word-constituents ()
"Allow symbols to be recognized in abbrev triggers."
(modify-syntax-entry ?- "w")
(modify-syntax-entry ?> "w"))
Then define your abbreviations:
(define-abbrev-table 'global-abbrev-table
'(("--" "—")
("->" "→")))
Enable it in your desired mode(s):
(add-hook 'text-mode-hook #'abbrev-mode)
(add-hook 'text-mode-hook #'my/make-symbols-word-constituents)
Now, typing ->
followed by a space will expand to →
, --
to —
, just like
you’d expect.
Doom Emacs Users
If you’re using Doom Emacs, use after!
and add-hook!
like this:
(after! markdown-mode
(add-hook! 'markdown-mode-hook 'abbrev-mode)
(add-hook! 'markdown-mode-hook 'my/make-symbols-word-constituents))
(after! org
(add-hook! 'org-mode-hook 'abbrev-mode)
(add-hook! 'org-mode-hook 'my/make-symbols-word-constituents))
You can see my full configuration in cowboy-bebug/dotfiles@1121f2f. It includes the symbol setup, abbrev definitions, and mode hooks exactly as described above.