Spell checking in Vim

I have come to find spell checking in Vim to be very useful when writing documentation or commit messages. I had previously enabled spell checking for certain file types but I have now set it globally in my .vimrc:

set spell spelllang=en_au

The spelllang=en_au marks the language and region to use as English and Australia respectively.1

Using spell check

With spell check enabled, words are highlighted using the colour scheme currently used in Vim. As an example, misspelt words may appear as red, spelling suggestions as green, and so on.

The ]s and [s keys can be used to navigate forward and backward through misspelled words respectively.

Type z= when the cursor is on a highlighted word to open up a list of suggestions. Type the number corresponding with the replacement word and press Enter to make the correction.

1z= can be used to insert the first suggestion without opening the list.

Marking words as correct or incorrect

Words like "JSON" are usually marked as spelling errors. Typing zg when the cursor is on a highlighted word will add it to Vim's dictionary.

If Vim marks a word as correct when it isn't, typing zw will remove it from the dictionary.

Setting a dictionary

Vim's internal dictionary is not perfect and may not align with your writing style. The dictionary can fortunately be overridden with a local dictionary. A local dictionary is automatically created whenever a word is added or ignored.

The location of this file is in ~/.vim/spell/, and is named based on the defined language, e.g. ~/.vim/spell/en.utf-8.add.

A custom dictionary can be created and defined like so:

set spell spelllang=custom-file.spl

Where custom-file.spl is the path to the custom dictionary.

It should be noted that the *.spl extension is a compressed format. To generate a *.spl file, the mkspell command can be used like so:

:mkspell custom-file.add

The mkspell command converts an .add format to a .spl format for Vim. More information on the mkspell command can be found within the Vim documentation.


Works cited

"Using Spell Checking in Vim." Linux.com | The source for Linux information, 1 Sep. 2010, https://www.linux.com/learn/using-spell-checking-vim/. Accessed 17 December 2017.

"Vim Documentation: Spell." Vim Documentation, 1 Feb. 2011, http://vimdoc.sourceforge.net/htmldoc/spell.html. Accessed 23 December 2017.

Footnotes


  1. A list of language codes can be found on the Library of Congress' website. Specifically, the ISO 639-1 language code should be used.