Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim highlight a list of words

Tags:

I need to highlight a list of words in vim, preferably the words should be defined in a file. A bit like spell checking. I have been looking at http://vim.wikia.com/wiki/Highlight_multiple_words but it requires that I define each word as a new position and does not support storing the words to a file (though they are storable).

like image 232
zpon Avatar asked Nov 12 '10 08:11

zpon


2 Answers

You could use the :match command.

:match Todo /word1/ 

The first parameter of the command is the highlight-group (to see all available groups :highlight). The second parameter is a search pattern.

You can put these in any file and load it via :source.

Another way is to change the regular expression (thx @zpon):

:match Todo /word1\|word2\|word3/ 

If you want to highlight them differently:

:match Todo /word1/ :2match Error /word2/ :3match Title /word3/ 
like image 154
michael.kebe Avatar answered Sep 22 '22 02:09

michael.kebe


I would recommend you use syn keyword for this. There are other options like :match as suggested by michael.kebe and syn match etc, but these are all based on regular expression matches. The disadvantage of this is that as the number of words that you want to match increases, the speed of Vim decreases. Keyword matches are much faster. You can also easily define multiple keywords on a line (although there is a limit of about 512 characters on a line, if memory serves me correctly).

syn keyword Todo word1 word2 word3 syn keyword Todo word4 syn keyword Todo word5 

Put these lines in any file and :source it or dump it in your ~/.vim/after/syntax/c.vim for it to be sourced automatically for all C files (pick your syntax file for the file type you're interested in).

As with michael.kebe's answer, the first parameter (Todo in this case) is the highlight group. You can make up your own group if you want to and define the highlighting:

syn keyword MyHighlightGroup word6 word7 " Then EITHER (define your own colour scheme): hi MyGroupName guifg=Blue ctermfg=Blue term=bold " OR (make the colour scheme match an existing one): hi link MyGroupName Todo 
like image 44
DrAl Avatar answered Sep 24 '22 02:09

DrAl