如何在Vim中以另一种语言嵌入源代码中的一种语言的语法突出显示代码片段? [英] How to syntax highlight fragments of code in one language embedded in the source code in another language in Vim?
问题描述
我有一个自定义的 XML 文件格式,它可以在某些标签中包含代码块.
I have a custom XML file format which can contain blocks of code within certain tags.
例如:
<Root>
<Sql> select * from foo </Sql>
<MoreJunk> ... </MoreJunk>
<Python><![CDATA[
def Bar(*args):
return False
]]></Python>
</Root>
如何让 Vim 对 <Sql>
标签内的文本使用 SQL 语法高亮显示,并为 <Python>
标签内的文本使用 Python 高亮显示?
How can I get Vim to use SQL syntax highlighting for the text inside <Sql>
tags and use Python higlighting for text inside <Python>
tags?
我知道 Vim 已经可以做到这一点,因为它正确地突出显示了 HTML 文件中的 Javascript.
I know Vim can already do this because it correctly highlights Javascript inside HTML files.
我尝试检查 HTML 语法文件,但无法弄清楚.
I tried inspecting the HTML syntax file but couldn’t figure it out.
推荐答案
对于带有 Python 的 XML 示例,您必须执行以下操作:
For your XML with python example you would have to do something like this:
runtime! syntax/xml.vim
unlet b:current_syntax
syntax include @Python syntax/python.vim
syntax region pythonCode start=+<Python>+ keepend end=+/</Python>+ contains=@Python
这些行将包含 XML 语法和 python 语法,并指定一个 python 区域,VIM 将在其中使用 python 语法而不是 XML 语法...
These lines will include the XML syntax and the python syntax, and the specify a python region where VIM will use the python syntax instead of the XML syntax...
当然,所有这些都在 VIM 中有详细记录.有关如何包含语法文件,请参阅 :he :syn-include.
Of course, all this is well documented in VIM. See :he :syn-include on how to include syntax files.
这篇关于如何在Vim中以另一种语言嵌入源代码中的一种语言的语法突出显示代码片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!