14

I have my hunspell dictionaries at C:\hunspell\. It contains .dic and .aff files for three languages: en_GB, en_US, and nb_NO. The descriptions I find on the web about how to make hunspell work in Emacs make me confused.

What is the minimum code I need in my init file to be able to use these three hunspell dictionaries in Emacs?

I have tried the following code suggested by one web site:

(add-to-list 'exec-path "C:/hunspell/bin/")
(setq ispell-program-name (locate-file "hunspell"
                       exec-path exec-suffixes 'file-executable-p))

But when wanting to change dictionary by M-x ispell-change-dictionary, I get the following message:

ispell-phaf: No matching entry for nil.

2

6 Answers 6

7

Emacs setup:

(setq ispell-program-name "c:/what-ever-path/hunspell")

;; "en_US" is key to lookup in `ispell-local-dictionary-alist'.
;; Please note it will be passed as default value to hunspell CLI `-d` option
;; if you don't manually setup `-d` in `ispell-local-dictionary-alist`
(setq ispell-local-dictionary "en_US") 

(setq ispell-local-dictionary-alist
      '(("en_US" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil ("-d" "en_US") 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))

;; With Emacs 28.2 may also need to set hunspell-default-dict
(setq hunspell-default-dict "en_US")

Hunspell dictionary setup:

Run hunspell -D in dos window which will list the directories hunspell searching for dictionaries. Copy your dictionaries to that directory. This is the minimum setup you need.

See http://blog.binchen.org/posts/what-s-the-best-spell-check-set-up-in-emacs.html for more technical details.

2
  • Worked for me! My setup with use-package is (use-package ispell :config (setq ispell-program-name "C:/Program Files (x86)/hunspell-1.3.2-3-w32-bin/bin/hunspell.exe") (setq ispell-local-dictionary "en_US") (setq ispell-local-dictionary-alist '(("en_US" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil ("-d" "en_US") nil utf-8)))) Sep 6, 2018 at 18:09
  • Just do (setq ispell-program-name "c:/what-ever-path/hunspell.exe") in Windows 10 for hunspell without other configuration works for English spelling check.
    – Yu Shen
    Feb 12, 2019 at 2:59
4

I ran into this problem myself a while back. If I recall correctly, the reason you get that error message is because hunspell is unable to configure itself based on the current environment. So to fix it, you need to configure the hunspell specific ispell variables. The following code should be enough to setup hunspell for english dictionaries:

(require 'flyspell)
(setq ispell-dictionary "english")
(add-to-list 'ispell-local-dictionary-alist '(("english"
                                               "[[:alpha:]]"
                                               "[^[:alpha:]]"
                                               "[']"
                                               t
                                               ("-d" "en_US")
                                               nil
                                               utf-8)))
(setq ispell-hunspell-dictionary-alist ispell-local-dictionary-alist)

The important part is the ispell-hunspell-dictionary-alist, it has to be populated with a proper dictionary list, such as the one given in ispell-local-dictionary-alist.

There's quite a bit of details surrounding this list though. If you want to know more about it, feel free to read up with M-x describe-variable ispell-local-dictionary-alist.

1
  • +1 there seems to be something wrong with the default attempt to populate the ispell-hunspel-dictionary-alist. Instead of (require 'flyspell) you might consider (with-eval-after-load "ispell" ...) Nov 23, 2018 at 16:54
1

Install all the dictionaries you want in the location that hunspell searches in; find this with hunspell -D. Once installed, this command should show them.

In the init file, add just add one of them E.g. I have en_GB and en_US dictionaries installed. I've, in my init file, this:

(setenv "DICTIONARY" "en_GB")

Upon opening Emacs, just enable flyspell-mode. Emacs should say that it is started ispell with default dictionary. This means en_GB is in action, for our example. Now if you want to switch, just do M-x ispell-change-dictionary and give the new dictionary name E.g. en_US. Now the other dictionary should be in action. This, again, will be notified by Emacs saying it started ispell but this time with the en_US dictionary.

1

Assuming you're using a recent version of Emacs (24.4 or above, as I recall) then all you need to do is make sure you're using the correct dictionary name, and Emacs will do the rest automatically.

The main problem is that Windows uses a different language description format, for example British English is called ENG, and US English is ENU. This means your dictionary files should be called ENU.dic and ENU.aff for US English, and ENG.dic and ENG.aff for British English.

It may also be necessary to have a "default" dictionary or hunspell may not be too happy. You can also set the DICTIONARY environment variable to force a default.

Unfortunately I can't work out what your Norwegian dictionary should be called. If you're using the Norwegian locale in Windows, you should be able to check within Emacs by evaluating:

(getenv "LANG")

Which will show you the setting that Emacs is using.

1

chen bin and serghei's answer worked for me on Emacs 26.x, but not with Emacs 27.1, which seems to have an updated ispell.el file. In Emacs 27.1, I would get:

Can’t find Hunspell dictionary with a .aff affix file

To "fix", I overwrote ./share/emacs/27.1/lisp/textmodes/ispell.el with a version from 26.x.

1
0

This is just a guess, but maybe you need to tell which language you would like to use as the “default”:

(setq ispell-dictionary "en") ; default dictionary

Default value of ispell-dictionary is nil, so may be this is the cause of your problem.

1
  • Thanks Mark, for your tip, but unfortunately it does not seem to change my situation. The error message still pops up
    – myotis
    Aug 22, 2015 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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