't_Co' is always set to 256 if 'nocompatible' is set

234 views
Skip to first unread message

fech...@gmail.com

unread,
Nov 3, 2013, 10:44:55 AM11/3/13
to vim...@googlegroups.com
Since I updated to Vim 7.4 I'm seeing this strange behaviour:

$ echo $TERM
xterm-16color
$ vim
:set t_Co?
t_Co=256

I first suspected that something is wrong with my vimrc, but starting without
a vimrc behaves the same:

$ vim -Nu NONE
:set t_Co?
t_Co=256

What's really wierd is, that if I start Vim without 'nocompatible' then 't_Co'
is set to the correct value:

$ vim -u NONE
:set t_Co?
t_Co=16

This was definitely *not* happening with older versions of Vim. Now, is this
a bug or somehow intended? Can anybody reproduce this?

Thanks,
Dustin

Bram Moolenaar

unread,
Nov 3, 2013, 5:12:33 PM11/3/13
to fech...@gmail.com, vim...@googlegroups.com
When you set Vim to be not-compatible, it checks the returned info from
xterm to figure out the number of supported colors. So your xterm
probably actually supports 256 colors, right?
See ":help xterm-codes".

--
What do you get when you cross a joke with a rehtorical question?

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

fech...@gmail.com

unread,
Nov 3, 2013, 6:55:28 PM11/3/13
to vim...@googlegroups.com, fech...@gmail.com
> When you set Vim to be not-compatible, it checks the returned info from
> xterm to figure out the number of supported colors. So your xterm
> probably actually supports 256 colors, right?

Yes, the xterm I'm using supports 256 colors. I actually switched my
operating system recently, so thats probably the reason I didn't experienced
this before (can't remember how many colors xterm supported there).

I set TERM to 'xterm-16color' because I want all programs (and especially Vim)
to use only 16 colors.

Anyway, it seems like I have to hardcode 't_Co' in my vimrc which is quite ugly.
Or can I somehow force Vim to use the appropriate terminfo entry instead?

Tony Mechelynck

unread,
Nov 3, 2013, 8:14:24 PM11/3/13
to vim...@googlegroups.com
The colours Vim uses depend on its colorscheme. If you don't set any,
you get the default colours, which use only the 16 color codes
compatible with any color depth (or maybe even 16 foreground and 8
background colours; there are still some consoles in use today which
don't go higher than that). Most programs displaying on the console do
the same; many even use only black & white, possibly with underline if
available (on some consoles, underlining becomes blue color).

To display more than 16 colours on a console in Vim, you need a
colorscheme which defines highlights with ctermfg= and/or ctermbg=
values higher than 15, and those who do may assume that the user knows
hat he's doing, and not check that t_Co is high enough (they could test
if they wanted to).

So I think your fears are unfounded. If you don't set any colorscheme,
or if you choose one that supports 16-color consoles, Vim won't use
more. Of course, gvim uses all 2^24 (or 16777216) colors available on
modern color graphic displays.

However, even so, you can force Vim to ignore the t_Co value returned by
the xterm, as follows (untested):

:autocmd TermResponse * set t_Co=16
or even
:au TermResponse * if &term ~= '^xterm-\=16'
\ | set t_Co=16 | endif

This will be triggered only when the response comes back from the xterm,
thus not on any other terminals. The condition in the if clause means:
if 'term' starts with "xterm16" or "xterm-16". The single quotes are
essential in order to pass the backslash as-is to the regex engine.

I'm writing this second example on two lines with \ line continuation
for legibility, but note that continuation lines are not supported in
'compatible' mode. If this is a concern, you may prefer to write
everything on one long line.


Best regards,
Tony.
--
More are taken in by hope than by cunning.
-- Vauvenargues

Message has been deleted

fech...@gmail.com

unread,
Nov 4, 2013, 6:06:27 AM11/4/13
to vim...@googlegroups.com
Sorry for reposting this message, I accidently hit the post button when
I wasn't finished yet:

Hey,

first of all, thanks for your detailed and very helpful post :)

On Monday, November 4, 2013 2:14:24 AM UTC+1, Tony Mechelynck wrote:
> The colours Vim uses depend on its colorscheme. If you don't set any,
> you get the default colours, which use only the 16 color codes
> compatible with any color depth (or maybe even 16 foreground and 8
> background colours; there are still some consoles in use today which
> don't go higher than that).

Hmm, are you sure about this? At least on my system the default colorscheme
uses more than 16 colors if t_Co is set high enough:

:colorscheme
default
:set t_Co?
t_Co=256
:highlight
SpecialKey xxx term=bold ctermfg=81 guifg=Cyan
Directory xxx term=bold ctermfg=159 guifg=Cyan
Visual xxx term=reverse ctermbg=242 guibg=DarkGrey
(only a few examples)

The funny thing is, that if TERM is 'xterm-16color' then all of these
"higher" color definitions are actually shown just as normal text (without
color), which for example breaks visual mode.
If on the other side TERM is just 'xterm' Vim can display 256 colors.
Really weird.

> To display more than 16 colours on a console in Vim, you need a
> colorscheme which defines highlights with ctermfg= and/or ctermbg=
> values higher than 15, and those who do may assume that the user knows
> hat he's doing, and not check that t_Co is high enough (they could test
> if they wanted to).

Like stated above, the default colorscheme also does this.

> So I think your fears are unfounded. If you don't set any colorscheme,
> or if you choose one that supports 16-color consoles, Vim won't use
> more.

Well, for me the default colorscheme is totally broken if I set TERM to
'xterm-16color', so I wouldn't say that my "fears are unfounded" :)

> However, even so, you can force Vim to ignore the t_Co value returned by
> the xterm, as follows (untested):
>
> :autocmd TermResponse * set t_Co=16
>
> or even
>
> :au TermResponse * if &term ~= '^xterm-\=16'
> \ | set t_Co=16 | endif

Thanks, I will try that. This seems to be the best solution for now.

Regards,
Dustin

fech...@gmail.com

unread,
Nov 5, 2013, 12:23:34 PM11/5/13
to vim...@googlegroups.com
On Monday, November 4, 2013 2:14:24 AM UTC+1, Tony Mechelynck wrote:
> However, even so, you can force Vim to ignore the t_Co value returned by
> the xterm, as follows (untested):
>
> :autocmd TermResponse * set t_Co=16
> or even
> :au TermResponse * if &term ~= '^xterm-\=16'
> \ | set t_Co=16 | endif

I now actually tried your suggestion and it didn't work. I think the
TermResponse event gets triggered before Vim interprets the response
from xterm, so the change of t_Co is overwritten later by Vim.

I also tried countless other things in my vimrc to force t_Co to 16, but
nothing worked.

Finally, I found out that xterm has an option called 'allowTcapOps'.
After setting it to false Vim is no longer able to query xterm, and t_Co
is set to the correct value now.

Reply all
Reply to author
Forward
0 new messages