Home Vim How To Use Spell Check Feature In Vim Text Editor

How To Use Spell Check Feature In Vim Text Editor

Enable And Disable Spell Check Feature In Vim Editor

By sk
Published: Last Updated on 16K views

Vim is one of the best, most popular, feature-rich and powerful text editor. Vim ships with a 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.

Before getting started, let us learn how spell check works in Vim.

How spell check works in Vim editor

  • The boolean option spell determines whether spell checking is enabled or not. It is a window local option so if it is desired to only enable it for a specific buffer use :setlocal spell spelllang=en_us instead for example.
  • spelllang specifies which spell file Vim will look at (also determined by spellfile option). The locations in runtimepath are searched in the order they appear and is stopped at the first search. If you don't have a local spell file in .vim/spell directory this is typically somewhere like /usr/share/vim/vim81/spell for example. These are binary files and have the form LL.EEE.spl where LL is the language (such as en for English) and EEE is the encoding (utf-8 for example).
  • Vim recognizes mainly four types of spelling "errors". These are: a) words not recognized (SpellBad) b) words not capitalized (SpellCap) c) Rare words (SpellRare) and d) Wrong spelling for selected region (SpellLocal) for example using "colour" with US English. Each of these is highlighted differently. The ]s and [s commands go to next misspelled word in all four categories but ]S and [S only for the first two categories.
  • There are two locations where words you add are stored. Using the commands zg and zw stores them in the first entry in spellfile. These are local to the buffer since spellfile is buffer-local. The commands zG and zW stores the words in internal wordlist which is global for all the buffer where spell is set. However, these are NOT persistent and are lost when Vim exits.
  • Apart from z= command to load spell suggestions one can also use Ctrl + x s in insert mode to load spell suggestions. This is helpful when you don't know the spelling and so type it halfway to load the suggestions list. After you make a correction with z= you can use :spellrepall command to apply the same change to all words that match (may want to map it some key in normal mode).
  • You can add your own suggestions for commonly misspelled words in a local file (for example .vim/sugg). The variable spellsuggest determines which will be used first. If this file is the first entry (like set spellsuggest=file:~/.vim/sugg,best) then your suggestions will be shown first. The entries here must have two columns separated by slash.
  • Finally, you can use your own spell file. For example .dic and .aff files used in many other packages can be converted into vim .spl files using the command :mkspell outfile infile. Then use that file by specifying it in spelllang.

See Vim help (:help spell.txt) for more details. Special thanks to /r/random_cynic for this detailed inputs on Reddit forum.

Enable Spell Check Feature In Vim Editor

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

:set spell
Enable Spell Check feature in Vim
Enable Spell Check feature in Vim

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

Find 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.

Find spelling mistakes in Vim
Find spelling mistakes in Vim

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).
  • [s - 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.
  • [S - Similar to "[s" but search backwards.

Correct spelling mistakes, typos

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.

Correct spelling mistakes in Vim
Correct spelling mistakes in Vim

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.

Fix spelling mistakes and typos in Vim
Fix spelling mistakes and typos in Vim

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.

Add exceptions
Add exceptions

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 Editor

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
Disable Spell Check feature in Vim
Disable Spell Check feature in Vim

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

Conclusion

Making spelling mistakes while coding or typing is common and inevitable. Fortunately, Vim editor has a built-in spell check feature that is used to find and correct spelling mistakes and typos easily.

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

You May Also Like

3 comments

Rafael Moczalla December 1, 2019 - 4:54 pm

Hello!

In the section “Add words to Spellfile” you wrote “Now, any words which are not in Dictionary, locate the misspelled word (use z=) and type zg.” I guess you mean “Now, any words which are not in Dictionary, locate the misspelled word (use ” ]s “) and type zg.”

Reply
sk December 2, 2019 - 1:03 pm

Good catch. Corrected now. Thanks for the heads up.

Reply
Larry Dighera January 30, 2022 - 12:19 am

Thank you for your very professional and complete tutorial. Very much appreciated.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More