vbd
vbd

Reputation: 3557

Define colored keywords working with all syntax-files

What I want is to define a list of approx. 20 keywords which will be same coloured independet from the active syntax-file. I copied and and pasted the following to my .vimrc to highlight the word DONE, but it won't work.

syn match tododone        /DONE/ 
syn region done start=/\*\*DONE/ end=/\*\*/ 
hi link tododone tDone
hi link done tDone
hi default tDone ctermfg=DarkGreen guifg=White guibg=DarkGreen

Is it possible? And if yes what have I missed?

Upvotes: 2

Views: 303

Answers (1)

DrAl
DrAl

Reputation: 72606

It is possible, but you'll have to do it after the syntax highlighting has been defined (most syntax highlighters start with :syn clear, which will erase what you've done). This can be done with an autocmd. Try this:

hi link tododone tDone
hi link done tDone
hi default tDone ctermfg=DarkGreen guifg=White guibg=DarkGreen

function! HighlightKeywords()
    " syn keyword is faster than syn match and is
    " therefore better for simple keywords.  It will
    " also have higher priority than matches or regions
    " and should therefore always be highlighted (although
    " see comments about containedin= below).
    syn keyword tododone DONE

    syn region done start=/\*\*DONE/ end=/\*\*/
endfunction

autocmd Syntax * call HighlightKeywords()

Note that the syn region part can't be guaranteed as there are various overlapping issues with the region highlighter that may cause you problems.

Also, as a general note, if there are regions in which you want the highlighting to appear, these will have to be explicitly listed, which may make things a bit messy: e.g.

" Allow this keyword to work in a cComment
syn keyword tododone DONE containedin=cComment

For more information, see:

:help :syn-keyword
:help :syn-region
:help :function
:help :autocmd
:help Syntax
:help :syn-containedin
:help :syn-priority

Upvotes: 3

Related Questions