highlighting prefixed keywords

15 views
Skip to first unread message

Jorge Almeida

unread,
Dec 17, 2017, 1:50:19 PM12/17/17
to vim...@googlegroups.com
I need to apply custom highligthing in a setup that should be simple
for people who understand highligthing:

I have a bunch of keywords with a common prefix, the single character
'c'. The keywords are to be listed one by one, not obtained via some
algorithm. Example:

cmount
cumount
copen
cclose
(etc)

So, I want to highlight these keywords, *minus the prefix*, with some
colour (say, the same colour for all keywords). That is, in "cmount",
only "mount" should be coloured, not the leading 'c'.

TIA

Jorge Almeida

Tim Chase

unread,
Dec 17, 2017, 2:03:02 PM12/17/17
to vim...@googlegroups.com
Does

:match Error /\%(\<c\)\@<=\w\+/

give you what you're looking for? Alternatively, if they're a fixed
list of keywords, you can join them together something like

:match Error /\%(\<c\)\@<=\(mount\|umount\|open\|close\)/

You can adjust the color group to something other than Error, as
that's just what I chose by default.

-tim


Jorge Almeida

unread,
Dec 17, 2017, 2:16:42 PM12/17/17
to vim...@googlegroups.com
On Sun, Dec 17, 2017 at 7:02 PM, Tim Chase <v...@tim.thechases.com> wrote:
> On 2017-12-17 18:49, Jorge Almeida wrote:
>> I need to apply custom highligthing in a setup that should be simple
>> for people who understand highligthing:
>>
>> I have a bunch of keywords with a common prefix, the single
>> character 'c'. The keywords are to be listed one by one, not
>> obtained via some algorithm. Example:
>>
>> cmount
>> cumount
>> copen
>> cclose
>> (etc)
>>
>> So, I want to highlight these keywords, *minus the prefix*, with
>> some colour (say, the same colour for all keywords). That is, in
>> "cmount", only "mount" should be coloured, not the leading 'c'.
>
> Does
>
> :match Error /\%(\<c\)\@<=\w\+/
>
> give you what you're looking for? Alternatively, if they're a fixed
> list of keywords, you can join them together something like
>
> :match Error /\%(\<c\)\@<=\(mount\|umount\|open\|close\)/
>
Indeed it does! (Well, the latter suggestion does.)

I did this:

highlight macro_skin guibg=bg guifg=DarkGreen gui=bold ctermfg=022 cterm=bold
match macro_skin /\%(\<c\)\@<=\(mount\|umount\|open\|close\)/

And that's it. (And just a few minutes after posting, to boot!)

Just a detail: is it possible to enumerate the keywords in a separate
declaration, to improve maintainability? (The actual list is more
extense). I'm thinking of something similar to:

syntax keyword Foo open close mount umount

Even if it's not possible, this is good enough.

Thanks

Jorge

Tim Chase

unread,
Dec 17, 2017, 4:14:17 PM12/17/17
to vim...@googlegroups.com
On 2017-12-17 19:16, Jorge Almeida wrote:
> Just a detail: is it possible to enumerate the keywords in a
> separate declaration, to improve maintainability? (The actual list
> is more extense). I'm thinking of something similar to:
>
> syntax keyword Foo open close mount umount
>
> Even if it's not possible, this is good enough.

Also, one might use

syn match jaKeyword /\<c\%(umount\|mount\|open\|close\)/hs=s+1
hi def link jaKeyword Identifier

which simplifies the syntax a bit.

Despite multiple attempts, I was unable to get it to work with
syn-keyword as it seems to want a Word boundary between the /\<c/ and
the keyword. I tried

syn region jaKeywordRegion start='\<c'rs=s+1 end='\>' contains=jaKeyword transparent
"syn keyword jaKeyword contained umount mount open close
syn match jaKeyword contained /umount\|mount\|open\|close/
hi def link jaKeyword Identifier

and it worked fine with the "match" version but failed with the
"keyword" version. I also tried various configurations of ms= and
rs= offsets to no avail.

-tim



Jorge Almeida

unread,
Dec 17, 2017, 4:30:48 PM12/17/17
to vim...@googlegroups.com
On Sun, Dec 17, 2017 at 9:14 PM, Tim Chase <v...@tim.thechases.com> wrote:
> On 2017-12-17 19:16, Jorge Almeida wrote:
>> Just a detail: is it possible to enumerate the keywords in a
>> separate declaration, to improve maintainability? (The actual list
>> is more extense). I'm thinking of something similar to:
>>
>> syntax keyword Foo open close mount umount
>>

> syn match jaKeyword /\<c\%(umount\|mount\|open\|close\)/hs=s+1
> hi def link jaKeyword Identifier
>
> which simplifies the syntax a bit.
>
OK, thanks. As it stands now it does the job.

Syntax highlighting is a great feature. Pity that it's so difficult to learn.

Cheers

Jorge
Reply all
Reply to author
Forward
0 new messages