XTerm is a classic terminal emulator for the X window system. It is so old that it is in fact a bit older than the X window system itself! Today it ships as one of the quintessential programs of the X window system. Here are some factoids I discovered about XTerm that may interest you to use it:

  • It was explicitly written to emulate the old DEC VT102 and Tektronix 4014 terminals.

  • It supports 256 colors. You may need to configure it to set the TERM environment variable correctly as xterm-256color. Terminal apps read this variable to decide how to show their colors, so this might be essential. See my config file below for how to do this.

  • Along with the classic xterm, there is also xterm with Unicode support that you can invoke as uxterm. The latter is what you almost always need.

  • Being one of those early X programs, it does not have a toolbar or scrollbar. Instead, you get three different menus if you hold down the Ctrl key and click any one of your 3 mouse buttons.

  • The 3 menus you get are: main options (left mouse button), VT options (center button) and font options (right button). All the menu options can also be set in its config file.

  • By default, xterm starts with white background and black foreground. Invoke it with the -rv (reverse video) option to get black background and white foreground. This can also be set in its config file (see below).

  • One keyboard shortcut I like in xterm is Alt + Enter which toggles the fullscreen mode.

  • Like any classic X application, XTerm copies and pastes from the PRIMARY clipboard. If you need the normal window manager clipboard behavior, choose Copy to clipboard in the VT menu.

  • A cool trick to select a large body of text in XTerm is to left-click at the beginning of the text and right-click at the end. The region in between the two clicks is copied to PRIMARY.

  • The main reason I like using xterm, other than its bare bones simplicity, is its keypress latency. Experiments seem to show that it has the lowest keypress latency among all terminal emulators on Linux. This may not be surprising considering the dinosaur age in which it was developed when every bit of speed mattered.

  • XTerm is a great program. Simple and fast. The only disadvantage for me is that its stdout speed is among the slowest in terminal apps. Only Terminator is slower than XTerm in my experience. This is a real deal killer if you frequently run commands that can throw thousands of lines to the terminal.

  • Being a classic X program, XTerm reads its config from a Xresources file. Here are some settings I use in my file to change foreground/background colors, set font size, enable font size increment/decrement and such settings:


Note that the settings are applied to the app only at the start of the X server. If you want to apply them right now, use the xrdb app as described here.



How to set TERM to xterm-256color



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

How to check colors supported by terminal


On the surface it seems like a simple question: how many colors are supported by a terminal you are using? This is important when configuring or debugging color problems in terminal programs like Vim.

Solution 1

The answer should be simple: just query the terminfo database. The easiest way to query the terminfo database to find this out is:

$ tput colors

The problem? The answer reported by this program is wrong! Under a GNOME Terminal which I can see rendering 256 colors, this reports 8 as the answer!

Why is it reporting the wrong answer? It seems to be misled by the TERM environment variable. For reasons which only the Ubuntu/GNOME Gods can answer, the TERM for GNOME Terminal is set to xterm instead of xterm-256color. If you set this manually to xterm-256color, then tput reports 256 correctly. But then what is the point of that?

Solution 2

Left: terminalcolors.py reporting 8 colors for xterm Right: For xterm-256color
Left: terminalcolors.py reporting 8 colors for xterm Right: For xterm-256color

Another solution is to run the terminalcolors.py Python script from here. This beautifully renders the colors in the shell. However, it also falls for the TERM. Only if it is set to xterm-256color can I see 256 colors rendered. Else only the first 8 colors are shown and the rest are in grey.

A similar output and behavior is seen by running msgcat --color=test.

Solution 3

xterm-color-count reporting colors correctly (well almost)
xterm-color-count reporting colors correctly (well almost)

The final solution that worked for me was xterm-color-count. This Bash script actually tries all the XTerm Control Sequences and examines their responses to report the colors. This script was the only one which reported back 256 correctly for me for xterm.

Tried with: GNOME Terminal 3.6.2 and Ubuntu 14.04