Screwtape's Notepad

Automatic $TERM selection

Everybody wants to use the terminal description that best fits their terminal, but the most flexible and powerful terminal descriptions are also the rarest - which means terminal emulators tend to default to the oldest and most widespread terminal descriptions even if they fall far short of what the emulator can actually do.

Here’s a fragment of .bashrc that I use to automatically set $TERM to the most capable version available when I log in:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
case "$TERM" in
    xterm*)
        TERMLIST=(
            xterm-256color
            xterm-16color
            xterm-color
            xterm
            ) ;;
    screen*)
        TERMLIST=(
            screen-256color-bce
            screen-256color
            screen-16color-bce
            screen-16color
            screen
            ) ;;
    *)
        TERMLIST="$TERM" ;;
esac
for term in $TERMLIST; do
    infocmp "$term" >/dev/null 2>&1 &&
        export TERM=$term &&
        break
done

Basically, it says:

Note that $TERMLIST for xterm* and screen* uses the bash/zsh array syntax. If you’re using a different shell, changing the parentheses to ordinary double-quote characters should work just as well (but doesn’t work in zsh).