ProbableOdyssey | Blake Cook

Vim spellcheck mappings I use

· 2 min read · 305 words

Using :set spell gives basic spellcheck for what you write in Vim and Neovim. It’s far from perfect — and there’s other plugins that probably do a better job of this — but I’ve found this is sufficient for my usage.

I’ve adopted using <C-s> as a “leader” for interacting with spellchecking so I can accept corrections, undo bad corrections, and add exceptions to the previous misspelled word without needing to navigate my cursor to the word in question, and also to make actions undoable with u.

These mappings behave the same in insert mode and

<C-s><C-s> : Correct last misspelled word, retaining current curor position
<C-s><C-x> : Undo last correction, pop-up selection for selecting other correction
<C-s><C-a> : Exempt last misspelled word, retaining current curor position
<C-s><C-d> : Undo last exemption

Here’s the snippets for these for init.lua / vimrc:

vim.keymap.set('i', '<C-s><C-s>', '<C-g>u<Esc>[s1z=`]i<C-g>u', { remap = false }),
vim.keymap.set('i', '<C-s><C-x>', '<C-r>u<C-r>z=', { remap = true }),
vim.keymap.set('i', '<C-s><C-a>', '<Esc>[szg`]i', { remap = false }),
vim.keymap.set('i', '<C-s><C-d>', '<C-r>zug', { remap = false }),

vim.keymap.set('n', '<C-s><C-s>', 'i<C-g>u<Esc>[s1z=`]i<C-g>u<Esc>', { remap = false }),
vim.keymap.set('n', '<C-s><C-x>', 'uz=', { remap = true }),
vim.keymap.set('n', '<C-s><C-a>', '[szg`]', { remap = false }),
vim.keymap.set('n', '<C-s><C-d>', 'zug', { remap = false }),
inoremap <C-s><C-s> <C-g>u<Esc>[s1z=`]i<C-g>u
imap <C-s><C-x> <C-r>u<C-r>z=
inoremap <C-s><C-a> <Esc>[szg`]i
inoremap <C-s><C-d> <C-r>zug

nnoremap <C-s><C-s> i<C-g>u<Esc>[s1z=`]i<C-g>u<Esc>
nmap <C-s><C-x> uz=
nnoremap <C-s><C-a> [szg`]
nnoremap <C-s><C-d> zug

As a bonus, map z= to a nicer UI for selecting corrections. I use fzf-lua in nvim with this configuration:

vim.keymap.set('n', 'z=', [[v:count ? v:count . 'z=' : ':FzfLua spell_suggest<CR>']], { expr = true })

So my mappings (and any invocation of z=) pops up a fuzzy spellcheck suggestion list in the place of the last incorrect word.

See :help spell for more details on how this system works, you might find other mappings that work more for you.

Reply to this post by email blZake@proZbableodyssey.blog (remove Z characters) ↪

Comments

Leave a comment

Markdown is supported. Your email is private and only used if you'd like a reply.