0

Follow up to this question - I've narrowed the issue down to the fact that set t_Co=16 in ~/.vimrc is not being applied.

SO post on restricting vim to 16 colors.

If I open vim and execute :echo &t_Co, value is set to 256. If I then manually execute :source ~/.vimrc, then :echo &t_Co again, I see the desired value of 16.

I've removed every plugin in my config to see if that was causing the issue but it persists anyway. Below are relevant .vimrc settings - this is the very end of the file:

set notermguicolors
set t_Co=16
syntax on

highlight clear CursorLine
highlight clear Todo
highlight signcolumn ctermbg=8
highlight statuslinenc ctermbg=3 ctermfg=8
highlight matchparen ctermbg=1 ctermfg=7
highlight diffchange ctermbg=0
highlight difftext ctermfg=8

I don't see any references to t_Co when inspecting via vim -V either. FWIW this issue started happening only after a recent iTerm2 and MacOS update. I've tried putting t_Co before syntax on, after it, before notermguicolors, after it, at the beginning of the file, at the end of the file. I presume something is happening during startup outside of .vimrc that does not happen again when I manually :source ~/.vimrc, but not sure where to look next.

I could set iTerm2 to an ansi terminal to limit colors that way but that was messing up other stuff. I would like t_Co to dictate available colors in vim, and to be applied on startup.

Potentially related SO post

Setting t_Co to 256 while leaving term to its Vim-defined value — or, more generally, setting t_Co and/or term to values that don't match with the host terminal — makes no sense and will likely create troubles when Vim sends a signal that is not understood by the terminal or vice-versa.

Thanks!

1 Answer 1

1

This is an attempt to help you solve your problem, not an attempt to solve it. I don't think this place is the right place for this.

So…

  • I use Terminal.app with xterm-256color as $TERM, where Vim behaves as expected.

  • I just installed the latest stable release of iTerm.app (3.5.1), which has xterm-256color set by default and I can confirm that:

    • doing :echo &t_Co in Vim prints 256 despite having set t_Co=16 in $MYVIMRC,
    • doing :echo &t_Co after manually sourcing $MYVIMRC effectively prints 16.
  • Following a hunch, I compared the output of $ infocmp in Terminal.app and iTerm.app, which was quite interesting. The built-in one is quite different from the system one:

    1c1
    < #   Reconstructed via infocmp from file: /opt/local/share/terminfo/78/xterm-256color
    ---
    > #   Reconstructed via infocmp from file: /private/var/folders/q4/8ckdmdb136z10l1nh7ss_hsw0000gn/T/AppTranslocation/9530E85E-6C12-4995-A7A5-0592242AA0BC/d/iTerm.app/Contents/Resources/terminfo/78/xterm-256color
    4c4
    <     colors#0x100, cols#80, it#8, lines#24, pairs#0x10000,
    ---
    >     colors#0x100, cols#80, it#8, lines#24, pairs#0x7fff,
    20,21c20,21
    <     ka3=\EOy, kb2=\EOu, kbeg=\EOE, kbs=^H, kc1=\EOq, kc3=\EOs,
    <     kcbt=\E[Z, kcub1=\EOD, kcud1=\EOB, kcuf1=\EOC, kcuu1=\EOA,
    ---
    >     ka3=\EOy, kb2=\EOu, kbs=^H, kc1=\EOq, kc3=\EOs, kcbt=\E[Z,
    >     kcub1=\EOD, kcud1=\EOB, kcuf1=\EOC, kcuu1=\EOA,
    40c40
    <     kind=\E[1;2B, kmous=\E[<, knp=\E[6~, kpp=\E[5~,
    ---
    >     kind=\E[1;2B, kmous=\E[M, knp=\E[6~, kpp=\E[5~,
    42,46c42,46
    <     memu=\Em, mgc=\E[?69l, nel=\EE, oc=\E]104\007,
    <     op=\E[39;49m, rc=\E8, rep=%p1%c\E[%p2%{1}%-%db,
    <     rev=\E[7m, ri=\EM, rin=\E[%p1%dT, ritm=\E[23m, rmacs=\E(B,
    <     rmam=\E[?7l, rmcup=\E[?1049l\E[23;0;0t, rmir=\E[4l,
    <     rmkx=\E[?1l\E>, rmm=\E[?1034l, rmso=\E[27m, rmul=\E[24m,
    ---
    >     memu=\Em, mgc=\E[?69la, oc=\E]104\007, op=\E[39;49m,
    >     rc=\E8, rep=%p1%c\E[%p2%{1}%-%db, rev=\E[7m, ri=\EM,
    >     rin=\E[%p1%dT, ritm=\E[23m, rmacs=\E(B, rmam=\E[?7l,
    >     rmcup=\E[?1049l\E[23;0;0t, rmir=\E[4l, rmkx=\E[?1l\E>,
    >     rmm=\E[?1034l, rmso=\E[27m, rmul=\E[24m,
    48,49c48,51
    <     setab=\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;5;%p1%d%;m,
    <     setaf=\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5;%p1%d%;m,
    ---
    >     setab=\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48:5:%p1%d%;m,
    >     setaf=\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38:5:%p1%d%;m,
    >     setb=\E[4%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m,
    >     setf=\E[3%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m,
    52,54c54
    <     smcup=\E[?1049h\E[22;0;0t, smglp=\E[?69h\E[%i%p1%ds,
    <     smglr=\E[?69h\E[%i%p1%d;%p2%ds,
    <     smgrp=\E[?69h\E[%i;%p1%ds, smir=\E[4h, smkx=\E[?1h\E=,
    ---
    >     smcup=\E[?1049h\E[22;0;0t, smir=\E[4h, smkx=\E[?1h\E=,
    
  • I disabled the "Set $TERMINFO_DIRS…" option from the "Advanced" tab so that iTerm.app uses the same xterm-256color as Terminal.app but it didn't change the observed behavior.

  • It looks like something else than the $TERM in iTerm.app is causing Vim to either ignore the set t_Co=16 in your vimrc or overrides it somehow. FWIW, I tried to play with other color-related options in iTerm.app without luck.

You should head off to Vim's issue tracker.

Good luck.


Here is a quick workaround until the problem is sorted upstream:

" before your highlight commands
autocmd! ColorScheme default set t_Co=16
colorscheme default

where we:

  1. add an autocommand that is triggered when we explicitly tell Vim to use the default colorscheme,
  2. use that autocommand to set t_Co=16,
  3. tell Vim to use the default colorscheme.
2
  • This was super informative, thank you. Something of note: after adding your workaround to my config, :echo &t_Co shows 16 as expected. However, my syntax colors are STILL not limited to 16, and manually sourcing my vimrc STILL results in a change to the colors used. So sounds like something else is afoot as well. Commented Jun 17 at 15:58
  • Piling up hi… commands in one's vimrc has never been very reliable to begin with, so you should create a full-fledged colorscheme, or at least follow the instructions at :help colorscheme-override.
    – romainl
    Commented Jun 17 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.