Some notes on computer stuff code tags rss about

How to set `xterm-256color` type of terminal in xterm on Slackware Linux?

April 14, 2013
[howto] [issue] [gnu/linux] [slackware] [xorg] [terminal] [xterm] [xdefaults]

Recently support for processing escape sequences in preview window was added to vifm. So I added highlighting of source code in my preview window using highlight tool specifying -O ansi to make it generate ansi compatible escape codes. But some of syntax group obtained the same color, because I had my terminal configured to display up to 8 colors. To solve the issue, I decided to start using xterm-256color terminal mode in xterm. Here's where all fun began.

I know that one can specify type of terminal with command like

export TERM=xterm-256color

So I could just put it to my ~/.profile or ~/.bashrc file. But that will set type of terminal even for native terminal or when I login to maching using ssh. Those issues bother people and some of them solve the issue this way

if [ -n "$DISPLAY" ]; then
    export TERM=xterm-256color
fi

But this is not the way to go, because they write code in such place so that it is executed every time, but does real work only in some cases. The better approach would to execute such code only for xterm. But it doesn't have any configuration file, or does it? Wait, there is ~/.Xdefaults file, when one can set application-specific resources. Let's do the right thing and read documentation at first (man xterm):

-tn name
    This  option specifies the name of the terminal type to be set in the
    TERM environment variable.  It corresponds to the termName resource.
    This terminal type must exist in the terminal database (termcap or
    terminfo, depending on how  xterm  is  built) and should have li# and
    co# entries.  If the terminal type is not found, xterm uses the
    built-in list “xterm”, “vt102”, etc.

...

termName (class TermName)
    Specifies the terminal type name to be set in the TERM environment
    variable.

I don't want to supply an extra -tn parameter to xterm everytime I use it, so let's set termName resource in the ~/.Xdefaults file:

XTerm*termName: xterm-256color

and apply the changes:

xrdb -merge ~/.Xdefaults

Now run new instance of xterm and check:

[~]$ tput colors
8
[~]$ echo $TERM
xterm

But why? It works just fine when set from a running shell. Check whether appropriate terminfo configuration file exists:

[~]$ ls -l /usr/share/terminfo/x/xterm-256color
-rw-r--r-- 1 root root 2.3K Jun 26  2011 /usr/share/terminfo/x/xterm-256color

OK, let's try using -tn parameter:

# in old instance
[~]$ xterm -tn xterm-256color
# in new instance
[~]$ tput colors
8
[~]$ echo $TERM
xterm

What's the ... Try again:

# in old instance
[~]$ xterm -tn xterm-color
# in new instance
[~]$ tput colors
8
[~]$ echo $TERM
xterm-color

So it works in general, it just doesn't work for xterm-256color. OK, let's reread the manual:

EMULATIONS
   ...
   The “TERMCAP”  environment  variable is not set if xterm is linked
   against a terminfo library, since the requisite information is not
   provided by the termcap emulation of terminfo libraries.
   ...

That seems strange, I saw this variable in a shell. From man termcap:

/etc/termcap is an ASCII file (the database master) that lists the
capabilities of many different types of terminals.

Let's look for xterm-color string in /etc/termcap:

[~]$ grep -nA15 xterm-color /etc/termcap
144:vs|xterm|xterm-color|vs100|xterm terminal emulator (X Window System):\
145-        :am:bs:mi@:km:co#80:li#55:\
146-        :im@:ei@:\
147-        :ct=\E[3k:ue=\E[m:\
148-        :is=\E[m\E[?1l\E>:\
149-        :rs=\E[m\E[?1l\E>:\
150-        :eA=\E)0:as=^N:ae=^O:ac=aaffggjjkkllmmnnooqqssttuuvvwwxx:\
151-        :kI=\E[2~:kD=\177:kP=\E[5~:kN=\E[6~:\
152-        :k1=\E[11~:k2=\E[12~:k3=\E[13~:k4=\E[14~:k5=\E[15~:\
153-        :k6=\E[17~:k7=\E[18~:k8=\E[19~:k9=\E[20~:k0=\E[21~:\
154-        :F1=\E[23~:F2=\E[24~:\
155-        :kh=\E[H:kH=\EOw:\
156-        :ks=:ke=:\
157-        :te=\E[2J\E[?47l\E8:ti=\E7\E[?47h:\
158-        :tc=vt-generic:

Try modifying the first line by adding xterm-256color to it.

[~]$ grep -n xterm-color /etc/termcap
144:vs|xterm|xterm-color|xterm-256color|vs100|xterm terminal emulator (X Window System):\

And now:

# in old instance
[~]$ xrdb -merge ~/.Xdefaults
[~]$ xterm
# in new instance
[~]$ tput colors
256
[~]$ echo $TERM
xterm-256color

Finally... I thought it would be way easier and faster than that (it took a couple of hours). The only question that's left is: why xterm is compiled to use termcap instead of terminfo on Slackware?