Vim Spelling Guide

Tags: vim writing 

Contents

Introduction

You can enable spell checking in Vim with the command :set spell.

To enable spell check only for the current buffer, use :setlocal spell.

In Neovim, use vim.o.spell = true.

You can disable spell checking with :set nospell.

Toggle Spell Checking with a Keymap

You can bind keys to toggle between spell checking mode.

For e.g., in Vim:

nnoremap <silent> <F2> :set spell!<CR>
inoremap <silent> <F2> <C-o>:set spell!<CR>

And, in Neovim:

map('n', '<Leader>=', ':set spell!<CR>', {desc='Spell check'})

Use Autocommand

You can create an Autocommand to enable spell checking in specific file types.

For e.g., in Vim:

autocmd BufRead,BufNewFile *.txt,*.md,*.tex setlocal spell

And, in Neovim:

-- spellcheck
autocmd({'BufRead', 'BufNewFile'}, {
    pattern = { '*.txt', '*.md', '*.tex'},
    command = 'setlocal spell' } )

Specify Language and Region

Specify a comma separated word list for Vim to include in spell checking with :set spelllang or :set spl.

For e.g. to use:

  • International English:set spl=en
  • British English: :set spl=en_gb

Other available English regions include en_au, en_ca, en_nz, en_us.

To use German and Russian, use :set spl=en,de,ru

And to include East Asian characters, use :set spl=en,cjk

Highlight

Vim highlights the following mistyped words:

  1. Words not recognized (hl-SpellBad).
  2. Words not capitalised with (hl-SpellCap).
  3. Rare words with (hl-SpellRare).
  4. Wrong spellings for selected region with (hl-SpellLocal).

Go to next misspelled word after cursor with ]s.

Go to previous misspelled word before cursor with [s.

Go to next & previous misspelled words respectively, while ignoring rare and non-region words with ]S and [S.

You can use a count for them. For e.g., to go to the third misspelled word after cursor, use 3]s.

Correct spelling

Spell Suggestions

Vim displays a list of corrections (and/or alternative words) for the word under cursor with z=.

To automatically correct the word under cursor with the first suggestion, use 1z=.

To show autocompletion menu of suggestions while in INSERT mode, press CTRL-X s.

After replacing a word with z= or CTRL-X s, replace all similar words using :spellr[epall].

Suggestion Keybinding

Below is an example of a genius keybinding in Vim that I borrowed from Gilles Castel.

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

This fixes the last misspelled word by pressing CTRL-Z in Insert mode.

Breakdown of the binding:

  1. Jump to the last misspelled word [s.
  2. Replace the word with the first suggestion with 1z=.
  3. Return to initial cursor position with `]a.
  4. The <C-g>u enables us to undo the correction.

The same keybinding in Neovim:

map('i', '<C-z>', '<C-g>u<Esc>[S1z=`]a<C-g>u', {desc='Fix spelling'})

Modify Suggestions

Vim shows a staggeringly long list of suggestions.

To restrict the number of suggestions, use the spellsuggest or sps options.

For e.g.:

  • Use best method and limit suggestions to 5: set sps=best,5
  • Use fast method and limit suggestions to 10: set sps=fast,10
  • Specify a custom file for suggestions: set sps=file:~/path/to/custom/suggestions,best
    • The file should list words in the following format: incorrect/correct (for e.g., misstake/mistake).
    • Vim ignores lines without a /, and acts as comments.
  • Specify a custom function for suggestions: set sps=expr:MySuggest()
    • The function includes a list of expressions, each with a sub-list of suggestions and score. Vim then uses the score to determine which suggestion to pick. The lower the score, the better.

Abbreviations

Some people prefer abbreviations to correct frequently mistyped words.

For e.g.,

iabbrev manteinance maintenance
iabbrev adn and
iabbrev funtcion function
iabbrev retunr return

Spell File

Vim looks for spell files in the spell/ subdirectory in the runtimepath.

We can add words to the spellfile and an internal wordlist.

The internal wordlist is local to buffers, and will clear when closed.

Good and wrong words

Add word as ‘good’ to the spellfile with zg or :spe[llgood] <word>

Add word as ‘good’ to the internal wordlist with zG or :spe[llgood]! <word>

Add word as ‘wrong’ to the spellfile with zw or :spellw[rong] <word>

Add word as ‘wrong’ to the internal wordlist with zW or :spellw[rong]! <word>

Add word as ‘rare’ to the spellfile with :spellrare <word> or :spellr <word>

Undo previous operations:

  • zug, zuw, :spellundo <word>
  • zuG, zuW, :spellundo! <word>

Conclusion

Please consult the documentation for more (:help spell and :help dictionary) information.

Comment on this post with email.

Return to Top of the page.
See all Posts.
Subscribe to RSS feed.