Emacs Advent Calendar 12: Spell checking

Sat 2015-12-12

In addition to providing commands for fixing typos, Emacs can also do spell checking on buffer contents.

To check a single word, press M-$. To check the spelling of the whole buffer, use M-x ispell. If active, both commands will operate on a region instead.

For every word that is not in the dictionary, Emacs will ask you what to do. Normally it will present a list of similar words from which one can be chosen with the associated key. Other actions are summarized in the following table.

Key Action
SPC skip word
r replace with new word
R replace with new word and do a query-replace
a accept as correct for this session
i insert word into private dictionary
u insert lower case word into private dictionary
X, C-g suspend interactive spell checking
x quit spell checking

Note that a suspended spell checking session can be resumed from the location where point was left on suspension by pressing C-u M-$. In contrast, quitting a session will move point back to where interactive spell checking started.

To set the dictionary used for spell checking, run M-x ispell-change-dictionary. Use tab-completion to list available languages. To permanently set the default dictionary, for example to "de_DE", add (setq ispell-dictionary "de_DE") to your ~/.emacs file.

Another useful feature is flyspell mode, which provides, as one might expect from its name, on the fly spell checking. It can be started with M-x flyspell-mode. A very similar mode is flyspell prog mode which only checks the spelling of comments and strings.

To permanently activate spell checking for a mode, add flyspell-mode to the mode's hook in your emacs initialization file, e.g. (add-hook 'text-mode-hook 'flyspell-mode).

More information on this can be found in sections 16.4 (Checking and Correcting Spelling) and 48.2.2 (Hooks).


This text by Ludger Sandig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.