Rook
Rook

Reputation: 62538

Vim matching based on beginning of lines

I have a file in which a line can start with +, - or *. In between some of these lines there can be lines (containing those characters as well, but not in column 1!) which start with a letter or a number (general text).

Knowing this, what would be the easiest way to setup a matching & highlighting mechanism, so that a line that starts for example with + and all subsequent lines until a line with either +, - or * in column 1, would be highlighted in ... Red?

All ideas welcomed.

Upvotes: 4

Views: 115

Answers (1)

Benoit
Benoit

Reputation: 79185

You could use:

syntax region MyRegion start=/^+/ end=/\ze\n[-+*]/
hi MyRegion guifg=red ctermfg=red

If you want to integrate it with your current colorscheme rather use:

hi link MyRegion Keyword

(or Comment, Identifier, Constant, etc.)

Note. This means the region ends on the line before the next -,+or * at the start of next line. Therefore two regions starting with a + are contiguous and you won't notice a difference in highlighting.

If you want each region from the line beginning with + to the line (inclusive) beginning with +, -or* then use:

syntax region MyRegion start=/^+/ end=/^[-+*].*/

Upvotes: 4

Related Questions