Vim Tip #11: Spelling


tags: Vim

Vim Tips

There's a command to turn spelling on and off: :set invspell. In NeoVim the first time you do this it may (and I think this is distro-dependent?) ask to download a dictionary. In general, spelling won't be applied to "technical" files, like code - but should still apply to comments. But I find that comments tend to involve so many technical terms that the "misspelled word" markers can get fantastically annoying. So for me, :set invspell<CR> is mapped to <leader>s and I use it frequently to turn spelling on and off (see Map Leader and Normal mode mappings).

There are a number of useful commands for spelling. To go to the next misspelled word, press ]s (to go to the previous one, press [s). When you're on the highlighted misspelled word, press z= to get suggestions (it'll tell you what you can do from there).

To add a word to your dictionary, press zg. I've added words to your dictionary by mistake on plenty of occasions: use zug to remove a word that's in your dictionary.

Extra tip: Your dictionary is probably: ~/.config/nvim/spell/en.utf-8.add (if you're using NeoVim, sorry, not sure where it is for regular Vim) - literally a list of all the words you've added (mine includes words like 'USB,' 'Postgres,' 'Laphroaig,' and 'ansible'). But there's another file: ~/config/nvim/spell/en.utf-8.add.spl. This is, as I understand it, a binary compilation of those added words that's updated automatically when you add words. If you do something like save your Vim config in a git repo (I do), you may run into the problem that the plain text list of words gets out of sync with the binary. Fixing this is UGLY. Open a new buffer or tab, then :edit <path-to-spellfile> (the human-readable dictionary). With that file still open, run :mkspell! % (in Vim, % is the contents of the current buffer).

Again, this isn't something most people end up tinkering with. But I have, so who knows - you may too.