45

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
  • 41,389
  • 58
  • 210
  • 364
  • 1
    vim < ssh < tmux < smartphone… could someone tell me at which point we jumped the shark? – romainl Mar 13 '13 at 10:00
  • 6
    You got the order wrong it is `vim < tmux < ssh < smartphone over LTE`, and it is **awesome** because you can hop onto your terminal session exactly the way you left it, from anywhere. literally anywhere – Steven Lu Mar 13 '13 at 14:56
  • The smartphone part is the crazy part. – romainl Mar 13 '13 at 16:07
  • 1
    Note that were this an nvim question rather than a vim question, the answers would be different. – JdeBP May 28 '17 at 12:22

2 Answers2

97

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.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 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 Mar 13 '13 at 14:58
  • 3
    It is the answer to the underlying problem. You don't need to do anything in Vim at all. – romainl Mar 13 '13 at 16:08
  • 1
    Quite thorough an answer this has become. Thanks – Steven Lu Mar 16 '13 at 18:19
  • 6
    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. – Armen Michaeli Jun 12 '15 at 19:33
  • 1
    i totally agree with the comment from @amn above :) awesome anser @romainl! really, great explenation. thank you :) – 3.141592 Apr 21 '20 at 11:34
  • 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: https://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". – mkjeldsen Dec 17 '20 at 19:20
11

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.

Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
  • 1
    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 Mar 13 '13 at 14:59
  • 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. – Steven Lu Mar 13 '13 at 15:01
  • @StevenLu, you must use `screen-256color`, not `xterm-256color`. – romainl Mar 13 '13 at 17:09
  • 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. – Jim Stewart Mar 13 '13 at 22:06
  • 3
    `screen-256color` is the [recommended](http://sourceforge.net/p/tmux/tmux-code/ci/master/tree/FAQ) 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*. – romainl Mar 14 '13 at 11:08
  • @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. – Jim Stewart Mar 20 '13 at 22:42