Automatically highlight words words when opening text file in vim
How do I make vim select all the words that are in my dictionary when I open a text file (at startup)?
My preferred way would be to add 2 or 3 lines / fun / autocmd settings to my vimrc, but if that is not possible, what plugin would be offering this exact dictionary word feature? Thanks in advance.
source to share
I've tried various approaches such as creating custom dictionaries and then overriding the highlighting for words Normal
and SpellBad
. I've also tried labeling the desired words as "rare" (since Vim uses different emphasis for rare words), but those ideas didn't work. Here are the best I could think of:
Define selection
First, determine how you want the words to be highlighted. In this example, I want all the numbers from one to ten to be highlighted, so I name my group, Numbers and tell Wim that I want these words to be red, either terminal or GUI version.
highlight Numbers ctermfg=red guifg=red
Option 1: Syntax
If there are many words, use the command syntax
to define keywords to highlight:
syntax on
syntax keyword Numbers one two three four five six seven eight nine ten One Two Three Four Five Six Seven Eight Nine Ten
Option 2: match
Note that using the syntax parameter, you need to enable various upper and lower case permutations. If you don't want to do this, you can instead use a
match
keyword that works with regex patterns rather than a list of words. Use the option \c
to ignore case.
match Numbers /\c\<one\>\|\<two\>\|\<three\>\|\<four\>\|\<five\>\|\<six\>\|\<seven\>\|\<eight\>\|\<nine\>\|\<ten\>/
The downside to using it match
is that Vim has to keep evaluating pattern matching to change the text. It becomes costly if the regex pattern is too long (many words). This will cause Vim to become too slow.
source to share