Code Yarns ‍👨‍💻

How to set TERM to xterm-256color

(First posted on: 2015-03-18 13:39:49+00:00)



GNOME Terminal, Terminator and other terminal programs in X that are based on VTE set the TERM environment variable to xterm. Since they support 256 colors, TERM should actually be set to xterm-256color.

Why is this important? Vim and other terminal programs decide how many colors to support based on TERM. So, if Vim sees xterm it reverts to using 8 colors, instead of 256 colors. That is, it sets its t_Co variable to 8 instead of 256.

Wrong solution 1

One common solution to this problem is to add set t_Co=256 to your .vimrc.

This is a crude solution because you are overriding the capability of Vim to automatically detect the terminal type and set colors based on that. For example, if you copy your .vimrc to another computer and run Vim under the virtual terminal (VT) which supports only 8 colors, your .vimrc is now setting t_Co erroneously to 256!

Wrong solution 2

Another common solution is to set TERM=xterm-256color in .bashrc or similar shell initialization script. For example, it can be set based on the value of COLORTERM which is set to gnome-terminal by GNOME Terminal.

However, this is a wrong solution because you are setting a terminal capability in a shell!

Also, think what happens if you now run screen or tmux inside the shell. It will set its own terminal type in TERM and overrides your setting.

Correct solution

We first need to identify that this problem lies in VTE and the GNOME-based terminals that use it. This is a well known problem and distributions are mulling over fixing it. In the meanwhile, we can at least make sure that TERM is set to xterm-256color before a shell is invoked.

For example, in Terminator I do this by right-click -> Preferences -> Profiles -> Command -> Custom command and adding a env TERM=xterm-256color fish to set it before calling the Fish shell. You can add similar custom commands for Bash or in GNOME Terminal.

Reference: TERM Strings

Tried with: Terminator 0.97 and Ubuntu 14.04