Justin D.
Justin D.

Reputation: 4976

Vim - Custom Generic Syntax File

I created a vim script to allow easy insertion of snippets when I code. I use placeholders of the form <#placeholder#> and loop through them. I would like to highlight the placeholders as comments so vim doesn't try to color those blocks :

syntax match abridgePlaceholder "<#.[^#]*#>"
highlight link abridgePlaceholder Comment

This works fine in a live session, but I tried to package it in a syntax file inside .vim/syntax but it doesn't work. I think vim doesn't load this syntax file at runtime.

I also tried putting that syntax in my .vimrc, but it didn't work neither...

So I would like that syntax to be applied to all filetypes. Is there a way to do this?

Upvotes: 1

Views: 293

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172540

Yes, this is possible. You need to hook into the Syntax event; this gets fired whenever the filetype / syntax changes. You can place this into your ~/.vimrc, but after :syntax on, so that the default syntax handling (which clears existing stuff) comes first:

:autocmd Syntax * syntax match abridgePlaceholder "<#.[^#]*#>"

The highlight needs to be defined only once:

:highlight link abridgePlaceholder Comment

Caveats

The syntax additions may interfere with the existing filetype's syntax, adding containedin=ALL may alleviate the problem, but there's no perfect solution.

Alternative

Alternatively, you could use :match / call matchadd(), which is completely separate from syntax highlighting, but this needs to be defined for each window (again, using :autocmds).

Upvotes: 3

Related Questions