在 Vim 中为自定义语法关键字设置颜色? [英] Set colors for custom syntax keywords in Vim?

查看:121
本文介绍了在 Vim 中为自定义语法关键字设置颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 vim 中使用了一个语法文件,它定义了许多特定于文件类型的语法关键字.为了给这种类型的文件着色,我还为共享目录创建了一个 colorcheme 文件,该文件试图突出显示这些语法关键字,但是当我打开该扩展名的文件时它们不会生效.

然而,我的颜色文件会突出显示正常组,例如 NormalSpecialComment 等.同样,当我尝试移动时这些突出显示到我的 .vimrc 文件的命令,它们仍然没有效果.但是,在 vim 中加载文件后,手动输入突出显示命令即可正常工作.

为了使用语法文件中定义的语法关键字,我需要做什么特别的事情吗?我可能需要指定我的语法文件的加载顺序吗?我的 .vimrc 中的颜色文件?

使用 :scriptnames,我能够看到我的自定义颜色方案文件在语法文件之前加载很久,实际上它加载到最后.但是,我的 .vimrc 文件将 colorscheme CustomPersonal 指定为最后一行,远在 syntax on 之后.

解决方案

你的 .vimrc 将在你第一次启动 Vim 时运行;在文件加载之前不会应用语法文件,这听起来像是覆盖了您的设置.

相反,您可以将自定义配色方案放在 ~/.vim/after/syntax/.vim 而不是 CustomPersonal.vim(或至少从那里调用它).

另一种替代方法是将 autocmd 添加到您的 .vimrc 以响应合适的事件源 CustomPersonal.vim.>

I'm using a syntax file in vim that defines a number of filetype-specific syntax keywords. To color files of this type I've also created a colorscheme file for the share directory that attempts to highlight these syntax keywords, however they don't take effect when I open files of that extension.

My color file does however highlight normal groups such as Normal, Special, Comment, etc. As well, when I attempt to move these highlight commands to my .vimrc file, they still have no effect. However, after the file is loaded in vim, entering the highlight commands manually works as intended.

Is there something special I need to do in order to use syntax keywords defined in the syntax files? Do I maybe need to specify the load ordering of my syntax files & color files in my .vimrc?

EDIT: using :scriptnames, I am able to see that my custom colorscheme file loads long before the syntax file, which in fact loads dead last. However, my .vimrc file specifies the colorscheme CustomPersonal as the last line, far after syntax on.

解决方案

Your .vimrc will be run when you first start Vim; the syntax file won't be applied until the file loads, and it sounds like it's overriding your settings.

Instead you could put your custom color scheme in ~/.vim/after/syntax/<filetype>.vim instead of CustomPersonal.vim (or at least call it from there).

Another alternative would be to add an autocmd to your .vimrc to source CustomPersonal.vim in response to a suitable event.

这篇关于在 Vim 中为自定义语法关键字设置颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆