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
spelldetermines 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_usinstead for example.
spelllangspecifies which spell file Vim will look at (also determined by
spellfileoption). The locations in
runtimepathare searched in the order they appear and is stopped at the first search. If you don't have a local spell file in
.vim/spelldirectory this is typically somewhere like
/usr/share/vim/vim81/spellfor example. These are binary files and have the form
LLis 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
[scommands go to next misspelled word in all four categories but
[Sonly for the first two categories.
- There are two locations where words you add are stored. Using the commands
zwstores them in the first entry in
spellfile. These are local to the buffer since
spellfileis buffer-local. The commands
zWstores 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 sin 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
:spellrepallcommand 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
spellsuggestdetermines 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
.afffiles used in many other packages can be converted into vim
.splfiles using the command
:mkspell outfile infile. Then use that file by specifying it in
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:
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.
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.
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:
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 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:
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:
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
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.