Skip to content

Spell-check in Vim 7

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!]

Vim7 with HTML syntax and spellcheck OFF

(HTML syntax highlighting with no spell-check)

Vim7 with HTML syntax and spellcheck ON

(HTML syntax highlighting with spell-check enabled, the names "Anshul", "Nigham" for example are highlighted with red background)

8 Comments

  1. dave wrote:

    Many thanks I have been through vast chunks of the internet and many documents to find the correct magik to apply to the spilling issue in vim. No more will my mail be badly spilt.

    Thanks

    Tuesday, December 12, 2006 at 2:08 am | Permalink
  2. Dirk Gently wrote:

    Great tip, I’ve been thinking about “How does vim look spellings.” Unfortunately this could be logical.

    Wednesday, February 27, 2008 at 10:43 am | Permalink
  3. Ali wrote:

    God bless you. I was looking for that for a while.

    Monday, February 9, 2009 at 12:01 am | Permalink
  4. leipie wrote:

    Cool!

    I was looking for this. If you set up your local right you can just type “:set spell” and “:set nospell” for enabling and disabling spell checking. (I am using Vim 7.2)

    Thnx ;)

    Wednesday, March 4, 2009 at 11:45 pm | Permalink
  5. chris wrote:

    Thanks for this was handy +. (I’d been assuming for six months that Vim didn’t have a spell checker!)

    Wednesday, September 2, 2009 at 6:01 pm | Permalink
  6. Monsterslayer wrote:

    ;-) taking up where Chris has put the exclamation mark, I had been assuming for over 10 years, that Vim has a spell checker but never had use for it.

    Thank you for not disappointing me. I can now translate web-contents from one language to another with much more ease and even: FUN!

    Thursday, July 7, 2011 at 10:49 pm | Permalink
  7. Maybell wrote:

    Hi there to all, it’s actually a nice for me to go to see this website, it includes helpful Information.

    Saturday, April 27, 2013 at 10:14 pm | Permalink
  8. Oh my goodness! Amazing article dude! Thank you so much, However I am encountering troubles with
    your RSS. I don’t understand the reason why I cannot subscribe to it. Is there anybody having similar RSS issues? Anybody who knows the solution can you kindly respond? Thanks!!

    Thursday, June 20, 2013 at 7:20 am | Permalink

Post a Comment

Your email is never published nor shared.