4
votes

I am quite new to Vim and have the following problem:

In my .gvimrc I defined

syn keyword Todo PMID

to highlight every PMID in the files I edit. This works quite well as long as no general syntax highlighting is applied to the file (via setf or autocmd BufRead,BufNewFile …)

My question: How do I maintain a list of individual keywords which are highlighted no matter what file I edit and which syntax highlight I use for that file?

3

3 Answers

4
votes

In this case matches come handy:

let s:kwreg='\v<%(PMID|OTHER|OTHER2)>'
let s:kwsyn='Todo'
augroup MyKeywords
    autocmd!
    autocmd WinEnter * if !exists('w:my_keyword_mnr') | 
                     \    let w:my_keyword_mnr=matchadd(s:kwsyn, s:kwreg) |
                     \ endif
augroup END
let s:curtab=tabpagenr()
for s:tab in range(1, tabpagenr('$'))
    execute 'tabnext' s:tab
    let s:curwin=winnr()
    for s:win in range(1, winnr('$'))
        execute s:win.'wincmd w'
        let w:my_keyword_mnr=matchadd(s:kwsyn, s:kwreg)
    endfor
    execute s:curwin.'wincmd w'
endfor
execute 'tabnext' s:curtab
unlet s:curtab s:curwin s:tab s:win
2
votes

If you want that highlighted everywhere (as a kind of "overlay"), use matchadd(), as shown by ZyX's answer.

If this is a syntax extension for one / a couple of filetypes (e.g. only Java and Python files), put your :syntax definition into ~/.vim/after/syntax/<filetype>.vim. You have to study the original syntax a bit, and possibly add containedin=<groupNames> clauses, so that your items properly integrate.

1
votes

You could use something like this:

syntax match pmidTODO /TODO\|PMID/
hi link pmidTODO Todo
hi Todo guibg=yellow guifg=black

This should work even when Todo is redefined by Vim syntax files.

Edit: As ZyX pointed out, this does not survive syntax changes.