This vim binding will make <C-l> correct the previous spelling mistake:

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

How do I modify it to make it correct the previous mistake on the current line only?

1 Answer 1

inoremap <C-l> <c-g>u<Esc>:call <sid>fixLineSpellError()<cr>`]a<c-g>u

function! s:fixLineSpellError()
  " get current line number
  let lnum = line('.')
  " find last misspelled word before cursor
  normal! [s
  " do nothing if line changed
  if lnum != line('.') | return | endif
  " fix spell error if line doesn't change
  normal! 1z=

Map break down:

  • <c-g>u<Esc> break undo sequence, switch to normal mode.
  • :call <sid>fixLineSpellError()<cr> call a script scope function to fix current line spell error.
  • `]a move cursor back to the end of last change, start to append.
  • <c-g>u break undo sequence.
  • Works as intended and well explained, thanks!
    – Kevin Liu
    Commented Apr 30, 2019 at 13:55

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.