One of the best new features in Vim 7 is built-in spell-check (although plugins for spellcheck have been available earlier). With spell-check, I can now use Vim exclusively for editing text formatted files – latex, blog entries and the like – in addition to code. Here’s a quick look at the various spell checking features.
Turning on spellcheck
Spellcheck can be turned on/off with the following commands
:setlocal spell spelllang=en_gb
:setlocal nospell
en_gb can of course be replaced by your language of choice (en_us for example). Vim 7 supports spell-check for more than 50 languages, and the dictionaries are stored in $VIMRUNTIME/spell
. I’ve mapped this to the F6 and F7 keys by adding the following lines to the $HOME/.vimrc
file:
map <F6> <Esc>:setlocal spell spelllang=en_gb<CR>
map <F7> <Esc>:setlocal nospell<CR>
[via]
Once spell-check is turned on, words with spelling errors will be highlighted. You can move to the next and previous misspelled words by typing ]s
and [s
respectively, in normal mode. If the cursor is on a misspelled word, z=
shows suggestions and zg
adds the word to the dictionary. zug
performs an undo to the dictionary addition.
[via]
Highlighting spelling errors
You can customize the way misspelled words are highlighted by editing your colorscheme file. Vim recognizes four categories of misspelled words – SpellBad (for words not recognized), SpellCap (words which should be spelled with a capital), SpellRare (for rare words, I’ve no idea what the logic is here) and SpellLocal (for words that belong to the same language in a localization different from the current one). The last category is especially useful – for example in UK English, the word “analyze” is not shown as a SpellBad error (as it is in MS Word), but as a SpellLocal error, since it exists in the US English dictionary. Looking inside Vim’s dictionary directory, I note that there is an overall “english.ascii.spl” file and and “en” directory lists files such as “en_GB.diff” and “en_US.diff” which allows this feature, as well as saves space by not having the large common set of English words duplicated for five localizations. Neat!
But I digress. You can customize the highlight for any category by adding in the appropriate categories and their keywords to your colorscheme file (usually located in $VIMRUNTIME/colors). My colorscheme file has the following lines:
hi SpellBad term=reverse ctermfg=white ctermbg=darkred guifg=#ffffff guibg=#7f0000 gui=underline
hi SpellCap guifg=#ffffff guibg=#7f007f
hi SpellRare guifg=#ffffff guibg=#00007f gui=underline
hi SpellLocal term=reverse ctermfg=black ctermbg=darkgreen guifg=#ffffff guibg=#7f0000 gui=underline
Linux.com reported problems while using spell-check with syntax highlighting, but I experienced no such issues. Below are screenshots of Vim7 with my homepage source with spell-check disabled and enabled (click for large versions).
[Update: It seems vi has intelligent spell-checking depending on syntax. In a C++ file, for example, enabling spell checking ONLY applies spell-checking to strings and comments! How cool is that!]
(HTML syntax highlighting with no spell-check)
(HTML syntax highlighting with spell-check enabled, the names “Anshul”, “Nigham” for example are highlighted with red background)
8 Comments