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.