开发者

Vim highlight a list of words

I need to highlight a list of words in vim, preferably the words s开发者_Python百科hould 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).


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/


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


:match Todo /\<\(word1\|word2\|word3\)\>/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜