How can I use multiple dictionaries with the Emacs spell checker? Specifically I want to use a British English dictionary and a medical English dictionary at the same time.

  • Are you using flyspell?
    – PythonNut
    Apr 2 '16 at 15:37
  • Yes, I'm using flyspell. Apr 2 '16 at 15:46
  • 1
    The solution can use flyspell but doesn't have to. That way the answers can be relevant to more people. Apr 2 '16 at 16:05

Hunspell can spell check with multiple dictionaries, and you can configure this to work with Emacs. This is how I do it on OS X 10.11, with Emacs 25.0. It will not work with older Emacsen.

Install Hunspell

brew install hunspell

Download Hunspell dictionaries from LibreOffice and OpenMedSpel.

cd ~/Downloads/

curl http://extensions.libreoffice.org/extension-center/english-dictionaries/releases/2016.04.01/dict-en.oxt > dict-en.oxt
unzip dict-en.oxt en_GB.aff en_GB.dic

curl -L https://addons.mozilla.org/en-US/firefox/downloads/latest/6526/addon-6526-latest.xpi > openmedspel.xpi
unzip openmedspel.xpi dictionaries/OpenMedSpel.{aff,dic}
mv dictionaries/OpenMedSpel.dic en_US-med.dic
mv dictionaries/OpenMedSpel.aff en_US-med.aff

Put the dictionaries in ~/Library/Spelling/.

mv *.aff *.dic ~/Library/Spelling/

Add this to ~/.emacs/init.el:

    (with-eval-after-load "ispell"
      (setq ispell-program-name "hunspell")
      (setq ispell-dictionary "en_GB,en_US-med")
      ;; ispell-set-spellchecker-params has to be called
      ;; before ispell-hunspell-add-multi-dic will work
      (ispell-hunspell-add-multi-dic "en_GB,en_US-med"))
  • Note that the function ispell-hunspell-add-multi-dic seems to be not present in older versions of ispell.el, even the one in Emacs 24.5. I had to download the newest file at github.com/emacs-mirror/emacs/blob/master/lisp/textmodes/… and byte-compile it again to make it work.
    – xji
    May 8 '16 at 7:06
  • I'm using Emacs 25.0. I added a note about it working only on Emacs >= 25.0. May 12 '16 at 13:10
  • I installed Emacs 25. However, strangely I met the following error: Symbol’s function definition is void: ispell-hunspell-add-multi-dic, though I'm sure in /usr/share/emacs/25.1.50/lisp/textmodes/ispell.elc, the function is present. Any idea how it might have happened? Thanks.
    – xji
    Jun 11 '16 at 9:17
  • 1
    I realized it was my ispell.el in /usr/share/emacs/site-lisp/ shadowing the newer ispell.el in Emacs25... See unix.stackexchange.com/questions/28483/… for anybody who might be encountering the same issue.
    – xji
    Jun 11 '16 at 9:25

Suppose you already downloaded en_US-med.dic and en_US-med.aff and installed hunspell

Step 1, run hunspell -D in shell, which will tell the directory where hunspell searches for dictionaries, copy en_US-med.dic and en_US-med.aff to that directory.

Step 2, insert below code into ~/.emacs,

    (setq ispell-program-name "hunspell")
    ;; you could set `ispell-dictionary` instead but `ispell-local-dictionary' has higher priority
    (setq ispell-local-dictionary "en_US")
    (setq ispell-local-dictionary-alist '(("en_US" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil ("-d" "en_US,en_US-med") nil utf-8)))
    ;; new variable `ispell-hunspell-dictionary-alist' is defined in Emacs
    ;; If it's nil, Emacs tries to automatically set up the dictionaries.
    (when (boundp 'ispell-hunspell-dictionary-alist)
      (setq ispell-hunspell-dictionary-alist ispell-local-dictionary-alist))

We actually pass the option "-d en_US,en_US-med" to the hunspell CLI so it can use TWO dictionary "en_US" and "en_US-med" at the same time.

The "-d" options is documented in hunspell manual (man hunspell in shell)

Here is the quoted text from hunspell manual:

-d en_US,en_geo,en_med,de_DE,de_med

       en_US  and  de_DE  are  base dictionaries, they consist of aff and dic
       file pairs: en_US.aff, en_US.dic and  de_DE.aff,  de_DE.dic.   En_geo,
       en_med,  de_med  are  special dictionaries: dictionaries without affix
       file. Special dictionaries are optional extension of the base  dictio‐
       naries  usually  with  special (medical, law etc.)  terms. There is no
       naming convention for special dictionaries, only the ".dic" extension:
       dictionaries  without affix file will be an extension of the preceding
       base dictionary (right order of the parameter list needs for good sug‐
       gestions). First item of -d parameter list must be a base dictionary.

Tested on Emacs 24.3, Debian 7 with the word "fibrochondritis".

Should work at Emacs 23+ on any OS.

Please note on Windows the easiest way to let hunspell know the search path is to setup environment variable DICTPATH (it's documented in hunspell manual).

BTW, it's very possible the hunspell executable from Cygwin/MSYS2 only recognises path in UNIX format.

  • That method doesn't allow me to use multiple dictionaries. May 14 '16 at 7:04
  • The code I gave IS multi-dictionary.
    – chen bin
    May 14 '16 at 12:05
  • I see, thanks for pointing that out – I didn't notice the -d option. May 15 '16 at 15:22
  • Well this doesn't seem to be working for some reason. When I use the -d option in the command line it works, but in Emacs, helm-flyspell seems to somehow only use the dictionary with the system language.
    – xji
    Jun 11 '16 at 8:16
  • helm-flyspell might use aspell.
    – chen bin
    Jan 16 '17 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.