Selected Article

Emacs Spell Checking on Windows

Posted on: 2025-04-13, Updated on: 2025-04-13 20:21:51 | Read time: 2.2 minutes

Topic(s): emacs & programming

Getting the built-in ispell and flyspell spell-checking packages in Emacs to work on Windows machines requires some extra configuration. I will share very briefly how I got these packages working in this article.

Windows is pretty far from the UNIX-like way of doing things, so we don't have the programs needed for the interactive spell checkers installed already. Luckily, msys2 provides versions of the hunspell and associated dictionaries via mingw-w64. I have msys2 installed on my system (located in this path: C:/tools/msys64/) using the Chocolatey package manager for Windows, but you can also download msys2 here. Now with msys2 being installed, you can use the msys2 shell and execute the following commands to install hunspell and the English word dictionary: pacman -Syu && pacman -S mingw64/mingw-w64-x86_64-aspell mingw64/mingw-w64-x86_64-aspell-en. The command preceding the && operator is used to update the pacman package manager. Simply follow/accept the prompts you see on your console. Here are links describing each package mingw64/mingw-w64-x8664-aspell and mingw64/mingw-w64-x8664-aspell-en.

Now that you have hunspell installed and a dictionary installed, we need to configure Emacs to use the proper executable. Open up your ~/.emacs or ~/.emacs.d/init.el that is located in your user's “home” directory. Here is the snippet of Emacs Lisp code that I use to configure hunspell with ispell in my init file:

(use-package ispell
    :ensure nil
    :config
    ;; Configuration for the ispell functionality on Windows.
    ;; Using msys2 and the MinGW hunspell and hunspell-en packages.
    ;; https://www.reddit.com/r/emacs/comments/8by3az/how_to_set_up_sell_check_for_emacs_in_windows/
    ;; https://stackoverflow.com/questions/8931580/hunspell-cant-open-affix-or-dictionary-files-for-dictionary-named-en-us
    (when-system 'windows
                 (setq ispell-program-name "C:/tools/msys64/mingw64/bin/hunspell.exe" ; Set the executable pathname.
                       ispell-dictionary "en_US"))) ; Set the appropriate word dictionary.

The when-system macro is defined as:

(defmacro when-system (sys-symbols &rest body)
  "Control structure macro executes BODY when current system is one
of SYS-SYMBOLS.
SYS-SYMBOLS Can be a single symbol or list of symbols.

Example call with all possible SYS-SYSMBOLS:
(when-system '(windows linux bsd macos) ...)"
  (declare (indent 1) (debug ((symbolp form &optional form) body)))
  `(progn
     (let* ((eval-symbols ,sys-symbols) ; Store evaluated macro argument
            (symbols eval-symbols)) ; Store evaluated macro argument in symbols list
       ;; Convert single symbol to list if needed
       (unless (listp eval-symbols)
         (setq symbols (list eval-symbols)))
       ;; Build up result of applying logical OR to each symbol in the SYMBOLS list
       (when (seq-reduce
              #'(lambda (a b) (or a b))
              (mapcar (lambda (sym) ; This lambda relates a symbol to the corresponding SYSTEM-TYPE
                        (cond
                          ((eq sym 'windows) (string-equal system-type "windows-nt"))
                          ((eq sym 'linux) (string-equal system-type "gnu/linux"))
                          ((eq sym 'bsd) (string-equal system-type "berkeley-unix"))
                          ((eq sym 'macos) (string-equal system-type "darwin"))))
                      symbols)
              nil) ; Supplying nil as the INITIAL-VALUE ensures the correct behavior
         (progn ,@body)))))

Now you should be able to successfully use ispell, flyspell, and other spell checking modes. Make sure you replace the string value supplied to ispell-program-name in the setq form to the correct path of the hunspell executable on your system. You may also need to supply a different dictionary language to ispell-dictionary. Thanks for reading!


Post a Comment

Click to generate a new captcha.

0 Comments