Wednesday, January 18, 2017

Re: How to syntax highlight only part of the string?

On Wednesday, January 18, 2017 at 8:24:28 AM UTC-6, Igor Forca wrote:
> Hi,
> in $HOME\vimfiles\syntax\sql.vim I have written several rules to have syntax highlighting for SQL statements. Works perfectly. But now I would like to write error like rules to have syntax mark with red background for bad habits or syntax errors.
>
> For example I have written the following rule to mark all of the spaces at the end of line. This is not syntax error, just dummy spaces at the end of line, that is bad habit to have them, because there are of any use.
>
> " space at the end of a line
> syntax match sqlerror "\s\{-}$"
>
> Above works fine it marks with red background all of the spaces at the end of line.
>
> But now I have the following problem. Having one space between "order" and "by" is OK. Having two or more spaces between this two words I would like to mark a bad habit with red background.
>
> So far I have written the following:
>
> " two or more spaces between order and by words
> syntax match sqlerror "\<order \{2,}by\>"
>
> But above syntax highlights both words "order" and "by" and spaces between them.
>
> Is it possible to mark only spaces that are too many?
>
> Example:
> " OK not problem and no red background
> order by
>
> " there are two spaces between "order"
> " and "by" words and only ONE
> " space should get with red background
> order by
>
> " there are three spaces between "order"
> " and "by" words and only TWO spaces
> " should get with red background
> order by
>
> How to write such a syntax highlights rule to search for "order by" combination but only marks red background spaces between them if there are more then two spaces between the worlds and not red background marking the words "order" or "by" itself?
> Regards

Use '\zs' and '\ze' to mark the beginning and the end of the match. This also works for search patterns. This is probably the easiest way but specifically for syntax highlighting there are also ways to adjust the beginning and end of the match or highlighted area with offsets, see :help :syn-pattern-offset.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment