Vim: Difference between t_Co=256 and term=xterm-256color in conjunction with TMUX

44,912

Solution 1

When you don't use tmux or screen, you only need to configure your 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 it will depend on the terminal emulator and is outside of the scope of your question and this answer.

You don't need to do anything in Vim as it's perfectly capable to do the right thing by itself.

When you use tmux or screen, those programs set their own default value for $TERM, usually screen, and Vim does what it has to do with the info it is given.

If you want a more uniform (and colorful) behavior, you must configure them to use a "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 tell Vim they support 256 colors and Vim will do what you expect it to do.

edit

My answer assumes that you are able to edit those configuration files but, since you are able to edit your ~/.vimrc, I don't think that I'm that far off the mark.

edit 2

The value of the term option (retrieved with &term) is the name of the terminal as picked up by Vim upon startup. That name is what you are supposed to setup in your terminal emulator itself.

The value of the t_Co option (&t_Co) is what Vim considers to be the maximum number of colors that can be displayed by 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 up, it gets the value of the TERM environment variable, queries the terminfo database with that value and stores a number of informations on its environment in several t_… variables among which… the number of colors available in t_Co. Given a "legal" terminal type (one that Vim can lookup), Vim always assumes the correct number of colors.

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.

While it is entirely possible to do so, messing with t_Co and term in Vim is both totally useless and possibly harmful.

Again, just setup your terminal emulators and terminal multiplexers correctly. That's really all you need.

If you end up in a terminal multiplexer or a terminal emulator where you can't define a correct TERM, then and only then you can force Vim to assume 256 colors. To that end, 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 configure each individual part:

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

and you are done.

If you can't control every part, use a simple conditional in your ~/.vimrc to set t_Co according to &term but don't change the value of term.

But if you can edit a ~/.vimrc there's no reason you can't edit a ~/.screenrc or ~/.tmux.conf or ~/.bashrc or whatever.

Solution 2

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

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

It's often helpful to set it explicitly in case the OS value is incorrect. This is especially true if you're connecting over a network from a terminal emulator that doesn't provide the correct value.

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

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

Share:
44,912
Steven Lu
Author by

Steven Lu

Play a multitouch HTML5 Tetris clone -- http://htmltetris.com (Interesting note about this site. It used to be my site, then Tetris Co. sent me a cease and desist, then I forgot about it, and now it’s back: someone brought it back and put MY code back on the site.) A huge fan of tmux and vim.

Updated on July 09, 2022

Comments

  • Steven Lu
    Steven Lu almost 2 years

    I am testing the various different terminals that I tend to use to SSH into Linux boxes that I have Tmux set up on.

    Basically I noticed this behavior, and I am hoping that somebody could offer an explanation of what's going on. Now it may be the case that this is specific behavior that affects the Prompt app.

    I am using Vim within Tmux, and on Panic's Prompt app on my iPhone5 I was having the behavior that 256 colors were not enabling when the .vimrc set the colors using the set t_Co=256 directive. Here, Vim was correctly displaying the colors when it was not run through Tmux. Also, OS X's Terminal.app correctly rendered the colors (I did not test PuTTY with this on windows unfortunately) with Vim in Tmux.

    Then I swapped out set t_Co=256 for set term=xterm-256color and now the colors work when using Vim through Tmux.

    Note also that I tested both set -g default-terminal "xterm-256color" and set -g default-terminal "screen-256color" settings for Tmux and this change had no effect on behavior.

  • Steven Lu
    Steven Lu about 11 years
    I have already specified in the question that this is not the answer. Now I realize this answer might help other readers (because it goes over things that I have already fully considered and tested) but is not the answer to the question about the difference between those two vim settings.
  • Steven Lu
    Steven Lu about 11 years
    I never realized I could set both at the same time. I think that probably has the best chances of making things "just work". Thanks
  • Steven Lu
    Steven Lu about 11 years
    The primary point of contention here is, why, when I tell Tmux to specify via set -g default-terminal "xterm-256color" and i verify by running in tmux echo $TERM it reports xterm-256color, that vim running inside tmux is still unable to provide proper coloring unless I use vim's set term=xterm-256color? At least on my iphone within prompt it does that.
  • romainl
    romainl about 11 years
    It is the answer to the underlying problem. You don't need to do anything in Vim at all.
  • romainl
    romainl about 11 years
    @StevenLu, you must use screen-256color, not xterm-256color.
  • Jim Stewart
    Jim Stewart about 11 years
    I use xterm-256color everywhere. Terminal emulator settings, Linux shell config (zsh), Vim term setting, tmux default-terminal setting. I don't use screen-256color anywhere.
  • romainl
    romainl about 11 years
    screen-256color is the recommended color TERM for tmux. Fixing color problems in tmux is as easy as setting it properly and not trying to mess with vim-specific hacks. Frankly, all these shenanigans within one's ~/.vimrc are absolutely useless.
  • Steven Lu
    Steven Lu about 11 years
    Quite thorough an answer this has become. Thanks
  • Jim Stewart
    Jim Stewart about 11 years
    @romainl is right; if I set iTerm2 to screen-256color and completely omit any terminal settings in zsh, tmux, and vim, it all works fine. I know I put some of those shenanigans in for a reason, but it was a couple years ago with Linux, no tmux, and gnome-terminal. Probably fine there with just screen-256color as well, though.
  • Armen Michaeli
    Armen Michaeli almost 9 years
    Based on my experience, I consider this among the very best answers on terminals from all SO answers I had to plow through during my time spent to understand terminals, their color systems, vim, tmux and all that stuff. It explains the basics correctly, and I am willing to bet half of the problems people experience with their terminals and colors are because they do not understand what is going on (well, duh) but are willing to blindly copy paste stuff in their shell startup scripts and configuration files. This answer should be bookmarked, and hopefully elaborated. Well done.
  • 3.141592
    3.141592 about 4 years
    i totally agree with the comment from @amn above :) awesome anser @romainl! really, great explenation. thank you :)
  • mkjeldsen
    mkjeldsen over 3 years
    On Vim 8, everything may not just work. Firstly, this answer doesn't delve into the minutia of the terminfo database, which is what makes everything work: github.com/mirror/ncurses/blob/master/misc/terminfo.src. Entries can be missing necessary configuration to instruct clients what color capabilities the terminal supports. Secondly, even if all the relevant terminfo entries are correct, Vim might show colors for xterm-* entries but not others; in that case, see ":h xterm-true-color".