Search code examples
regexvimsyntax-highlightingvim-syntax-highlighting

Syntax Highlighting in VIM to match first/last letter of line


I'm trying to to setup syntax highlighting in VIM to match the whole line only if the first non-whitespace character is "-" or if the last non-whitespace character of the line is ":". A typical line has many tabs in front of text.

For instance I have lines like this (with tabs in front of the text):

- Go Home today

Eat vegetables:

I don't however want to match these (not end or beginning of line).

Go - Home today

Eat : vegetables

I have tried various iterations but can't get it right. For instance the following only highlights dashes, including every dash in file. I want it to highlight the whole line if the last or beginning characters match.

syn region dash start=/\v-/ end=// oneline
highlight link dash String

I know I just need to get my start/end searches right, but am having no luck. How can I do this?


Solution

  • Try something like this:

    syn region dash start=/^\s*\zs-/ end=/$/ oneline
    syn region dash start=/^/ end=/:\ze\s*$/ oneline
    highlight link dash String