自定义Vim HTML语法 [英] Custom Vim HTML syntax
问题描述
我有一个脚本读取HTML文件,并用Perl设置的值替换〜%foo%〜
出现次数。就像这样:
I have a script that reads an HTML file and replaces occurrences of ~%foo%~
with a value set by Perl. Something like this:
<span class="~%classname%~">~%hi_mom%~</span>
在浏览器中产生这样的内容:
Would produce something like this in the browser:
<span class="classyclass">Hello World</span>
所以我想使用Vim语法高亮区分出现〜% foo%〜
在HTML中。默认情况下,HTML语法突出显示将使HTML元素的属性值 Magenta
,并且我希望〜%foo%〜
部分为 DarkMagenta
。我在正确的轨道上,因为如果我注释掉 tokenQuoted
行(或标记行),我会得到所需的结果,但是匹配和高亮都未注释,标记突出显示将覆盖 tokenQuote
突出显示。
Right so I want to use Vim syntax highlighting to distinguish the occurrences ~%foo%~
in the HTML. By default, the HTML syntax highlighting will make an HTML element's attribute values Magenta
and I want the ~%foo%~
portion to be DarkMagenta
. I'm on the right track because if I comment out the tokenQuoted
lines (or token lines) I get the desired results but with both matches and highlights uncommented the token highlighting overrides the tokenQuoted
highlighting.
syntax match token containedin=ALLBUT,htmlString,htmlValue '\~%[^%]\+%\~'
syntax match tokenQuoted containedin=htmlString,htmlValue '\~%[^%]\+%\~'
" tokenQuoted assumes htmlString/htmlValue (:highlight String) is Magenta
highlight token term=none ctermfg=White guifg=White
highlight tokenQuoted term=none ctermfg=DarkMagenta guifg=DarkMagenta
我正在使用的文件来源于默认的 html.vim
源自
autocmd * .html〜/ .vim / syntax / html.vim
在 .vimrc
。
The file I'm working in is sourced after the default html.vim
is sourced via
autocmd *.html ~/.vim/syntax/html.vim
in .vimrc
.
推荐答案
问题在于 toke n
匹配不会被包含在 tokenQuoted
匹配中。为了得到想要的结果,即突出显示与非引号标记不同的引用标记,请在语法文件中使用以下内容:
The problem is that token
match is not being excluded from being contained in the tokenQuoted
match. To get the desired results, i.e. highlighting quoted tokens different from non quoted tokens, use the following in your syntax file.
syntax match token containedin=ALLBUT,htmlString,htmlValue,tokenQuoted '\~%[^%]\+%\~'
syntax match tokenQuoted containedin=htmlString,htmlValue '\~%[^%]\+%\~'
highlight token term=none ctermfg=White guifg=White
highlight tokenQuoted term=none ctermfg=DarkMagenta guifg=DarkMagenta
或者,如果使用语法区域而不是匹配是合理的,请将上面的语法匹配行替换为以下代码。
Or if it makes sense to use a syntax region rather than a match, replace the syntax match lines above with the following.
syntax region token contained start=+\~%+ end=+%\~+ containedin=ALLBUT,htmlString,tokenQuoted
syntax region tokenQuoted contained start=+\~%+ end=+%\~+ containedin=htmlString
还应该提到,当我是tes我创建了文件〜/ .vim / syntax / html.vim
并添加了上述内容。没有必要添加任何东西到我的.vimrc文件。
I guess I should also mention that when I was testing this I just created the file ~/.vim/syntax/html.vim
and added the above content. There was no need to add anything to my .vimrc file.
这篇关于自定义Vim HTML语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!