Vim: difference between t_Co = 256 and term = xterm-256color in combination with TMUX

I test the various terminals that I usually use for SSH in Linux boxes on which I configured Tmux.

I mainly noticed this behavior, and I hope someone can offer an explanation of what is happening. Now it may happen that this is a specific behavior that affects the Prompt application.

I use Vim in Tmux, and in the Panic Prompt application on my iPhone5 I had a behavior that did not support 256 colors when .vimrc set colors using the set t_Co=256 directive. Here Vim displayed colors correctly when it did not start through Tmux. Also, OS X Terminal.app displayed colors correctly (I did not test PuTTY with this on windows, unfortunately) with Vim in Tmux.

Then I replaced set t_Co=256 with set term=xterm-256color , and now the colors work when using Vim through Tmux.

Note also that I tested the set -g default-terminal "xterm-256color" and set -g default-terminal "screen-256color" settings for Tmux, and this change did not affect the behavior.

+27
vim terminal xterm tmux
Mar 13 '13 at 2:29
source share
2 answers

If you do not use tmux or screen , you only need to configure terminal emulators to advertise themselves as "capable of displaying 256 colors" by setting their TERM to xterm-256color or any comparable value that works with your terminals and platforms. How you do this will depend on the terminal emulator and is beyond the scope of your question and this answer.

You do not need to do anything in Vim, as it is perfectly capable of doing the right thing on its own.

When you use tmux or screen , these programs set their own default value for $TERM , usually screen , and Vim does what is relevant to the information it provides.

If you want more uniform (and colorful) behavior, you must configure them to use the "better" value for $TERM :

  • tmux

    Add this line to ~/.tmux.conf :

     set -g default-terminal "screen-256color" 
  • Screen

    Add this line to ~/.screenrc :

     term "screen-256color" 

Now both multiplexers will say that Vim supports 256 colors, and Vim will do what you expect from it.

change

My answer suggests that you can edit these configuration files, but since you can edit your ~/.vimrc , I don't think I'm so far from the mark.

change 2

The TERM parameter value (obtained using &term ) is the name of the terminal that Vim received at startup. This name is what you should configure in your terminal emulator.

The value of t_Co ( &t_Co ) is what Vim considers to be the maximum number of colors that can be displayed on the host terminal. It is defined according to the entry corresponding to $TERM in terminfo :

  term | t_Co -----------------+------ xterm | 8 xterm-256color | 256 screen | 8 screen-256color | 256 

When Vim starts, it receives the value of the TERM environment variable, queries the terminfo database with that value, and stores several environmental information in several t_… variables, among which ... the amount of color is available in t_Co . Given the β€œlegal” type of terminal (the one that Vim can find), Vim always accepts the correct number of colors.

Setting t_Co to 256 , leaving TERM in its Vim-specific value - or, more generally, setting t_Co and / or TERM values ​​that do not match the host terminal - does not make sense and is likely to cause problems when Vim sends a signal that is not understood by the terminal or vice versa.

While it is entirely possible to do, messing with t_Co and TERM in Vim is completely useless and possibly harmful.

Again, configure terminal emulators and terminal multiplexers correctly. That is really all you need.

If you end up in a terminal multiplexer or terminal emulator where you cannot determine the correct TERM , then and only then can you force Vim to accept 256 colors. For this purpose, changing the value of t_Co is the only thing that makes sense:

 if &term == "screen" set t_Co=256 endif 

So ... if you can customize every single part:

  • terminal emulator: xterm-256color
  • tmux / screen: screen-256color
  • vim: nothing

and you're done.

If you cannot control every part, use the conditional ~/.vimrc simply set t_Co according to &term , but do not change the TERM value.

But if you can edit ~/.vimrc , there is no reason why you cannot edit ~/.screenrc or ~/.tmux.conf or ~/.bashrc or anything else.

+57
Mar 13 '13 at 6:56
source share

You can use both set t_Co=256 and set term=xterm-256color together.

term tells Vim which terminal type to use, which controls the display / display of all aspects of Vim, including how to map key input, redraw the screen, move the cursor, display colors, etc. Normally, Vim can output this on its own using the term environment variable provided by your OS.

It is often useful to set it explicitly if the OS value is incorrect. This is especially true if you are connecting over the network from a terminal emulator that does not provide the correct value.

t_Co is one of many terminal options (used by the termcap system that Vim uses to control the terminal). It specifically refers to the number of colors supported by the terminal. Sometimes you need to override this if the terminal emulation is mostly correct, but Vim incorrectly determines the number of colors supported.

I use both of these options in my .vimrc to make sure Vim uses 256 colors in tmux using all the terminals I like (Ubuntu gnome-terminal, OSX iTerm2 and Windows KiTTY). I also have most of these terminals explicitly configured to send xterm-256color as their terminal type.

+7
Mar 13 '13 at 2:49
source share



All Articles