Spelling And Grammar With Vim
So I've found myself recently writing quite a lot of things which are not code (or not only code). I'm talking about README files, and documentation files, articles, and the Vim book I'm working on right now.
Obviously I'm doing that in Vim. But as a non-native English-speaker, sometimes I make mistakes (well, OK, quite often I make mistakes).
So has Vim something to offer to facilitate that problem?
It does. Quite a lot, actually.
Vim has a built-in spell checker. You can turn it on by setting
spell on (see
Now it will check all the words in the current buffer and highlight the ones that are misspelled. You can move from one incorrect word to another with
When you set your cursor on an incorrect word, you can fix it immediately by selecting a word from a list of suggestions.
z= opens up a list of suggestions.
From there, you need to select a word from a list to replace the current misspelled one with. Just put a number and press
In the Insert mode the same list can be seen by invoking
C-X, C-K (dictionary completion).
Words are ordered by proximity. Most likely, you will want to take the first one. The shortcut for that is
If Vim doesn't know the word, but you think it's a normal one, you can add it to the list of good words with
The chances are though that you don't want to turn the spell on for regular source code files. You can, of course, only turn it on for specific types:
autocmd FileType markdown setlocal spell
Dictionary And Thesaurus
So how does Vim know which spelling is correct? Vim looks it up in a dictionary file. Where is that file located, depends on the system. You can check by:
In OSX, for instance, that's going to be
Apart from the spelling dictionary, Vim supports a thesaurus which it can use to provide you with a list of synonyms for any particular word.
In order for it to work, you need to provide a thesaurus file yourself. Those are simple plain text files, each line of which has several words with the same meaning separated by space. You can download this one or this one.
And then you need to tell Vim where you've put it:
" .vimrc set thesaurus+=/Users/yanis/thesaurus/words.txt
In order to invoke the thesaurus completion in insert mode, press
So far, we've only used Vim built-in capabilities. But we can do more if you an external tool that is specifically built for checking the grammar.
That external tool is LanguageTool. You can download it from the website, or if you're on OSX, you can install with brew:
brew install languagetool
Now, if you use the ALE linter plugin, you don't have to do anything else. It's smart enough to pick it up for you and run on the markdown files.
Otherwise, you might want to install one of the available integration plugins:
(But seriously, why aren't you using ALE ?)
Another cool program is proselint. It not so much as for grammar checking but instead it gives you some advice on how to improve your prose. And that advice come from the notorious writers of the past.
From their website:
You’ll be guided by advice inspired by Bryan Garner, David Foster Wallace, Chuck Palahniuk, Steve Pinker, Mary Norris, Mark Twain, Elmore Leonard, George Orwell, Matthew Butterick, William Strunk, E.B. White, Philip Corbett, Ernest Gowers, and the editorial staff of the world’s finest literary magazines and newspapers, among others.
So definitely check it out, plus it is also supported by ALE.
Where To Go Next?
I guess my Vim setup for writing is quite good. It could be better though, if Grammarly finally made an API for their brilliant service.
If you know any other good tools / Vim plugins for spelling and grammar, please let me know.
So, where's next?
- You can also follow me on Twitter for more articles like this