I am trying to write a syntax file (.vim) to hightlight in 2 colour format whenever timestamp at the beginning of the line changes. For example:

1:00 - start
1:00 - wait for event
2:00 - wait for event
3:00 - wait for event
3:00 - wait timedout

I want to highlight first two line in grey colour, and switch to which when time change from 1:00 to 2:00 for line 3, and change back to grey at 3:00 for line 4 and 5. Any one got an idea?

Thanks in advance

  • 1
    Can you share what you've tried? – Herb Nov 1 '17 at 16:19
  • I have an idea, give me a few minutes to try it out first... – DarkWiiPlayer Nov 2 '17 at 9:18
  • What do you want it to do if you have multiple events at time 2:00? – Rich Nov 2 '17 at 11:34

Here's what I came up with: \v^(\S+).*\n(\1@!&\zs.*)

  1. \v sets the expression to very magic (:help /magic)
  2. ^ anchors the expression to the beginning of a line
  3. (\S+) is the first word of the line, we will later compare to this
  4. .*\n the rest of the line including line break; we don't really care about this
  5. \1@! matches an empty string here if \1 would not match (\1 is the first word of the previous line)
  6. &\zs.* & match this if what comes before (anything other than \1) matches, \zs ignore what we matched before, this is where the real match starts, .* match the rest of the line, meaning the entire line, because we only matched "" with \1@! and & goes back to the starting position anyway*.

So, what this ends up doing is match a whole line if it starts with a different word than the preceding line. If you want to filter a bit more, and, say, only want lines with a time \d+:\d+ at the start, just switch out the \S+ for your (more specific) pattern.

Finally, as for highlighting those lines, you can use :match (:help :match and :help group-name) like so: :match Todo /\v^(\S+).*\n(\1@!&\zs.*)/. I am using Todo here because I like doing that for generic highlights, but you could also use Underlined for something less in-your-face, or Error for something even more important-looking. The end result would ultimately be the same though.

*meaning, for example, /..:..&00/ matches 00:00 because both /00/ and ..:.. match 00:00, but it doesn't match 11:1100, because while ..:.. matches 11:11, 00 goes back again and tries matching at the same place ..:.. did, not after it.

  • Looks like you've got an extra ) at the end of your regexp? – Rich Nov 2 '17 at 11:10
  • @Rich True, it's fixed now :) – DarkWiiPlayer Nov 2 '17 at 12:02

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.