Search code examples
vimvim-syntax-highlighting

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?


Solution

  • 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

    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".