为什么我不能通过本地 vimrc 文件更改 vim 语法突出显示?

Why can't I change vim syntax highlighting via local vimrc file?

我正在尝试通过在本地配置 $project/.lvimrc 中放置额外的指令来自定义 vim 突出显示,该配置由 https://github.com/embear/vim-localvimrc 插件管理。

不幸的是,命令似乎像

syntax match Operator "\<MYOP\>"
位于 .lvimrc

被 vim 默默忽略。在命令行中键入命令按预期工作。来自 .lvimrc 的其他命令也有效。那么什么可以阻止 vim 正确解释局部突出显示?

加载顺序可能有问题,即先加载您的 .lvimrc,然后加载文件类型语法并覆盖 .lvimrc 语法命令。您可以通过在两个文件中包含 echom 语句来进行检查。

另请注意,本地 vimrc 并不是自定义语法高亮的标准方式。来自 Vim FAQ 24.11:

You should not modify the syntax files supplied with Vim to add your
extensions. When you install the next version of Vim, you will lose your
changes. Instead you should create a file under the ~/.vim/after/syntax
directory with the same name as the original syntax file and add your
additions to this file.

For more information, read 

    |mysyntaxfile-add|
    |'runtimepath'|

那是因为 https://github.com/embear/vim-localvimrc 插件默认在沙箱中启动本地文件。沙箱中不允许语法命令(至少在我的设置中),因此引发了异常。出于某种原因,Vim 会静默处理此类异常。

就我而言,以下修改形成了一个解决方案:

  1. 通过添加 let g:localvimrc_sandbox = 0 到 master .vimrc 文件来禁用 localvimrc 的沙箱
  2. 添加set conceallevel=2到localvimrc