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.