Spell Checking in Emacs using File-Specific Language Settings

Emacs provides functionality to perform spell-checking using ispell.

If all files are edited in the same language, it is sufficient to have a global language configuration for the ispell process. Some people however have the need to edit languages in two or more languages. Switching between the languages manually can lead to frustration if this needs to be done multiple times. Several solutions for this problem exist.

I personally like to provide the information on the desired spell checking language to a per-file basis. Emacs allows this by providing functionality to define and use file variables.

A file variable can be provided by including the variable definition inside two -*- delimiters in the first line. Example:

-*- mode: modename; var: value; ... -*-

This can be used to set the variable ispell-local-dictionary to a file-specific value

-*- ispell-local-dictionary: "de_DE" -*-

This line would force Emacs to load the German dictionary for this file. Setting the variable does not automatically perform a re-evaluation of the buffer with the new dictionary. In order to do so, you can instruct emacs to automatically load the spelling process and evaluate the buffer with the buffer-specific dictionary (I always use flyspell for that):

(add-hook 'text-mode-hook #'flyspell-mode)
(add-hook 'flyspell-mode-hook #'flyspell-local-vars)
(defun flyspell-local-vars ()
    (add-hook 'hack-local-variables-hook #'flyspell-buffer))

This will automatically load the locally defined dictionary and evaluate the buffer with the new dictionary.

Additional ways to load local variables are defined in the manual.