How can I change the language in Emacs when using ispell?
EmacsIspellEmacs Problem Overview
I would like to use the ispell-buffer command in Emacs. It uses the English language by default. Is there an easy way to switch to another dictionary (for example, another language)?
Emacs Solutions
Solution 1 - Emacs
The following command proposes a list of installed dictionaries to use:
M-x ispell-change-dictionary
Usually, M-x isp-c-d
expands to the above also.
Solution 2 - Emacs
From the file ispell.el you may specify some options for the ispell
commands. This happens by adding a section to the end of your file like this:
;; Local Variables:
;; ispell-check-comments: exclusive
;; ispell-local-dictionary: "american"
;; End:
Note the double semicolon marks the start of comments in the current mode. It should probably be changed to reflect the way your file (programming language) introduces comments, like //
for Java.
Solution 3 - Emacs
At the end of a LaTeX file you can use:
%%% Local Variables:
%%% ispell-local-dictionary: "british"
%%% End:
that will set the dictionary to be used just for that file.
Solution 4 - Emacs
Use M-x ispell-change-dictionary
and hit TAB
to see what dictionary are available for you.
Then write the setting of default dictionary in your .emacs
, and add a hook to start ispell automatically for you specific mode (if you want).
For instance, start ispell in AUCTeX automatically using British English (by default English dictionary is American English)
(add-hook 'LaTeX-mode-hook 'flyspell-mode) ;start flyspell-mode
(setq ispell-dictionary "british") ;set the default dictionary
(add-hook 'LaTeX-mode-hook 'ispell) ;start ispell
Solution 5 - Emacs
If you want to change the language on a per-directory basis, you can add this to a .dir-locals.el
file:
(ispell-local-dictionary . "american")
If you have no .dir-locals.el
file already, it will look like this:
((nil .
((ispell-local-dictionary . "american")))
)
See the emacs wiki page about directory variables for more information.
Solution 6 - Emacs
For convenience (f7) I added the following to my .emacs:
(global-set-key [f7] 'spell-checker)
(require 'ispell)
(require 'flyspell)
(defun spell-checker ()
"spell checker (on/off) with selectable dictionary"
(interactive)
(if flyspell-mode
(flyspell-mode-off)
(progn
(flyspell-mode)
(ispell-change-dictionary
(completing-read
"Use new dictionary (RET for *default*): "
(and (fboundp 'ispell-valid-dictionary-list)
(mapcar 'list (ispell-valid-dictionary-list)))
nil t))
)))
BTW: don't forget to install needed dictionaries. E.g. on debian/ubuntu, for the german and english dictionary:
sudo apt install aspell-de aspell-en
Solution 7 - Emacs
Here is some code to remap the C-\ key to automatically toggle between multiple languages and to change the input method to the corresponding language. (derived from this post: https://stackoverflow.com/a/45891514/17936582 )
;; Toggle both distionary and input method with C-\
(let ((languages '("en" "it" "de")))
(setq ispell-languages-ring (make-ring (length languages)))
(dolist (elem languages) (ring-insert ispell-languages-ring elem)))
(defun ispell-cycle-languages ()
(interactive)
(let ((language (ring-ref ispell-languages-ring -1)))
(ring-insert ispell-languages-ring language)
(ispell-change-dictionary language)
(cond
((string-match "it" language) (activate-input-method "italian-postfix"))
((string-match "de" language) (activate-input-method "german-postfix"))
((string-match "en" language) (deactivate-input-method)))))
(define-key (current-global-map) [remap toggle-input-method] 'ispell-cycle-languages)