Defining a vim syntax file that selectively applies other syntax files in different regions

StackOverflow https://stackoverflow.com/questions/23685731

  •  23-07-2023
  •  | 
  •  

質問

Is there a way to define a syntax file in Vim that is essentially a "meta" layer on top of other syntax files? By this I mean that the file defines syntax highlighting regions, but all it does for each region is specify a different syntax file to be applied to that region. This is useful for PHP embedded in HTML, or any other sort of file mixing languages. I am aware that there are scripts that handle PHP and HTML together, but I don't think they do it in the manner I described. I'm interested in the general solution to this problem.

役に立ちましたか?

解決

That's what :syntax include is for. With it, you can mark certain regions in the buffer, and apply the subordinate syntax in there. As long as the regions are clearly delimited and do not mix, this works pretty well. This is used for example in the HTML syntax to include JavaScript inside <script> tags, and Vimscript uses that for Perl, Python, and other integration languages. You'll find these scripts at $VIMRUNTIME/syntax/.

Alternative

My SyntaxRange plugin provides a different approach, where you can highlight certain regions with another syntax, without defining a separate new syntax. This is used for more adhoc markup, e.g. to highlight a Python snippet inside an HTML blog post, or to automatically highlight inline patches inside an email body.

Composite filetypes

Finally, there are add-on syntaxes that go "on top" of a main syntax. An example is cpp.doxygen. You cannot do that with arbitrary syntaxes, though; the script has to be specially written to support that.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top