Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Spell checking

#19

Run time:

Using a spell checker is a good habit to get into. In this episode, I demonstrate how to use Vim’s built in spell checking feature.

Shownotes

Spell checking is enabled by running :set spell. I like to be able to quickly toggle spell checking on and off, so I keep the following in my .vimrc:

" Toggle spell checking on and off with `,s`
let mapleader = ","
nmap <silent> <leader>s :set spell!<CR>

" Set region to British English
set spelllang=en_gb

The default spelllang is en, which includes all regions of English. In the example above, I run set spelllang=en_gb, which sets the region to British English. The available regions for the English language are:

  • en - all regions
  • en_au - Australia
  • en_ca - Canada
  • en_gb - Great Britain
  • en_nz - New Zealand
  • en_us - USA

When the region is set to British English, American spellings (e.g. ‘color’) are highlighted as regional variations, rather than being marked as misspellings.

The value of spelllang can be set locally to each buffer. This means it is possible to have several documents open at once, and for each to have their own spelling dictionary. If you would prefer to set the spelllang to the same value for all documents, you can run one of the following:

:windo set spelllang=en_us
:bufdo set spelllang=en_us

The first of these will set the spelling dictionary for all windows in the current tabpage. The second one will apply the spelling dictionary to all open buffers.

Usage

You can advance through the highlighted spelling errors with the ]s command, or you can move through them backwards with the [s command.

When the cursor is on a misspelled word, you can bring up a list of suggested corrections with the command z=. The prompt at the bottom of the screen advises you to enter the number of the word you want to use in place of the misspelled word, then hit enter. This takes you back to your document, with the correction applied.

If you prepend the z= command with a count, it will take that word from the list of suggested corrections without even showing you the list. So if you are confident that the first suggestion is the one you want, you could instead run 1z=.

Adding and removing words to spellfile

By default, Vim will load a spellfile from the location:

~/.vim/spell/LL.EEE.add

Where LL is the language and EEE is the encoding of the file in the active window. For example, if you are editing a file whose encoding is UTF-8, with spelllang set to en_us then Vim will look for a spell file at ~/.vim/spell/en.utf-8.add

If you don’t want to correct a word, you can add it to the spellfile with the zg command. You can also remove a word from the spelling dictionary with the zw command. If you change your mind, each of these commands can be reverted with the undo commands zug and zuw, respectively.

Spelling dictionaries for other languages

Out of the box, Vim comes with a spelling dictionary for the English language. If you want to spell check another language, you have to first install the spell file for it. Here is a good article on creating a spell file for Vim.

Further Reading

Comments

Browse similar content


Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..