Enable spell-checking in Vim and customize default behavior

To make Vim an even better editor, you can turn on its spell-checking functionality and customize it to behave the way you want depending on your task. Vincent Danen tells you how.

Vim is a fantastic editor, with a lot of tricks up its sleeve. It makes a great programmer's text editor, a great quick text file editor, and works wonders in conjunction with certain mail clients like Mutt.

One thing that may be lesser known about Vim is that it has built-in spelling support, which can make using it to write email and documentation even more useful. No more need to hope your spelling is correct, or double-checking it later with something like ispell or another text editor.

To enable spelling support, which was introduced in Vim 7, use the following command in normal mode:

:set spell spelllang=en_ca

Change the local code to whatever language you happen to be writing in. The above is for Canadian English; use "en_us" for American English, "en_gb" for English in Great Britain, etc.

Once spelling is activated, spelling mistakes and other inconsistencies will be color highlighted when typing in insert mode; in gVim this will result in squiggly underlines and in Vim itself, will result in highlighted words. This largely depends on your color scheme, but typically misspelled words will be highlighted red, with other inconsistencies such as improper capitalization being highlighted blue or cyan.

Click to enlarge.

If you are working on code, you may get tired of seeing all the highlighted misspelled "words" (i.e. function or variable names), and you can disable spellchecking by using:

:set nospell

Vim offers some command shortcuts to navigate to misspelled or incorrect words. The [s and ]s keyboard commands will move you to the previous or next misspelled word, respectively. To see a suggested list of words in order to correct the spelling, use z= in command mode to get the suggested list of replacement words, for example:

Change "documnt" to:
 1 "document"
 2 "documents"
 3 "docent"
Type number and <Enter> or click with mouse (empty cancels):

If you are like me and use Mutt for email, with Vim as the editor, you may want spelling enabled by default only if you are composing an email. You can do something like the following if autocmd support was compiled into Vim, which it should be for most. (Some distributions provide a minimal Vim and an enhanced Vim; you'll want the enhanced Vim for this).

" Only do this part when compiled with support for autocommands.
if has("autocmd")
    " disable autoindent, make the background dark, change the colorscheme to
    " suit the dark background, make sure our textwidth is 72 characters and
    " disable line numbering for email
    autocmd FileType mail set noautoindent |
        \ set nosmartindent |
        \ set background=dark |
        \ colorscheme evening |
        \ set textwidth=72 |
        \ set nonumber |
        \ set spell spelllang=en_ca
endif " has("autocmd")

The above has a few more bits that are nice when using Vim to write email, but the primary point of interest is matching the autocmd FileType to "mail" and then setting the spell option accordingly. This will enable spelling only when the FileType is "mail"; otherwise spelling is off by default.

Because Vim and gVim make such nice text editors, having the ability to set spelling support within them makes them that much better. And being able to customize exactly when it is enabled, and when it is disabled by default, really helps to avoid remembering (or forgetting) to set it on your own.

Get the PDF version of this tip here.

About Vincent Danen

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

Editor's Picks