Skip to content

Instantly share code, notes, and snippets.

@Konfekt
Last active April 25, 2025 07:20
Show Gist options
  • Save Konfekt/9f141a8e21a864d43181dbb712b4ea90 to your computer and use it in GitHub Desktop.
Save Konfekt/9f141a8e21a864d43181dbb712b4ea90 to your computer and use it in GitHub Desktop.
fix last spelling mistake by Ctrl-Q; press repeatedly to cycle through suggestions; proceed to penultimate mistake after a one-second timeout
" Fix last spelling mistake by Ctrl-Q;
" press repeatedly to cycle through suggestions;
" proceed to penultimate mistake after a one-second timeout.
inoremap <silent><expr> <c-q> &l:spell ? "<c-g>u<c-o>:<c-u>call FixLastSpellingError()<cr><c-g>u" : "<c-q>"
function! s:FixLastSpellingError(count) abort
let l:pos = getpos('.')[1:3]
let l:len0 = len(getline('.'))
execute 'normal!' '[s' .. a:count .. 'z='
let l:len1 = len(getline('.'))
let l:offset = l:len1 - l:len0
let l:pos[1] = max([l:pos[1] + l:offset, 0])
call cursor(l:pos)
endfunction
function! s:SpellTimerCB(timer) abort
let s:spell_count = 0
let s:spell_timer = 0
let &l:undolevels = get(b:, 'undolevels', &g:undolevels)
unlet! b:undolevels
endfunction
call s:SpellTimerCB(0)
function! FixLastSpellingErrorTimed() abort
" restart timeout
if s:spell_timer != 0
call timer_stop(s:spell_timer)
undo
normal! g``
else
let b:undolevels = &l:undolevels
let &l:undolevels = 1
endif
let s:spell_timer = timer_start(1000, function('s:SpellTimerCB'))
" Bump counter and apply suggestion
let s:spell_count += 1
call s:FixLastSpellingError(s:spell_count)
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment