tags:

views:

61

answers:

1

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?

+3  A: 

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
Al
Yeah, you want to try to do this using *only* keywords if possible. Dealing with interactions with pre-existing syntax (in particular C) can get extremely messy, especially if you have regions. If you ever run into issues with containment: the C syntax file makes extensive use of contains=ALLBUT. This can make a lot of weird stuff happen.
Jefromi
you can define extra syntax after the defaults by putting it in the appropriate `.vim/after/syntax/<filetype>.vim` file.
rampion
`containedin=ALL` should help the new syntax items to match in more places.
too much php
@rampion: Yes, but (if I understood the question) I think vbd wanted to have a few generic keywords that appeared in all files without having to keep creating new syntax/<filetype>.vim files.
Al
@too much php: Yes, good point, thank you.
Al
Thank you for the solution and the explaining. I got it to work!
vbd