Using Spell Checking in Vim

2014-12-24

In this post I'll show how spell checking can be used in Vim. At the end, I'll also show how to create your own dictionary and keep it under version control.

Using spell checking is very simple. Just use :setlocal spell to turn it on for the current buffer. Incorrectly spelled words will be highlighted.

Key Combinations

The following key combinations can be used in normal mode after turning on spell checking.

z=
Correct the misspelled word under the cursor. This pops up a list of words and asks you to select the correct one.
]s
Move to the next misspelled word after the cursor.
[s
Move to the previous misspelled word before the cursor.

There is also an idea of a user-defined "spell file". This file holds words that have been added by the user. For example, if you turn on spell checking in a buffer and type in "Haskell", it will show up as an incorrect word, but you can use the following commands to add it to your spell file.

You can use the following key combinations in normal mode to add or delete words from the spell file.

zg
Add word under cursor to spell file as "correct word".
zw
Add word under cursor to spell file as "incorrect word".
zug
Undo a previous zg.
zuw
Undo a previous zw.

Additional Settings

The first time you add a word, the user-defined spell file will be created under the directory spell/ in the first directory in your runtimepath. By default, this will be ~/.vim/spell/en.utf-8.add. However, if you are using a plugin system like Vundle or pathogen, it might have changed your runtimepath, so the spell file might be created somewhere else.

In order to have the directory ~/.vim/ come first in your runtimepath, you may have to add something like the following to your ~/.vimrc. Make sure to add this after all of the Vundle or pathogen code.

" Make sure that ~/.vim/ is first in the
" runtimepath.  Commands like zg will store
" the user-defined spell file in the first
" entry in the runtime path. (By default, it will
" store the spell file in a file like
" spell/en.utf-8.spl in the first entry in
" runtimepath.)
let &runtimepath=("~/.vim/," . &runtimepath)

In order to check what your runtimepath looks like, you can use the following command: :set runtimepath

Languages other than English can also be used. However, you may have to download the dictionary separately. Add the following to your ~/.vimrc.

" Make sure spell checking is done in French.
" (spelllang defaults to 'en'.)
set spelllang=fr

The spell file will then become ~/.vim/spell/fr.utf-8.spl.

A completely different spell file can also be used. It can be set like the following in your ~/.vimrc.

" Change the spell file to a custom path.
" It must end in '.{encoding}.add'.
:set spellfile=~/.vim/myspellfile.utf-8.add

Conclusion and Git

You can find an example of the above settings in my ~/.vimrc. You may also be interested in my spell file, and the short bash script I am using to install my dot files on a new machine.

tags: vim