I want to change the color of a keyword that is followed by a left parenthesis in a .R file.

For instance, when I type length(), I want the word length to be highlighted (and not the parentheses), but when I type only length it won't receive any highlighting.

I would also like to specify the color of the highlighting based on the word. So length() and function() should result in different colors for length and function.

Edit: So far I'm using the following line in my r.vim syntax file to match a valid R object name followed by (:

syn match rFunction "\(\(\a\|[.][._\a]\)[._\a0-9]*\)\+\ *("

And this works fine, but I want to not highlight that ( at the end, but rather have it be set to my default.

1 Answer 1


Since you already have a pattern to match, I think you could use an offset for the highlighting. From the vim documentation:

The pattern can be followed by a character offset.  This can be used to
change the highlighted part
  :syn match String  /"[^"]*"/hs=s+1,he=e-1
    some "string" text
          ^^^^^^        highlighted

So I think this should do the job:

syn match rFunction "\(\(\a\|[.][._\a]\)[._\w]*\)\+\ *("he=e-1

You could also use lookaheads, which would be preferable, since it allows you to avoid having some elements enter the match:

syn match rFunction "\(\(\a\|[.][._\a]\)[._\w]*\)\+\ *[(]\@="
  • This does work, except that now I have issues when matching open and close parentheses together. My syntax file uses regions to match ( ) together and the match on the word( now overrides matching a pair. I've used syn region rFunction matchgroup=Function start="\(\(\a\|[.][._\a]\)[._\w]*\)\+\ *(" matchgroup=Function end=")" transparent but this makes the ( ) the color of the Function group, when I'd rather have it be the Delimiter group.
    – mickwar
    Oct 29, 2014 at 23:26
  • @mickwar I think vim syntax patterns support lookaheads, so instead of including the ( in the pattern directly, add it as a positive lookahead. Something like: "\(\(\a\|[.][._\a]\)[._\w]*\)\+\ *\\[\(]\@="he=e-1
    – muru
    Oct 30, 2014 at 0:12
  • Removing some of the escape characters gave me the match properly: syn match rFunction "\(\(\a\|[.][._\a]\)[._\w]*\)\+\ *[(]\@=". This works great, thanks!
    – mickwar
    Oct 30, 2014 at 17:13
  • @mickwar ah, sorry about those. Copy-paste artifact.
    – muru
    Oct 30, 2014 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.