vim - syn match lines which contain only / but not // -
to match lines contain forward slash(/
), can use following in syntax file
syntax match colorpath "\v/.*$" #highlight link colorpath comment1
to match lines have double forward slash(//
), can use same statement
syntax match colorpath2 "\v//.*$" #highlight link colorpath2 comment2
but first part overwrites second 1 shown in image1. also, in line 1 patha not colored, if use "\v./.$" color entire line .
is there way can differentiate 2 lines shown in image2?
this simple problem of adapting regular expressions needs.
to include patha
before /
, need match it. .*/.*$
match entire line (as you've found out); .
matches anything (also whitespace), , *
greedy match. have ask characters can contained inside colorpath, , cannot. let's assume can consist of whitespace. regular expression atom \s
(and equivalent [^[:space:]]
inside collection). this, arrive at:
syntax match colorpath "\s*/.*$"
second problem obscures colorpath2, because colorpath2 subset of colorpath. need make sets of matches disjunct, example this:
syntax match colorpath "\s*[^/[:space:]]/[^/].*$"
this says: non-whitespace, no slash (and still no whitespace) before slash, single slash, followed no slash, end of line. previous command works if there indeed path characters before slash (so fails on /pathx
). fix that, have add branches matching @ start of line, , after whitespace (of course, if syntax allows places):
syntax match colorpath "\%(\s*[^/[:space:]]\|^\|\s\zs\)/[^/].*$"
another way express negative lookbehind , lookahead; less readable, avoids manual joining of \s
, [^/]
in previous command, , automatically handles corner case of single slash @ start, without additional branches.
syntax match colorpath "\s*/\@<!//\@!.*$"
this because atoms assert match, not consume anything. see :help /zero-width
details.
Comments
Post a Comment