Stefan Majewsky
Stefan Majewsky

Reputation: 5555

How to create a syntax rule that can be contained anywhere?

The following snippet from my .vimrc highlights superfluous whitespace at line ends in a shade of gray:

autocmd Syntax * syntax match MySpace /\s\+$/
autocmd ColorScheme * highlight MySpace ctermbg=238

But this does not work when this whitespace is already matched by a syntax group. For example, trailing whitespace in various types of comments is not marked.

The manual talks about the contains=ALL option for syntax groups, but there seems to be no analogous containedin=ALL. Can I emulate it in any way? The only method I could come up would be to list all relevant syntax groups in the containedin= option of MySpace, and that's clearly tedious and not at all elegant.

Upvotes: 0

Views: 199

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172520

You should use the :match command (or matchadd()), as described in the Vim Tips Wiki article about this particular topic.

If you like a ready-to-use solution for this, you can also try out my ShowTrailingWhitespace plugin, or one of the alternatives listed on the plugin page.

Upvotes: 0

Stefan
Stefan

Reputation: 114158

Don't know how to do this with Syntax, but you can use the listchars options to highlight trailing spaces.

From my .vimrc:

" List chars
set listchars=""           " Reset the listchars
set listchars+=tab:\|\     " show tabs as "|"
set listchars+=nbsp:·      " show non-breaking spaces as "·"
set listchars+=trail:·     " show trailing spaces as "·"
set listchars+=precedes:«
set listchars+=extends:»

Upvotes: 1

Related Questions