Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a Vim multiline syntax highlight clause?

I'm trying to write the syntax highlight module for a tiny text format that includes three different kinds of list elements (starting with -, o and x respectively), and I'd like to highlight entries based on their kind. For single lines it's easy, I just use a syn match myGroup /^\s\+- .\+/ and I'm done.

Problem is, I've been trying to do it so that the next lines without a list marker keep the same colour as the starting list item line, to no success. I've been trying to do it with syntax regions, but I can't seem to understand how they work.

To be more precise, this is the output I'm trying to reach: enter image description here

If any change is needed in the file format so that it is easier/possible, I have liberty to change it.

Any clue of how can I get it?

like image 801
Alexandre Araujo Moreira Avatar asked Nov 11 '15 13:11

Alexandre Araujo Moreira


People also ask

How do I create a syntax highlight in Vim?

After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting. The file will look like the following image if syntax highlighting is on. Press ESC key and type, “syntax off” to disable syntax highlighting.

Does Vim have syntax highlighting?

VIM is an alternative and advanced version of VI editor that enables Syntax highlighting feature in VI. Syntax highlighting means it can show some parts of text in another fonts and colors. VIM doesn't show whole file but have some limitations in highlighting particular keywords or text matching a pattern in a file.

How do I highlight a line in Vim?

With the default backslash leader key, pressing \l will highlight the line that currently contains the cursor. The mapping also sets mark l so you can type 'l to return to the highlighted line.


1 Answers

You can try something along these lines

syntax region Minus_Region start=/^\s\+-/ end=/;/
hi Minus_Region guifg='Yellow'

syntax region O_Region start=/^\s\+o/ end=/;/
hi O_Region guifg='Green'

syntax region X_Region start=/^\s\+x/ end=/;/
hi X_Region guifg='Gray'

You define region by its start and its end (in this case ;), no matter how many lines are involved.

For more information, see help

  • :h syn-region

If you want to finish the regions without having a end marking character (in this case ;), you could do it using the match-end (me) option on the end argument of the regions, and having the regions end on the next region-start marker. Example:

syntax region Minus_Region start=/^\s\+- / end=/^\s\+[-ox] /me=s-1

syntax region O_Region start=/^\s\+o /  end=/^\s\+[-ox] /me=s-1

syntax region X_Region start=/^\s\+x /  end=/^\s\+[-ox] /me=s-1

The me=s-1 part means "The real match ends at one character to the left of the start position of the pattern match".

like image 188
ryuichiro Avatar answered Sep 27 '22 18:09

ryuichiro