Tim
Tim

Reputation: 14164

How do I correct Vim spelling mistakes quicker?

My usual Vim work flow is:

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.

Upvotes: 43

Views: 15528

Answers (7)

unrealapex
unrealapex

Reputation: 629

You can press ]s and [s to cycle forward and back misspelled words in a buffer. If you are over a misspelled word in normal mode, you can press z= to see a list of spelling suggestions.

answer suggested by zonker on linux.com

Upvotes: 0

Ryan Carney
Ryan Carney

Reputation: 43

I created a plugin just for this use case https://github.com/arecarn/vim-spell-utils

It provides the insert mode mapping CTRL-A that does exactly what was asked and corrects last misspelling with 1st suggestion then returns to insert mode where you you last left off. It also accounts for changes to the line lengths due to spelling corrections while gi and `] do not.

Upvotes: 1

Benoit
Benoit

Reputation: 79243

You can use Ctrl + Y to accept an element in a popup menu. See :help complete_CTRL-Y.

Upvotes: 2

Jonas
Jonas

Reputation: 618

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

Upvotes: 44

PDug
PDug

Reputation: 1252

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

Upvotes: 36

Tim
Tim

Reputation: 14164

I fixed it with the following remap in my .vimrc.

imap <F2> <Esc>mti<C-X>s<Esc>`tla

Press F2 in insert mode to correct the last mistake and go back to insert mode where you were. It overwrites the t marker.

Upvotes: 2

Prince Goulash
Prince Goulash

Reputation: 15735

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

Upvotes: 13

Related Questions