How does vim choose a color that is not in the terminals color palette?

29 views
Skip to first unread message

S. Jacobi

unread,
Jun 14, 2017, 8:35:39 PM6/14/17
to v...@vim.org
I am using termite as my terminal emulator, which advertises 256 colors
support via terminfo. I only defined 20 colors: fg, fg_bold, bg, cursor
and the 16 colors in their normal and bright variants.
I do not use a vim colorscheme, just the terminal colors. In some shell
scripts I see colors that I have not defined and which are not affected
if I change my color palette. So my question is: How are those colors
determined and how can I change them?

How to reproduce:
1) set t_Co=256 in vimrc (if it is set to 16 those "new" colors do not
appear)
2) I set all 16 colors in my termite config file to #ff0000 (to
oberserve the changes)
3) Minimal shell script:
#/bin/bash
$testvar=1
cd dir
./command --help
4) $testvar is in cyan, --help is in some pinkish color

I haven't tested setting all 256 colors in my config file to red, if
this affects those two cases I found.

Kind regards

Tony Mechelynck

unread,
Jun 14, 2017, 8:54:50 PM6/14/17
to vim_use, v...@vim.org, sja...@mailueberfall.de

First, check that Vim knows that yout terminal allows 256 colors:

:set t_Co?

The answer should be

t_Co=256

If it isn't, and you know that your current console _does_ support 256 colors, you can set it. Even in your vimrc if you know how to identify that terminal (as opposed to a dumb 16-color text console) at startup, e.g. from its 'term' value.

Then, if you don't want to bother to identify where (at which ordinal) in the terminal palette a certain pink or green or whichever color is found, I recommend to use gui-like settings in the cterm. There are two ways to do that:

a) (easiest)
if has('termguicolors')
set termguicolors
endif

b) to use guifg= guibg= in the cterm even with a gui-enabled Vim not compiled with +termguicolors, you could also take advantage of the CSApprox plugin, see
http://vim.wikia.com/wiki/Using_GUI_color_settings_in_a_terminal#Solution_2:_the_CSApprox_plugin


Best regards,
Tony.

S. Jacobi

unread,
Jun 15, 2017, 7:16:45 AM6/15/17
to vim...@googlegroups.com
Thanks for your answer. Me setting t_Co=256 in my vimrc was just
keeping this line for testing purposes. If t_Co is not set at all, it
is set automatically to 256 because this is what termite adertises via
terminfo. I can manually force it to 16 so the "extra" colors are not
used.
My vim was not compiled with the termguicolors feature, so I don't know
if b) applies here, too.
But I am actually interested in the internals. Is there a way to find
the color index without manually checking color16 to color 255? Or
even a comprehensive list of all uses of colorX with X > 15? No vim
color scheme is used.

Christian Brabandt

unread,
Jun 15, 2017, 2:48:21 PM6/15/17
to v...@vim.org
I think those are hard coded default values see syntax/syncolor.vim

Best,
Christian
--
F: Was ist orange und bohrt sich in die Wand?
A: Eine BOrange!

Tony Mechelynck

unread,
Jun 15, 2017, 6:21:54 PM6/15/17
to vim...@googlegroups.com
The CSApprox plugin was written before the +termguicolors feature was
even invented. It applies regardless of its presence, in any Vim
version which can remember GUI highlight settings while running in the
console. I tried to find out which Vim version this means but failed.
If when running Vim in a console and setting both (gui= guibg= guifg=
guisp= ) and (cterm= ctermfg= ctermbg= ) parts of the :highlight
command they are both remembered, then you should be all right.

> But I am actually interested in the internals. Is there a way to find
> the color index without manually checking color16 to color 255? Or
> even a comprehensive list of all uses of colorX with X > 15? No vim
> color scheme is used.

From the CSApprox plugin help:
[...]
Also, there are at least three different 256-color palettes in use. Nearly
all terminals use an xterm-compatible palette, so most users need not concern
themselves with this, with only two exceptions: Eterm uses a slightly
different palette, and older Konsole (pre KDE 2.2.0) used a third palette.
CSApprox has no reliable way to tell which palette your terminal uses, so it
makes some educated guesses:
[...]

There is also a "colortest" plugin, see ":help colortest.vim" — but by
default it will display the basic bg & fg colors by setting special
highlight groups to them. Vim cannot define anywhere near 16777216²,
or even 256², highlight groups at the same time so it is not possible
to get a list of all possible combinations displayed. IIUC the 256
colors are subdivided in 3 categories:
- Colors 0 to 0x0F are the colors used by 16-color terminals
- 216 (i.e. 6³) positions are used for the so-called "safe" colors,
i.e. those of the 16777216 for which the red, green and blue settings
are each (separately) a multiple of 0x33, namely one of {0x00, 0x33,
0x66, 0x99, 0xCC, 0xFF} for red, one of the same list for green and
one of the same list for blue.
- The rest (24 positions) are additional greyscale values.

Best regards,
Tony.
Reply all
Reply to author
Forward
0 new messages