1

I am currently working on a custom syntax highlighting for vim. I want to highlight lines starting with a space, but only if they contain any letters or numbers.

I could achieve this result with the syn match '^ .*.[a-zA-Z].*', but I want the highlight to be working only if the previous line does not end with a "=". Is this possible?

Thanks!

0

Here is a sample text to test the regex you want to build :

this line doesn't begin with a space
 this line begins with a space and the previous line doesn't end with =
 this line begins with a space and the previous line ends with =

The set of characters [a-zA-Z] can be expressed with the atom \a. So your regex can be rewritten like this :

^ .*.\a.*

Now your regex uses the ^ character, which stands for a beginning of a line. The problem is it can only be used at the beginning of the regex. To describe a beginning of line anywhere inside the regex, you have to use \_^.

Finally, you don't want the previous line to begin with the character =. This can be expressed with the following regex (in very magic mode) :

(\=\n)@<!

The end result is :

(\=\n)@<!\_^ .*.\a.*

Or if you want to be sure the very magic mode is enabled :

\v(\=\n)@<!\_^ .*.\a.*
0

Yes, it is.

O.k., now seriously. This is only for the starting space.

syntax match WhiteSpaceWithoutPrecedingEqualSign /=\@<!\n^ \S/hs=s+1,he=e-1
hi WhiteSpaceWithoutPrecedingEqualSign guibg=blue

This is for the line.

syntax match WhiteSpaceWithoutPrecedingEqualSign /=\@<!\n^ \S.*/hs=s+1
hi WhiteSpaceWithoutPrecedingEqualSign guibg=blue

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.