Wiener Boat
Wiener Boat

Reputation: 446

Syntax highlighting by pattern

I've been using a syntax highlight for some time to match space characters that come after leading tabs.

 autocmd Syntax * highlight LeadingSpaces guibg=#afd7af ctermbg=Black
 autocmd Syntax * syntax match LeadingSpaces /^\t\+\zs \+/

Problem is, it doesn't always work. For example, inside block comments the highlight doesn't take effect:

 test  // the extra space before "test" gets highlighted
/**
 * test  // here the extra spaces aren't highlighted
 */

If anybody has an idea what I'm doing wrong, that would be great. Otherwise I'll have to go back to regular matching, I guess.

Upvotes: 1

Views: 97

Answers (1)

mb14
mb14

Reputation: 22596

I had a similar problem (but display trailing spaces at the end of the line). To solve it, instead of using syntax, which interferes with the actual syntax you can just highlight a regular expression using matchadd.

This look like this (reusing your LeadingSpaces )

autocmd BufNew * call matchadd ('LeadingSpaces', '^\t\+\zs \+')

Upvotes: 2

Related Questions