Vim is one of the best, most popular, feature-rich and powerful text editor. There is no doubt about that. It has lot of features. For example, the beginners can easily learn the basics of Vim from the built-in help-section by running "vimtutor" command in Terminal. Learning Vim is worth the effort. Today, in this guide, we will be discussing one of the most-widely used feature called "spell check" in Vim editor. If you're a programmer who edits lots of text, then "spell check" feature might be quite useful. It helps you to avoid embarrassing spelling mistakes/typos while editing text files using Vim.

Use Spell Check Feature In Vim Text Editor

Enable Spell Check

To enable Spell Check feature in Vim, open it and type the following from Command Mode:

:set spell

Remember you need to type the above command inside Vim session, not in the Terminal window.

Find and correct spelling mistakes, typos

Now, go to "Insert Mode" (type "i" to switch to Insert Mode from Command mode) and type any misspelled letters. Vim will instantly highlight the misspelled words.

As you see in the above output, I have typed "Welcome to Linux learng sesion" instead of "Welcome to Linux learning session" and vim is highlighting the misspelled words "learng" and "sesion" in red color.

Now, go back to Command mode by simply pressing the ESC key.

You can navigate through the misspelled words by typing any one of the following letters:

]s - Find the misspelled word after the cursor (Forward search).

- Find the misspelled word after the cursor (Forward search). [s - Find the misspelled word before the cursor (Backward search).

- Find the misspelled word before the cursor (Backward search). ]S (Note the capital "S") - Similar to "]s" but only stop at bad words, not at rare words or words for another region.

(Note the capital "S") - Similar to "]s" but only stop at bad words, not at rare words or words for another region. [S - Similar to "[s" but search backwards.

After you located the misspelled word, type z= to find suggestions for the that particular word. Here, Vim shows me the list of suggestions for the misspelled word "learng". Pick the correct word from the list by typing the respective number and press ENTER key to update the misspelled word with right one.

As you see in the above screenshot, I entered number 13 to replace the misspelled word "learng" with correct word "learning. Vim immediately updated the correct word in the input after I hit ENTER key.

Similarly, correct all spelling mistakes in your text as described above. Once you've corrected all mistakes type :wq to save the changes and quit Vim editor.

Please remember - we can only check the spelling mistakes, not the grammar mistakes.

Set Spell language

By default, Vim uses "en" (all regions of English) to check for spelling mistakes. We can also choose our own spell language. For instance, to set US region English, type the following from the Command mode in Vim editor:

:set spell spelllang=en_us

The list of all available regions for the English language is:

en - all regions

en_au - Australia

en_ca - Canada

en_gb - Great Britain

en_nz - New Zealand

en_us – USA

Add words to Spellfile

Some times you might want to add some words as exceptions, for example your name, a command, Email etc. In such cases, you can add those specific words to the Spellefile. This file contains all exceptions.

Make sure you have ~/.vim/spell/ directory in your system. If it is not, create one:

$ mkdir -p ~/.vim/spell/

Then, set spellfile using:

:set spellfile=~/.vim/spell/en.utf-8.add

Now, locate the misspelled word using either [s or ]s and type zg. It will add the word under the cursor as good word in spellfile. i.e adds the words to your own dictionary.

To undo this add (remove the word from spellfile), just use zug. To mark the mispelled word, type zw. To undo this action, use zuw.

Disable Spell Check in Vim

Vim will highlight all misspelled and words which are not available in the Dictionary. Some times, you find this annoying while writing code or a README file that contains a lot of words which are not available in the Dictionary. In such cases, you can disable the "Spell Check" feature by simply typing the following command:

:set nospell

That's it. Now, Vim will highlight nothing. You can enable the spell check feature at any time by running ":set spell" from the Command mode in Vim.

Vim has more built-in help pages for Spell Check feature. To know more about spell check feature, run:

:help spell

You also refer individual help section for every options, for example:

:help ]s :help [s :help z= :help zg :help zug :help zw :help zuw :help spelllang :help spellfile :help spellfile-cleanup

Also read:

And, that's all for now. Hope this helps.