2009-12-30

automatically checking your spelling

When I'm typing fast, it's easy to make spelling mistakes (as you may have noticed reading Emacs-Fu). It's not so much that I don't know how to write things, but sometimes there seems to be a bit of a disconnect between brain and fingers.

One tool that helps me to make fewer mistakes is automatic spelling checking, and in particular on-the-fly spelling checking. Spell-checking in emacs is taken care of by the ispell-package. Traditionally, this was a way to interface emacs with the ispell-program on Unix, but nowadays it's often used with other programs, such as aspell.

aspell is GNU's intended replacement for ispell, and has been for a long time. In the meantime, other spelling checkers have come up; in the Free Software world, the most prominent one is probably hunspell, which is used by e.g. Mozilla and OpenOffice. As I said, Emacs supports all of those; I'm using aspell, which works well for me. To use aspell for spelling-checking, I have the following in my .emacs:

(setq ispell-program-name "aspell"
  ispell-extra-args '("--sug-mode=ultra"))

Please consult the aspell documentation for the details.

You can spell-check your text with ispell-buffer and friends, which are also available through the menu (Tools/Spell Checking/...). This works fine, but it makes spelling checking a separate step you have to go through and you may forget. So, I like to do the spelling-checking on-the-fly, that is, while I am typing. This can be done using flyspell-mode (you can also use flyspell-prog-mode, to do spell checking inside comments in computer programs, but I find that a bit over the top).

When flyspell-mode is enabled, it will mark misspelt eh misspelled words by painting them in a different color while you are typing -- the common way many word processing programs do it. A common way to enable flyspell-mode is to put it the the mode-hook for the modes where you want to use it. For example, to enable flyspell-mode for all your org-mode buffers, you can add something like the following to your .emacs:

(add-hook 'org-mode-hook
  (lambda()
    (flyspell-mode 1)))

Note, you can use the middle mouse button to get alternatives for a misspelled word.

By default, it uses the English dictionary, but it's easy to switch to another using M-x ispell-change-dictionary. To make it easier, I have defined the C-c N key binding to activate Dutch-language ("nederlands") spelling checking, and update the buffer.

(global-set-key (kbd "C-c N") 
  (lambda()(interactive)
    (ispell-change-dictionary "nederlands")
    (flyspell-buffer))) 

Now, there's another category of mistakes – their-they're-there, its-it's or to-too-two that require a spelling checker that's a bit smarter. There are some free implementations in OpenOffice and Abiword; it'd be interesting to see if those could be integrated with emacs as well.

Now, laugh about the sweet irony of the spelling errors that I failed to notice :)

9 comments:

yo said...

For grammar checking there is the command line diction program and the emacs mode diction.el.

rico said...

the auto lang mode
http://www.emacswiki.org/emacs/AutoLangMode
checks the text and sets the dict on the fly

Anonymous said...

Sometimes, flyspell-mumamo-mode works well, e.g. for TeX-mode.

oylenshpeegul said...

Nice! I think the org-mode-hook example needs another close paren.

djcb said...

@oylenshpeegul: thanks, fixed.

Roland Smith said...

I like to do the following;

cat foo.tex | ispell -t -l -W 4 -C -d nederlands | sort | uniq | less


This gives me a list that ispell thinks are wrong. I can then pick out the really misspelt words.

dot said...

I often write documents with a mix of English and Spanish (such as, changing language every paragraph). Would it be possible to configure aspell so that it looks into two dictionaries in parallel?

I assume it already does, given that it has a personal dictionary and the distribution one, so it is only a matter of interface.

Sergio said...

Great tip Dirk as usual

Anonymous said...

@jyby check out http://www.dur.ac.uk/p.j.heslin/Software/Emacs/Download/flyspell-babel.el