Embedded syntax highligting in Vim

夙愿已清 提交于 2019-12-17 15:31:25

问题


I have a custom XML file format which can contain blocks of code within certain tags.

For example:

<Root>
    <Sql> select * from foo </Sql>
    <MoreJunk> ... </MoreJunk>
    <Python><![CDATA[
    def Bar(*args):
        return False
    ]]></Python>
</Root>

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?

I know Vim can already do this because it correctly highlights Javascript inside HTML files.

I tried inspecting the HTML syntax file but couldn't figure it out.


回答1:


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

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...

Of course, all this is well documented in VIM. See :he :syn-include on how to include syntax files.




回答2:


This document describes how to write your own syntax highlighting. You should probably be able to figure out how the HTML-syntax highlighting works with javascript, with that as a reference.



来源:https://stackoverflow.com/questions/519753/embedded-syntax-highligting-in-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!