自定义 Vim HTML 语法
我有一个脚本,它读取 HTML 文件并用 Perl 设置的值替换出现的 ~%foo%~
。像这样的东西:
<span class="~%classname%~">~%hi_mom%~</span>
在浏览器中会产生类似这样的东西:
<span class="classyclass">Hello World</span>
对,所以我想使用 Vim 语法突出显示来区分 HTML 中出现的 ~%foo%~
。默认情况下,HTML 语法突出显示将使 HTML 元素的属性值为 Magenta
,而我希望 ~%foo%~
部分为 DarkMagenta
。我走在正确的轨道上,因为如果我注释掉 tokenQuoted
行(或标记行),我会得到所需的结果,但匹配项和突出显示都未注释,标记突出显示会覆盖 tokenQuoted
代码>突出显示。
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
来源之后获取的 .vimrc
中的 autocmd *.html ~/.vim/syntax/html.vim
。
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>
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
The file I'm working in is sourced after the default html.vim
is sourced viaautocmd *.html ~/.vim/syntax/html.vim
in .vimrc
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于
token
匹配未排除在tokenQuoted
匹配中。要获得所需的结果,即突出显示与非引号标记不同的引号标记,请在语法文件中使用以下内容。或者,如果使用语法区域而不是匹配更有意义,请将上面的语法匹配行替换为以下内容。
我想我还应该提到,当我测试这个时,我刚刚创建了文件
~/.vim/syntax/html.vim
并添加了上述内容。无需向我的 .vimrc 文件添加任何内容。The problem is that
token
match is not being excluded from being contained in thetokenQuoted
match. To get the desired results, i.e. highlighting quoted tokens different from non quoted tokens, use the following in your syntax file.Or if it makes sense to use a syntax region rather than a match, replace the syntax match lines above with the following.
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.