Thomas' Tech Tips

Use spell-checking in Vim

29 October 2023 - Thomas Damgaard

One often overlooked feature in Vim is its powerful spell checking capabilities.

When spell checking is enabled, misspelled words will be highlighted, making them easy to spot.

Enabling Spell Checking in Vim

To begin using spell checking, we need to enable it

:set spell spelllang=en_us

This enables spell checking and sets the language to American English.

To disable spell checking, use:

:set nospell

Multiple languages support

By default, Vim uses American English for its spell check. However, Vim is quite versatile and can check spelling for multiple languages.

If you need spellchecking on multiple languages, spelllang can be a comma separated list:

set spelllang=en_us,nl,medical

Spell checking only one buffer

If you want to enable spellchecking on only the buffer in focus

:setlocal spell spelllang=en_us

this is handy if you have both text and code open in different files.

Finding misspelled words

In text mode Vim, it will highlight the entire word that is spelled wrong.

In GVim, it will mark the misspelled words using a squiggly line

Getting word suggestions

When Vim flags a word as misspelled you might want suggestions on how to correct it. In order to get suggestions for a misspelled word, move the cursor to the flagged word and press z=.

This will show a list of suggested words. Choose the correct word from the list and press Enter.

Adding words to Vim’s dictionary

Sometimes Vim might flag words that are correct, especially technical terms. Instead of ignoring these every time, you can add them to Vim’s dictionary:

To add a word to the dictionary, move the cursor to the word and type zg. This will add the word permanently to the dictionary.

Sometimes, you only want to temporarily ignore a word. For this, Vim has the concept of session dictionaries that are only used for the current Vim session. These dictionaries are deleted when exiting Vim.

To add a word temporarily to the session dictionary, move the cursor to the word and type zG.

If you mistakenly add a word, you can undo this by moving over the word and typing zuG for session-specific additions or zug for permanent additions.

Ignoring words

There might be instances where you want Vim to ignore certain words just for the current file or session. For this, position the cursor on the word and press zw. If you want to un-ignore the word, simply press zuw.

Custom spell files

If you want to use a custom spell file, you can set one up and then use :mkspell to generate a spellfile.

Then, you can set Vim to use your custom spellfile using the set spellfile=... command.

Filed under: howto, tips, vim

Back to article list