How do I correct Vim spelling mistakes quicker?

Tim picture Tim · Mar 15, 2011 · Viewed 8.4k times · Source

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.

Answer

Jonas picture Jonas · May 10, 2013

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).