I've turned on spellcheck with

:set spell spelllang=en_us

I can now correct the spelling of a word by typing 1z= (that is, choose the 1st spelling correction (z=)).

How can I make this automatically run on the previous word whenever I type a space, without any delays? It also tries to "fix" words that are already spelled correctly (which is why I can't do something like inoremap <space> <esc>mmb1z=`ma<space>), so I only want to correct every word I type if it's misspelled.

Is there any way to autocorrect misspelled words in Vim?

  • 2
    See this – Arturo Volpe Feb 3 '15 at 18:07
  • 1
    @ArturoVolpe That seems like a legitimate answer to the question. – bsmith89 Feb 3 '15 at 23:28
  • could you combine the previous error '[s' in a remap? I would add as an answer but don't know how to do the remap so I'll leave that for someone else. – Nick J Adams Feb 4 '15 at 14:42

I would recommend against automatically fixing every word that is marked wrong, for two reasons.

  • First, you'll get a lot of false positives on things you meant to type, especially in more technical writing. You certainly can (and perhaps should) add these two your dictionary so that they're recognized and suggested. However, if you don't notice that they're autocorrected, they could slip through to a final version of a document—or worse, if you're writing code or a configuration file (e.g., changing xmin to min would make the graph look totally wrong). Comments can also become weird (e.g., I had "Label and nullary instruction" changed to "Label and nullify instruction," which changes the meaning (a) from a noun phrase to an imperative, and (b) to meaning something completely different.)
  • Second, the autocorrect is pretty poor on short words. In my experience, it tends to complete to the closest word, which, with, e.g., three-letter words, can be totally different from what you mean. If you mean to type of but accidentally type fo, it will be corrected to few—not what you wanted! Again, these can be hard to catch in cursory revisions.

With that in mind, I think your real goal is to be able to quickly recognize and fix typographical errors.

I use the following mappings to accomplish this:

" Go back to last misspelled word and pick first suggestion.
inoremap <C-L> <C-G>u<Esc>[s1z=`]a<C-G>u

" Select last misspelled word (typing will edit).
nnoremap <C-K> <Esc>[sve<C-G>
inoremap <C-K> <Esc>[sve<C-G>
snoremap <C-K> <Esc>b[sviw<C-G>

When I'm typing body text, any misspelled words will be underlined, per my highlighting settings. If I see that the word is relatively long and it's a simple typo, then I can figure that spell will probably fix it correctly, so I hit <C-L> and just keep typing. If it's a shorter word or I anticipate that it will get it wrong, I can press <C-K> and adjust it myself. Finally, if spell unexpectedly corrects to the wrong thing, I've set an undo marker with <C-G>u, so I can just <Esc>ucW to change it manually This makes for a very quick workflow most of the time.

By chance, I have written a plugin that does exactly this. It is called Vim You, Autocorrect!

After installing, type :EnableAutocorrect to switch it on in any buffer, and you will soon see that wchargin's scaremongering is quite unfounded.

  • Note your comment on wchargin's answer got flagged as rude, I edited it to remove the "Poppycock!" and dismissed the flag. Not a big deal, just FYI. – Martin Tournoij Dec 6 at 8:35
  • @MartinTournoij It's fine. Lots of people on reddit also didn't get the joke. :) (Thanks for the heads up, though.) – Rich Dec 6 at 23:14

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.