Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I correct Vim spelling mistakes quicker?

My usual Vim work flow is:

  • In insert mode, spell something wrong.

    Vim spell

  • Press ^X s to get some suggestions.

    Vim screen

  • Press Esc to accept the first one.

After this, I'm in command mode in the middle of the line, instead of insert mode of where I was before. I could use A, but that only works if I was typing on the end of the line. Is there an alternative way? Optimally, I'd like a command that corrects the last mistake to the first suggestion without moving the cursor.

like image 688
Tim Avatar asked Mar 15 '11 13:03

Tim


People also ask

How do I correct Spelling in Vim?

Using Spellchecking To move to a misspelled word, use ]s and [s . The ]s command will move the cursor to the next misspelled word, the [s command will move the cursor back through the buffer to previous misspelled words. Just hit Enter if none of the suggestions work, or enter the number for the correct word.

How do you correct a Spelling error?

From the Review tab, click the Spelling & Grammar command. The Spelling and Grammar pane will appear on the right. For each error in your document, Word will try to offer one or more suggestions. You can select a suggestion and click Change to correct the error.


3 Answers

An improvement to PDug's answer: To make the spelling correction undoable separately from the insertions, use this:

imap <c-l> <c-g>u<Esc>[s1z=`]a<c-g>u

<c-g>u inserts an undo-break
The rest is the same.

This way, if you don't like the chosen correction, you can undo it using <Esc>u. Without the undo-breaks, this would undo the complete insertion. Note that the undo-break at the end of the mapping ensures that text added after the correction can be undone separately from the correction itself.

Also, I found it convenient to map this to CTRL+F (which is easy to reach) in both insert and normal mode like this:

imap <c-f> <c-g>u<Esc>[s1z=`]a<c-g>u
nmap <c-f> [s1z=<c-o>

This way, you can quickly fix the last error (relative to the cursor).

like image 141
Jonas Avatar answered Sep 30 '22 12:09

Jonas


This works fairly well:

imap ^L <Esc>[s1z=`]a

[s moves to the last spelling mistake
1z= chooses the first suggestion
`] move to the last insert point
a append text

like image 32
PDug Avatar answered Sep 30 '22 10:09

PDug


I can't offer an 'optimal' solution (although I suspect there is a way).

However, you can use gi to enter insert mode at the place in the file where you last left it. (help gi explains this more eloquently).

like image 9
Prince Goulash Avatar answered Sep 30 '22 11:09

Prince Goulash