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?
Solution
Here’s a direct answer for coloring just the word 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.
Explanation
If you look in colors/vim.vim and search for
cterm, you’ll see a lineThis says that, when
ctermfg=orctermbg=is encountered, highlight the next word asvimNumber,vimHiCtermColor,vimFgBgAttrib, orvimHiCtermError. Looking atvimHiCtermColor(a few lines above), we seeThis 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:
@vimHiCtermColorscontaining each of the groups in step 2.vimHiCtermFgBgdefinition to use@vimHiCtermColorsinstead ofvimHiCtermColor.The reason why what you tried did not work is twofold. First, the syntax groups specified in the
nextgroupare preferred over general groups (youryellowgroup, in particular). But, you may say, “What aboutcontainedin=ALL?” This is the second point. Keywords are individual units and cannot contain anything else. The originalvimHiCtermColorgroup was all keywords, so yourcontainedin=ALLcould not override it. IfvimHiCtermColorhad been a match instead of a keyword, it may have worked.