22

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?

3
  • 2
    See this Commented Feb 3, 2015 at 18:07
  • 1
    @ArturoVolpe That seems like a legitimate answer to the question.
    – bsmith89
    Commented Feb 3, 2015 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. Commented Feb 4, 2015 at 14:42

2 Answers 2

11

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.

2
  • @Rich Please explain why fixing every word is an excellent idea.
    – nilon
    Commented Apr 9, 2021 at 16:32
  • @Rich It may be a positive addition to add an explanation in your own answer.
    – nilon
    Commented Apr 11, 2021 at 21:27
7

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.

6
  • 1
    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. Commented Dec 6, 2018 at 8:35
  • 2
    @MartinTournoij It's fine. Lots of people on reddit also didn't get the joke. :) (Thanks for the heads up, though.)
    – Rich
    Commented Dec 6, 2018 at 23:14
  • mr Rich which package i want to install and type :EnableAutoCorrect i didnt understand Commented Oct 25, 2019 at 10:10
  • @MPCreations Do you use a plugin manager?
    – Rich
    Commented Oct 25, 2019 at 11:02
  • 1
    For what it's worth, I think that this is amazing.
    – wchargin
    Commented Apr 9, 2021 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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