Vim变量语法高亮显示 [英] Vim variable syntax highlighting
问题描述
我想更改我的vim配置文件,以仅突出显示我声明的变量,而不突出显示关键字.本文显示并解释了我的意思:备用语法突出显示
I'd like to change my vim config file to allow highlighting of only my declared variables, not keywords. This article shows and explains what I mean: Alternate syntax highlighting
我是vim的初学者(我从未更改过默认配置文件). 有人能指出我正确的方向吗?
I'm a beginner to vim (I've never changed the default config file). Could anyone point me in the right direction?
推荐答案
作为概念证明,我尝试了
As proof of concept, I tried
let vars = ['init', 'editable', 'init_ui']
let colors = ['ff0000', '00ff00', '0000ff']
for var in vars
execute 'syn keyword var_' . var var
execute 'hi default var_' . var 'guifg=#' . remove(colors, 0)
endfor
,它按预期工作.这将为列表中的每个变量创建语法项:var_init
,var_editable
和var_init_ui
.然后为每个语法项分配突出显示颜色.
and it worked as expected. This created syntax items for each variable in the list: var_init
, var_editable
, and var_init_ui
. Then it assigns a highlight color to each syntax item.
为了超越概念证明,您必须获取变量名列表.您可以通过解析标记文件(例如,由ctags生成)或通过在vim中编写解析器(这将非常便于移植)来做到这一点.您可以对列表进行排序并删除重复项,但是如果您跳过此步骤,我认为使用:hi default
可以节省您的时间.比我的示例提出一种更好的颜色生成方法.
In order to get beyond proof of concept, you have to get a list of variable names. You can do this by parsing a tag file (as produced by ctags, for example) or by writing a parser in vim (which would be very portable). You can sort the list and remove duplicates, but I think the use of :hi default
will save you if you skip this step. Come up with a better way of generating colors than my example.
输入缓冲区或用户显式调用函数时,可以使用自动命令来完成所有这些操作.然后,您就可以开始考虑在定义新变量时自动更新了.
You can do all of that using an autocommand when a buffer is entered, or when the user explicitly calls a function. Then you can start thinking about automatic updating as new variables are defined.
这篇关于Vim变量语法高亮显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!