This is a follow-up from VIM: simple steps to create syntax highlight file – for logfiles
I am trying to use the ‘region-match’ facility to syntax-highlight stack-traces in some logfiles: these logfiles (log4j-based) look a bit like this:
YYYY-MM-DD HH:MM:ss,SSSS...INFO...Message
YYYY-MM-DD HH:MM:ss,SSSS...INFO...Message
YYYY-MM-DD HH:MM:ss,SSSS...ERROR...Message
...stack trace...
...stack trace...
...blah blah, more server-vomit...
...
YYYY-MM-DD HH:MM:ss,SSSS...INFO...Message
So far I have managed to almost do what I want with this:
:syntax region error matchgroup=string start=/^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2},\d\{3}.* ERROR/ end=/^\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2},\d\{3}/
But the issue, is that match goes too far. It includes the next record (ie, the match includes the next YYYY-MM-DD...).
I believe this VIM manual example for quoted text suggests that I should be able to highlight in-between, but I don’t seem to be able to map the syntax for my example.
So to be clear: I need to match the first YYYY-MM-DD... line that includes ERROR, and then all subsequent lines up to but not including the next YYYY-MM-DD line.
There are a lot of difficulties with overlapping regions in Vim’s syntax highlighting engine. The order in which matches and regions are defined makes a difference and it can be very hard to make it do what you want.
The main thing I’d suggest is to look at
:help syn-pattern-offset. This provides a means to make the region end at the start of a pattern among other things. For example if your end pattern is:Then the region will end at the character before the
pof pattern.This takes a lot of playing around to make it work and I’m far from being an expert on these things, but to get you started, try this: