Ignore URLs and Acroynms While Spell-checking Vim

Today’s post is a short one that I have incorporated into Writing With Vim and will publish when I add a few more changes.

When Vim’s spell check is enabled, words that are not known to be good are highlighted as incorrect. This is the behavior that we want generally. However, there are certain types of text that are commonly marked as incorrect, and it can be tedious or impossible to constantly add them to your dictionary.

One example would be URLs. These are marked as incorrect by Vim by default, but it would be nice to just tell Vim to ignore them from the spell check so you can navigate spelling errors more quickly and to reduce the amount of noise on the screen. You can accomplish this with:

 " Don't mark URL-like things as spelling errors
 syn match UrlNoSpell '\w\+:\/\/[^[:space:]]\+' contains=@NoSpell

Here, we are defining a new syntax highlighting group called UrlNoSpell (could be called whatever you want), and when text matches the pattern, we mark it with the special highlight group called @NoSpell.

The pattern says to skip checking the spelling of any string which has word characters ([a-zA-Z0-9]) before a :// until a space character is encountered. So any string starting with http://, https://, ftp://, or even anything:// would qualify as a match. There may be things that aren’t true URLs that are matched with this pattern, but it should cover most cases and lead to few false negatives.

When I looked through my spellfile, I saw many acronyms or abbreviations. Things like “AWS”, “B2B”, “CEOs”, “HL7”, “MP3”, “PDF”, and so forth. I removed all of these from the spellfile with the following command:

 " Don't count acronyms / abbreviations as spelling errors
 " (all upper-case letters, at least three characters)
 " Also will not count acronym with 's' at the end a spelling error
 " Also will not count numbers that are part of this
 " Recognizes the following as correct:
 syn match AcronymNoSpell '\<\(\u\|\d\)\{3,}s\?\>' contains=@NoSpell

This function works similarly to the previous one, except the pattern is different. The pattern says: if we have an entire word composed of three or more characters that are all either uppercase letters or digits, possibly followed by “s” (as in “CEOs”), then mark this word as good. Again, while this could lead to false negatives (“ZARBLATZZZZ” is marked as good, for instance), it might be something to consider for your spell function.

I recommend adding these to your writing function and they will be enabled whenever you are in a writing mood.

Categories: main

« Running Old Programs In The Browser How to Actually Publish More Things »

Comments