Spell checking in Vim

Vim

Mark Twain has been quoted as saying that he respected a person who could spell a word more than one way. Unfortunately, Twain’s enthusiasm for creative spelling isn’t widely shared today, at least in the professional world. (1)

You know Vim already from a lot of features, including Macros, Reg Ex, or code formatting. Now, let’s add spell checking:

Support for spell checking was added in Vim 7 and therefore it is really easy to activate:

:set spell spelllang=en_us

Afterwards, the misspelled words are highlighted red. To move to a misspelled word, use [s (backwards) and ]s (forwards). Once the cursor is on the word, Vim will suggest a list of alternatives, by typing (in command mode):

z=

You choose between these alternatives by typing the corresponding number.

Or, for example if you want to automatically check Markdown files (and only those), add this to your ~/.vimrc:

autocmd FileType md,markdown set spell spelllang=en_us

(Yes, spell checking for code could become very nasty…).

The support for English is included, but other languages can be added easily:

:set spell spelllang=es_es   # for Spanish
:set spell spelllang=de_de   # for German

When using :setlocal instead of :set, spell checking is activated only for the local tab/buffer.

In case the language is activated the first time, Vim asks you if it should be installed:

:setlocal spell spelllang=es_es
Cannot find spell file for "es" in utf-8
Do you want me to try downloading it?
# Type Y

Downloading es.utf-8.spl...
:!curl 'http://ftp.vim.org/pub/vim/runtime/spell/es.utf-8.spl' -o '/tmp/vm1oj9D/47.spl'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
"/tmp/vm1oj9D/47.spl" [noeol] 1346L, 601018C
In which directory do you want to write the file:
1. /home/john-doe/.vim/spell
# Type 1

"~/.vim/spell/es.utf-8.spl" [New] 1346L, 601019C written
Do you want me to try getting the .sug file?
This will improve making suggestions for spelling mistakes,
but it uses quite a bit of memory.
# Type Y

Downloading es.utf-8.sug...
:!curl 'http://ftp.vim.org/pub/vim/runtime/spell/es.utf-8.sug' -o '/tmp/vm1oj9D/47.sug'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
"~/.vim/spell/es.utf-8.sug" [New] 6292L, 1912378C written
Press ENTER or type command to continue

quite a bit of memory means in this case 2MB.

Deactivate the spell check by simply typing:

:set nospell

More details can be found on the blog of linux.com (1).

So, happy writing!

You have another opinion?
Great! Then let's reduce the fallacy together!


Why are there no comments?