Reputation: 165
I have a type of text file that has a bunch of keywords that all start with with a hash (#). Example:
#foobar
#fizz bizz
I'd like to adjust a vim syntax file to highlight only words after a keyword; in this example it would be bizz.
Upvotes: 2
Views: 1016
Reputation: 172510
Even if you don't want to highlight the keyword itself (your opinion may change later), it's easier to parse both into separate syntax groups, as Vim's syntax highlighting is tailored to a full parsing of a document.
So, define syntax groups for keywords and words, and specify the relation between them:
syntax match myKeywords "#\w\+\>" skipwhite nextgroup=myKeywords,myWords
syntax match myWords "\<\w\+\>" contained skipwhite nextgroup=myKeywords,myWords
(The patterns take your scarce description as input; adapt according to your needs.) To highlight the words, you either link myWords
to an existing highlight group (recommended), or define your own attributes:
highlight link myWords Statement
The myKeywords
is parsed, but not highlighted, because there's no :highlight
command for it.
If there's a (third-party) syntax script that you need to "adjust", it's best to keep these extensions separately in the after directory (e.g. ~/.vim/after/syntax/<filetype>.vim
). Also, depending on existing syntax groups, there may be additional complications, if existing syntax groups already match (which can be fixed with containedin=...
).
If this is a new syntax, it's best to write a corresponding filetype detection; in the syntax script, replace the my
prefix with the chosen filetype name then. You'll find more information about syntax scripts at :help 44.11
.
Upvotes: 3
Reputation: 7627
To highlight text after #fizz
:
syntax match HashWord /\v(#fizz>)@<=.*/hs=s+1
highlight HashWord ctermbg=60
This creates a syntax group HashWord
that matches the pattern \v(#fizz>)@<=.*
. The highlighting starts (hs
) one character after the start (s+1
) of the pattern.
The background and foreground highlighting colors can be changed with ctermbg
and ctermfg
. For GUI vim use guibg
and guifg
instead.
Upvotes: 0