~ views

Neovim spellcheck - quick tips


Neovim shortcut to add to spell suggestions, and a Conform+NvimLint configuration for cspell, with a custom keybinding to add the <cword> to the cspell dictionary.

Note

Apr 11, 2024

After using the built-in spellchecker for a while, I decided to try one that would throw less false positives and also help improve grammar and writing style. I’ve added a new section at the bottom to set up Vale, which checks my writing for style and grammar. When I write these articles now, a combination of WriteGood and Vale rules help me create better content!

Vale diagnostics results in a terminal

In neovim, when a word is marked as misspelled, you can add it to the dictionary by typing zg while the cursor is on the word. To correct a word from a list of suggestions, you can type z=. This is great, but what if you want to add a custom spelling suggestion? For example, I want to add the word neovim to the dictionary, but I don’t want to add it to the dictionary as neovim, I want to add it as Neovim, with a capital N.

Configuration

Here’s how you do it:

  1. Create a custom spelling suggestion file in your neovim configuration directory. I called mine ~/.config/nvim/spell/sugg. You might notice files in that folder that end in .add — the words in these files are ignored by spellcheck.

    neovim / Neovim;
    ------

    If Neovim is marked as a misspelled word, type zg to add it to the dictionary.
    Note: If the word to the right of the / is mispelled, the suggestion will be ignored by neovim. Make sure all words on the right are added as “goodwords”.

    The file should now look like this:

    Full-size neovim screenshot
    Neovim screenshot showing a custom spelling suggestion file
  2. Add the following to your vim options:

vim.opt.sps = "file:/Users/aaron/.config/nvim/spell/sugg,best"

Now, when editing a file, if neovim is marked as a misspelled word, typing

z= will show Neovim as the first suggestion.

Disabling for syntax

Another thing to note is that there is a configuration option for spellchecking syntax. Generally, we do not want to spell check function and class names, etc - the whole page would start to turn red! To ignore certain syntax, add the following to your vim options:

vim.cmd("set spell syntax=off")

Setting up Vale

Here’s how you do it:

  1. On OSX, brew install vale.
  2. Create a .vale.ini file in your project root. Many projects on github have great examples of vale configurations tailored for technical writing. Here’s mine for some inspiration. with the following content:
StylesPath = .vale/styles
MinAlertLevel = suggestion
Packages = write-good, proselint, Microsoft # `vale sync` will download these packages for you.
Vocab = Me
[*]
IgnoredScopes = code, tt, table, tr, td # ignore HTML
BlockIgnores = (?s) *({< output >}.*?{< ?/ ?output >}), \
(?s) *({< highlight .* >}.*?{< ?/ ?highlight >})[*md]
BasedOnStyles = Vale, Microsoft, write-good # also check out `proselint`
; Vale.Spelling = NO
Vale.Spelling=warning
Vale.Terms = NO
write-good.E-Prime = NO # E-prime is too aggressive and not best suited for blog or tutorial writing.
write-good.ThereIs = suggestion
# I've sen many configs that disable these, but I'm still testing them out. If i
# update my vale configuration, I can reload the file in Neovim with `:e` to
# refresh the diagnostics.
; write-good.TooWordy = NO
; write-good.Weasel = NO
Microsoft.GeneralURL = NO
Microsoft.HeadingAcronyms = NO
Microsoft.Auto = NO
Microsoft.Acronyms = NO
Microsoft.Wordiness = NO # Postman.TooWordy, which was based on WriteGood, is better.
Microsoft.Passive = NO
Microsoft.Vocab = NO
Microsoft.ComplexWords = NO
Microsoft.We = YES
Microsoft.Adverbs = NO # Postman.Weasel now has every unique word from this added to it.
Microsoft.Terms = NO
Microsoft.Avoid = NO
[formats]
mdx = md
  1. Vale will automatically fetch the Style rules for you and save them into your StylesPath.
  2. Set up a keybinding in Neovim to append <cword>, the word under the cursor, to the custom spell file. Since this configuration specifies Vocab = Me, the spellfile should be created at ./vale/styles/config/vocabularies/accept.txt, and it will simply be a list of words to ignore separated by newlines.
Back to top