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?
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: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:
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 (asyellow
wasn't in the file anywhere and I needed something to work with).I moved the cursor over
transient
and pressed F3. Vim reported: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:This produces a file containing all of the syntax information. I searched for
vimSynKeyRegion
and found this line:The
vimSynKeyRegion
has been configured to contain items from the syntax cluster namedvimSynKeyGroup
. Therefore, we can highlighttransient
by making it a keyword in this group: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.
Hope all of that helps.
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:
@vimHiCtermColors
containing each of the groups in step 2.vimHiCtermFgBg
definition to use@vimHiCtermColors
instead ofvimHiCtermColor
.The reason why what you tried did not work is twofold. First, the syntax groups specified in the
nextgroup
are preferred over general groups (youryellow
group, in particular). But, you may say, "What aboutcontainedin=ALL
?" This is the second point. Keywords are individual units and cannot contain anything else. The originalvimHiCtermColor
group was all keywords, so yourcontainedin=ALL
could not override it. IfvimHiCtermColor
had been a match instead of a keyword, it may have worked.