skip to content

Search

Getting abbrev mode to expand symbols in Emacs

1 min read

Enable abbrev-mode to expand symbols like -> to → in Emacs, with a fix for symbol characters and setup for Doom Emacs.

I wanted to use abbrev-mode in Emacs to automatically expand things like -- into and -> into . I defined the abbreviations, turned on abbrev-mode, and… nothing happened.

Turns out, symbol characters like -, <, and = aren’t treated as 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.