在 Vim 中自定义语法突出显示

发布于 2024-08-25 22:41:09 字数 1930 浏览 9 评论 0原文

如何保留文件类型的所有当前格式,但添加功能

我想突出显示 .vim 文件中的颜色,以便突出显示每种颜色终端将如何解析它。

我创建了一个 vim.vim 文件,其中包含:

syn keyword yellow yellow containedin=All                                                    
highlight yellow ctermfg=yellow                                                              
                                                                                             
syn keyword red red containedin=all                                                          
highlight red ctermfg=red                                                                    
                                                                                        

并将其放入 ~/.vim/after/syntax/vim.vim

按照建议 此处

这没有效果。


更新

事实上,当我说我的更改没有效果时,我错了。如果您在一行中单独键入yellow,它将突出显示为黄色。不幸的是,这并不能解决我的问题。

我添加了 Al 描述的 F3 功能。

当我 f3 超过黄色(在上下文中 ctermfg=yellow)时,它返回:

hi<vimHiCtermColor> trans<vimHiCtermColor> lo<vimHiCtermColor> FG:-1 BG:-1

然后 :syn list vimHiCtermColor 返回:

--- Syntax items ---
vimHiCtermColor xxx contained lightmagenta darkgray lightgrey darkgrey lightgreen lightgray darkmagenta gray white red grey darkred brown darkblue darkgreen lightblue yellow cyan
                   contained lightcyan lightred black blue green magenta darkcyan darkyellow

我检查了 :syn list darkgray (我没有定义的东西)看看它是否存在:

--- Syntax items ---
E28: No such highlight group name: darkgray
Hit ENTER or type command to continue

我应该从这里去哪里?

How I can keep all the current formatting for a file type but add functionality.

I would like to highlight colors in .vim files so that each color is highlighted how the terminal will resolve it.

I created a vim.vim file containing:

syn keyword yellow yellow containedin=All                                                    
highlight yellow ctermfg=yellow                                                              
                                                                                             
syn keyword red red containedin=all                                                          
highlight red ctermfg=red                                                                    
                                                                                        

and put it into ~/.vim/after/syntax/vim.vim

As suggested here.

This has no effect.


Update

In fact I was mistaken when I said my changes had no effect. If you type yellow by itself on a line it will be highlighted yellow. Unfortunately this does not solve my problem.

I added the F3 functionality described by Al.

When I f3 over yellow (in the context ctermfg=yellow) it returns:

hi<vimHiCtermColor> trans<vimHiCtermColor> lo<vimHiCtermColor> FG:-1 BG:-1

Then :syn list vimHiCtermColor returns:

--- Syntax items ---
vimHiCtermColor xxx contained lightmagenta darkgray lightgrey darkgrey lightgreen lightgray darkmagenta gray white red grey darkred brown darkblue darkgreen lightblue yellow cyan
                   contained lightcyan lightred black blue green magenta darkcyan darkyellow

I checked :syn list darkgray (something I have not defined) to see if it exists:

--- Syntax items ---
E28: No such highlight group name: darkgray
Hit ENTER or type command to continue

Where should I go from here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不语却知心 2024-09-01 22:41:09

解决方案

这是仅将单词着色为黄色的直接答案。

syn cluster vimHiCtermColors contains=vimHiCtermColorYellow
syn keyword vimHiCtermColorYellow yellow contained

syn match  vimHiCtermFgBg   contained   "\ccterm[fb]g="he=e-1   nextgroup=vimNumber,vimHiCtermColor,@vimHiCtermColors,vimFgBgAttrib,vimHiCtermError

highlight vimHiCtermColorYellow ctermfg=yellow

这是一个为所有彩色终端名称着色的解决方案。它们仅在终端(而不是 GUI)中着色,其他属性(256 色终端、GUI 颜色、粗体等属性)根本不突出显示。为了进一步扩展这一点,您可能需要某种脚本来迭代所有可能的值。

syn cluster vimHiCtermColors contains=vimHiCtermColorBlack,vimHiCtermColorBlue,vimHiCtermColorBrown,vimHiCtermColorCyan,vimHiCtermColorDarkBlue,vimHiCtermColorDarkcyan,vimHiCtermColorDarkgray,vimHiCtermColorDarkgreen,vimHiCtermColorDarkgrey,vimHiCtermColorDarkmagenta,vimHiCtermColorDarkred,vimHiCtermColorDarkyellow,vimHiCtermColorGray,vimHiCtermColorGreen,vimHiCtermColorGrey,vimHiCtermColorLightblue,vimHiCtermColorLightcyan,vimHiCtermColorLightgray,vimHiCtermColorLightgreen,vimHiCtermColorLightgrey,vimHiCtermColorLightmagenta,vimHiCtermColorLightred,vimHiCtermColorMagenta,vimHiCtermColorRed,vimHiCtermColorWhite,vimHiCtermColorYellow

syn keyword vimHiCtermColorBlack black contained
syn keyword vimHiCtermColorBlue blue contained
syn keyword vimHiCtermColorBrown brown contained
syn keyword vimHiCtermColorCyan cyan contained
syn keyword vimHiCtermColorDarkBlue darkBlue contained
syn keyword vimHiCtermColorDarkcyan darkcyan contained
syn keyword vimHiCtermColorDarkgray darkgray contained
syn keyword vimHiCtermColorDarkgreen darkgreen contained
syn keyword vimHiCtermColorDarkgrey darkgrey contained
syn keyword vimHiCtermColorDarkmagenta darkmagenta contained
syn keyword vimHiCtermColorDarkred darkred contained
syn keyword vimHiCtermColorDarkyellow darkyellow contained
syn keyword vimHiCtermColorGray gray contained
syn keyword vimHiCtermColorGreen green contained
syn keyword vimHiCtermColorGrey grey contained
syn keyword vimHiCtermColorLightblue lightblue contained
syn keyword vimHiCtermColorLightcyan lightcyan contained
syn keyword vimHiCtermColorLightgray lightgray contained
syn keyword vimHiCtermColorLightgreen lightgreen contained
syn keyword vimHiCtermColorLightgrey lightgrey contained
syn keyword vimHiCtermColorLightmagenta lightmagenta contained
syn keyword vimHiCtermColorLightred lightred contained
syn keyword vimHiCtermColorMagenta magenta contained
syn keyword vimHiCtermColorRed red contained
syn keyword vimHiCtermColorWhite white contained
syn keyword vimHiCtermColorYellow yellow contained

syn match  vimHiCtermFgBg   contained   "\ccterm[fb]g="he=e-1   nextgroup=vimNumber,@vimHiCtermColors,vimFgBgAttrib,vimHiCtermError

highlight vimHiCtermColorBlack ctermfg=black
highlight vimHiCtermColorBlue ctermfg=blue
highlight vimHiCtermColorBrown ctermfg=brown
highlight vimHiCtermColorCyan ctermfg=cyan
highlight vimHiCtermColorDarkBlue ctermfg=darkBlue
highlight vimHiCtermColorDarkcyan ctermfg=darkcyan
highlight vimHiCtermColorDarkgray ctermfg=darkgray
highlight vimHiCtermColorDarkgreen ctermfg=darkgreen
highlight vimHiCtermColorDarkgrey ctermfg=darkgrey
highlight vimHiCtermColorDarkmagenta ctermfg=darkmagenta
highlight vimHiCtermColorDarkred ctermfg=darkred
highlight vimHiCtermColorDarkyellow ctermfg=darkyellow
highlight vimHiCtermColorGray ctermfg=gray
highlight vimHiCtermColorGreen ctermfg=green
highlight vimHiCtermColorGrey ctermfg=grey
highlight vimHiCtermColorLightblue ctermfg=lightblue
highlight vimHiCtermColorLightcyan ctermfg=lightcyan
highlight vimHiCtermColorLightgray ctermfg=lightgray
highlight vimHiCtermColorLightgreen ctermfg=lightgreen
highlight vimHiCtermColorLightgrey ctermfg=lightgrey
highlight vimHiCtermColorLightmagenta ctermfg=lightmagenta
highlight vimHiCtermColorLightred ctermfg=lightred
highlight vimHiCtermColorMagenta ctermfg=magenta
highlight vimHiCtermColorRed ctermfg=red
highlight vimHiCtermColorWhite ctermfg=white
highlight vimHiCtermColorYellow ctermfg=yellow

解释

如果你在 colors/vim.vim 中查找 cterm,你会看到一行

syn match  vimHiCtermFgBg   contained   "\ccterm[fb]g="he=e-1   nextgroup=vimNumber,vimHiCtermColor,vimFgBgAttrib,vimHiCtermError

这表示,当 ctermfg= 或遇到 ctermbg= 时,将下一个单词突出显示为 vimNumbervimHiCtermColorvimFgBgAttribvimHiCtermError< /代码>。查看 vimHiCtermColor (上面的几行),我们看到

syn keyword vimHiCtermColor contained   black blue brown cyan darkBlue darkcyan darkgray darkgreen darkgrey darkmagenta darkred darkyellow gray green grey lightblue lightcyan lightgray lightgreen lightgrey lightmagenta lightred magenta red white yellow

这列出了所有颜色终端名称,并且它们突出显示为具有相同语法组的关键字。因此,我们可以单独突出显示它们,而不是一起突出显示它们。上述第一个解决方案的四行描述了步骤:

  1. 创建一个新簇 @vimHiCtermColors,其中包含步骤 2 中的每个组。
  2. 为每个颜色值添加一个新关键字。
  3. 修改 vimHiCtermFgBg 定义以使用 @vimHiCtermColors 而不是 vimHiCtermColor
  4. 根据需要突出显示每个关键字。

您尝试的方法不起作用的原因有两个。首先,在 nextgroup 中指定的语法组优先于一般组(特别是您的 yellow 组)。但是,您可能会说,“containedin=ALL 怎么样?”这是第二点。关键字是单独的单元,不能包含任何其他内容。原来的 vimHiCtermColor 组都是关键字,因此您的 containedin=ALL 无法覆盖它。如果 vimHiCtermColor 是匹配项而不是关键字,则它可能有效。

Solution

Here's a direct answer for coloring just the word yellow.

syn cluster vimHiCtermColors contains=vimHiCtermColorYellow
syn keyword vimHiCtermColorYellow yellow contained

syn match  vimHiCtermFgBg   contained   "\ccterm[fb]g="he=e-1   nextgroup=vimNumber,vimHiCtermColor,@vimHiCtermColors,vimFgBgAttrib,vimHiCtermError

highlight vimHiCtermColorYellow ctermfg=yellow

And here's a solution for coloring all the color terminal names. They are only colored in the terminal (not the GUI), and other attributes (256-color terminal, GUI colors, attributes such as bold) are not highlighted at all. To extend this further, you'd probably want some sort of script to iterate over all the possible values.

syn cluster vimHiCtermColors contains=vimHiCtermColorBlack,vimHiCtermColorBlue,vimHiCtermColorBrown,vimHiCtermColorCyan,vimHiCtermColorDarkBlue,vimHiCtermColorDarkcyan,vimHiCtermColorDarkgray,vimHiCtermColorDarkgreen,vimHiCtermColorDarkgrey,vimHiCtermColorDarkmagenta,vimHiCtermColorDarkred,vimHiCtermColorDarkyellow,vimHiCtermColorGray,vimHiCtermColorGreen,vimHiCtermColorGrey,vimHiCtermColorLightblue,vimHiCtermColorLightcyan,vimHiCtermColorLightgray,vimHiCtermColorLightgreen,vimHiCtermColorLightgrey,vimHiCtermColorLightmagenta,vimHiCtermColorLightred,vimHiCtermColorMagenta,vimHiCtermColorRed,vimHiCtermColorWhite,vimHiCtermColorYellow

syn keyword vimHiCtermColorBlack black contained
syn keyword vimHiCtermColorBlue blue contained
syn keyword vimHiCtermColorBrown brown contained
syn keyword vimHiCtermColorCyan cyan contained
syn keyword vimHiCtermColorDarkBlue darkBlue contained
syn keyword vimHiCtermColorDarkcyan darkcyan contained
syn keyword vimHiCtermColorDarkgray darkgray contained
syn keyword vimHiCtermColorDarkgreen darkgreen contained
syn keyword vimHiCtermColorDarkgrey darkgrey contained
syn keyword vimHiCtermColorDarkmagenta darkmagenta contained
syn keyword vimHiCtermColorDarkred darkred contained
syn keyword vimHiCtermColorDarkyellow darkyellow contained
syn keyword vimHiCtermColorGray gray contained
syn keyword vimHiCtermColorGreen green contained
syn keyword vimHiCtermColorGrey grey contained
syn keyword vimHiCtermColorLightblue lightblue contained
syn keyword vimHiCtermColorLightcyan lightcyan contained
syn keyword vimHiCtermColorLightgray lightgray contained
syn keyword vimHiCtermColorLightgreen lightgreen contained
syn keyword vimHiCtermColorLightgrey lightgrey contained
syn keyword vimHiCtermColorLightmagenta lightmagenta contained
syn keyword vimHiCtermColorLightred lightred contained
syn keyword vimHiCtermColorMagenta magenta contained
syn keyword vimHiCtermColorRed red contained
syn keyword vimHiCtermColorWhite white contained
syn keyword vimHiCtermColorYellow yellow contained

syn match  vimHiCtermFgBg   contained   "\ccterm[fb]g="he=e-1   nextgroup=vimNumber,@vimHiCtermColors,vimFgBgAttrib,vimHiCtermError

highlight vimHiCtermColorBlack ctermfg=black
highlight vimHiCtermColorBlue ctermfg=blue
highlight vimHiCtermColorBrown ctermfg=brown
highlight vimHiCtermColorCyan ctermfg=cyan
highlight vimHiCtermColorDarkBlue ctermfg=darkBlue
highlight vimHiCtermColorDarkcyan ctermfg=darkcyan
highlight vimHiCtermColorDarkgray ctermfg=darkgray
highlight vimHiCtermColorDarkgreen ctermfg=darkgreen
highlight vimHiCtermColorDarkgrey ctermfg=darkgrey
highlight vimHiCtermColorDarkmagenta ctermfg=darkmagenta
highlight vimHiCtermColorDarkred ctermfg=darkred
highlight vimHiCtermColorDarkyellow ctermfg=darkyellow
highlight vimHiCtermColorGray ctermfg=gray
highlight vimHiCtermColorGreen ctermfg=green
highlight vimHiCtermColorGrey ctermfg=grey
highlight vimHiCtermColorLightblue ctermfg=lightblue
highlight vimHiCtermColorLightcyan ctermfg=lightcyan
highlight vimHiCtermColorLightgray ctermfg=lightgray
highlight vimHiCtermColorLightgreen ctermfg=lightgreen
highlight vimHiCtermColorLightgrey ctermfg=lightgrey
highlight vimHiCtermColorLightmagenta ctermfg=lightmagenta
highlight vimHiCtermColorLightred ctermfg=lightred
highlight vimHiCtermColorMagenta ctermfg=magenta
highlight vimHiCtermColorRed ctermfg=red
highlight vimHiCtermColorWhite ctermfg=white
highlight vimHiCtermColorYellow ctermfg=yellow

Explanation

If you look in colors/vim.vim and search for cterm, you'll see a line

syn match  vimHiCtermFgBg   contained   "\ccterm[fb]g="he=e-1   nextgroup=vimNumber,vimHiCtermColor,vimFgBgAttrib,vimHiCtermError

This says that, when ctermfg= or ctermbg= is encountered, highlight the next word as vimNumber, vimHiCtermColor, vimFgBgAttrib, or vimHiCtermError. Looking at vimHiCtermColor (a few lines above), we see

syn keyword vimHiCtermColor contained   black blue brown cyan darkBlue darkcyan darkgray darkgreen darkgrey darkmagenta darkred darkyellow gray green grey lightblue lightcyan lightgray lightgreen lightgrey lightmagenta lightred magenta red white yellow

This lists all of the color terminal names, and they are highlighted as keywords with the same syntax group. So, instead of highlighting them all together, we can highlight them separately. The four lines of the first solution above describe the steps:

  1. Create a new cluster, @vimHiCtermColors containing each of the groups in step 2.
  2. Add a new keyword for each color value.
  3. Modify the vimHiCtermFgBg definition to use @vimHiCtermColors instead of vimHiCtermColor.
  4. Highlight each keyword as you wish.

The reason why what you tried did not work is twofold. First, the syntax groups specified in the nextgroup are preferred over general groups (your yellow group, in particular). But, you may say, "What about containedin=ALL?" This is the second point. Keywords are individual units and cannot contain anything else. The original vimHiCtermColor group was all keywords, so your containedin=ALL could not override it. If vimHiCtermColor had been a match instead of a keyword, it may have worked.

泪眸﹌ 2024-09-01 22:41:09

我希望 ~/.vim/after/syntax/vim.vim 方法能够工作,但是如果关键字(黄色)已经突出显示,您可能需要更改匹配器,例如

syn keyword yellow yellow containedin=ALL

:是您要突出显示的文件吗?

编辑

我查看了 java.vim 文件,看起来有很多重叠的语法组,这会使突出显示自定义变得非常困难。这就是我所做的,以防有帮助。

一个有用的映射是:

" What is the current syntax highlighting group?
map <F3> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<' . synIDattr(synID(line("."),col("."),0),"name") . "> lo<" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">" . " FG:" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"fg#") . " BG:" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"bg#")<CR>

然后您可以将光标移到您感兴趣的内容上,然后按 F3 查看当前突出显示组是什么。作为测试,我在语法目录中打开 java.vim 并转到第 52 行,该行定义了 javaStorageClass。我决定突出显示该行上的“瞬态”一词(因为“黄色”不在文件中的任何位置,而我需要使用一些东西)。

我将光标移到 transient 上并按 F3。 Vim 报告:

hi<vimSynKeyRegion> trans<vimSynKeyRegion> lo<vimSynKeyRegion> FG: BG:

它显然是 vimSynKeyRegion 的一部分,从名称中可以猜出它是 syn 区域。我决定进一步研究这个问题,因此我询问了当前的突出显示配置:

:redir @a>
:silent syn list
:redir END
:vnew
"ap

这会生成一个包含所有语法信息的文件。我搜索了 vimSynKeyRegion 并找到了这一行:

vimSynKeyRegion xxx matchgroup=vimGroupName start=/\k\+/ skip=/\\\\\|\\|/ matchgroup=vimSep end=/|\|$/  contained oneline keepend contains=@vimSynKeyGroup 

vimSynKeyRegion 已配置为包含名为 vimSynKeyGroup 的语法集群中的项目。因此,我们可以通过将瞬态作为该组中的关键字来突出显示它:

:syn keyword MyNewGroup transient
:hi MyNewGroup guifg=#ff00ff
:syn cluster vimSynKeyGroup add=MyNewGroup

虽然这可能不完全适合您想要做的事情,但希望它能为您提供一些可以使用的东西。您可能会发现某些部分无法覆盖(如果它们与关键字或类似内容匹配),但您始终可以重新定义突出显示以更改颜色,例如

:hi vimGroupName guifg=#ffff00

希望所有这些都有帮助。

I would expect the ~/.vim/after/syntax/vim.vim approach to work, but if the keyword (yellow) is already highlighted, you may need to change the matcher with something like:

syn keyword yellow yellow containedin=ALL

What is the file you're trying to highlight?

Edit

I've had a look at the java.vim file and it looks like there's a lot of overlapping syntax groups, which can make highlight customisation quite difficult. Here's what I did, in case it's helpful.

A useful mapping is:

" What is the current syntax highlighting group?
map <F3> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<' . synIDattr(synID(line("."),col("."),0),"name") . "> lo<" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">" . " FG:" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"fg#") . " BG:" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"bg#")<CR>

You can then move the cursor over something you're interested in and press F3 to see what the current highlight group is. As a test, I opened java.vim in the syntax directory and went to line 52, which defines a javaStorageClass. I decided to highlight the word transient on that line (as yellow wasn't in the file anywhere and I needed something to work with).

I moved the cursor over transient and pressed F3. Vim reported:

hi<vimSynKeyRegion> trans<vimSynKeyRegion> lo<vimSynKeyRegion> FG: BG:

It is obviously part of a vimSynKeyRegion, which one would guess from the name is a syn region. I decided to look at this further, so I interrogated the current highlighting configuration:

:redir @a>
:silent syn list
:redir END
:vnew
"ap

This produces a file containing all of the syntax information. I searched for vimSynKeyRegion and found this line:

vimSynKeyRegion xxx matchgroup=vimGroupName start=/\k\+/ skip=/\\\\\|\\|/ matchgroup=vimSep end=/|\|$/  contained oneline keepend contains=@vimSynKeyGroup 

The vimSynKeyRegion has been configured to contain items from the syntax cluster named vimSynKeyGroup. Therefore, we can highlight transient by making it a keyword in this group:

:syn keyword MyNewGroup transient
:hi MyNewGroup guifg=#ff00ff
:syn cluster vimSynKeyGroup add=MyNewGroup

Although this may not fit exactly into what you're wanting to do, hopefully it will give you something to work with. You may find some parts cannot be overridden (if they're matched with keywords or similar), but you can always redefine the highlighting to change the colour, e.g.

:hi vimGroupName guifg=#ffff00

Hope all of that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文