混合两个语法高亮脚本 [英] Mixing two syntax highlighting scripts
问题描述
我用 Jekyll 写博文,为此我最终在同一个文件中使用了三种不同的语言:开头的 YAML 用于帖子元数据,正文中的 Markdown,以及代码片段中的 C++.
I write blog posts with Jekyll, and for that I end up using three different languages in the same file: YAML at the start for post metadata, Markdown in the body, and C++ in code snippets.
我试图设置一个脚本,这样我就可以让 vim 为我正确地突出显示所有三个,最后在语法/jekyll.vim 中得到了类似的东西:
I tried to setup a script so that I can have vim highlight all three properly for me, and ended up with something like this in syntax/jekyll.vim:
" Build upon Markdown syntax
runtime! syntax/markdown.vim
" pretend there's no syntax loaded
unlet b:current_syntax
" Bring in YAML syntax for front matter
syntax include @Yaml syntax/yaml.vim
syntax region yamlFrontmatter start=/\%^---$/ end=/^---$/ keepend contains=@Yaml
" pretend there's no syntax loaded
unlet b:current_syntax
" Bring in C++11 syntax for code snippets
syntax include @Cpp syntax/cpp.vim
syntax region cppCodeSnippet matchgroup=Comment start=/^{% highlight cpp %}$/ end=/^{% endhighlight %}$/ keepend contains=@Cpp
let b:current_syntax='jekyll'
我还设置了一个文件检测脚本来将 ft
设置为这种语法.
I also set up a file detection script to set ft
to this syntax.
它几乎有效.当我打开一个被检测为这种类型的文件时,除了 C++ 突出显示之外,我的所有内容都是正确的.但是,如果我在那之后输入 :syn on
,一切正常.我可以删除缓冲区并再次打开文件,所有亮点都可以.如果我关闭 vim 并再次启动它,我需要再次运行 :syn on
.
It almost works. When I open a file that gets detected as this type, I get everything correct except for the C++ highlights. However, if I type :syn on
after that, everything works fine. I can delete the buffer and open the file again and all highlights are ok. If I close vim and start it again, I need to run :syn on
again.
我错过了什么?我该如何调试这个问题?
What am I missing? How can I debug this issue?
推荐答案
快速修复: 将 syntax on
附加到 .vimrc
的最后一行code>,这与在实时会话中设置 :syn on
相同.
Quick fix: append syntax on
to the last line of your .vimrc
, which is the same as setting :syn on
in the live session.
没那么快:
看起来您可能已经在 $VIMRUNTIME
中的默认语法文件旁边安装了自定义 'jekyll' 语法.
Looks like you might have installed the custom 'jekyll' syntax alongside the default syntax files in $VIMRUNTIME
.
根据 Vim wiki 自定义语法部分,最好保留所有个人自定义在 ~/.vim
中.例如,将 jekyll.vim
语法放在 ~/.vim/syntax/
中.
According to Vim wiki section on custom syntax, it's preferable to keep all personal customizations within ~/.vim
. For example, putting your jekyll.vim
syntax in ~/.vim/syntax/
.
不要使用包含 Vim 分发的文件的目录,因为它会在升级过程中被覆盖(特别是不要使用 $VIMRUNTIME 目录).
Do not use a directory containing the files distributed with Vim because that will be overwritten during an upgrade (in particular, do not use the $VIMRUNTIME directory).
在 Vim 语法文档中::syntax enable
运行 ':source $VIMRUNTIME/syntax/DEFAULT_SYNTAX.vim'.:syn on
(或 :syntax on
)将使用默认值覆盖您的设置".
In the Vim syntax docs:
:syntax enable
runs ':source $VIMRUNTIME/syntax/DEFAULT_SYNTAX.vim'.
:syn on
(or :syntax on
) will "overrule your settings with the defaults".
因此,如果设置 :syntax on
使您的自定义语法起作用,则它必须包含在默认语法集中.
So if setting :syntax on
makes your custom syntax work, it must be contained in the default syntax set.
尝试将所有自定义内容保留在 ~/.vim
中,看看是否能解决问题.
Try keeping all the custom stuff in ~/.vim
and see if that settles things.
这篇关于混合两个语法高亮脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!