25

On a clean install of Ubuntu 12.10, the default gnome-terminal is reporting $TERM to be xterm where it should really be reporting xterm-256color. What is the best way of changing this? I'm avoiding putting this in my .bashrc as that's just asking for trouble.

3
  • 2
    Care to explain why xterm is wrong? Afaik we all have xterm. Besides that: also care to explain why putting this in bashrc is asking for trouble?
    – Rinzwind
    Dec 27, 2012 at 16:19
  • 4
    xterm is wrong because most applications won't work in 256-color mode unless it's xterm-256color or screen-256color (vim and tmux being the main ones I care about). Like @Freddy I would prefer not to set these in my shell profile, because I may be connecting from a different terminal type over SSH, and because it's really the terminal emulator's job to set this. I'm amazed that gnome-terminal doesn't provide multiple emulations. Mar 31, 2013 at 2:48
  • 3
    vte-0.40 (which will probably appear in Ubuntu 15.10) changed the default to xterm-256colors.
    – egmont
    Apr 25, 2015 at 23:39

3 Answers 3

20

You were well advised not to change your startup scripts, specially ~/.bashrc. Any "terminal detection" using current $TERM or $COLORTERM in ~/.profile is merely a guess, and may, as you said, cause trouble when using other terminals (say, Putty or xterm). The terminal emulator is supposed to set $TERM, and this should not be changed from within the shell.

Gnome terminal, AFAIK, does not offer a configuration to change its TERM, but it does allow you to change your startup command, and that's all you need. Here is the trick:

Profile Preferences => Title and Command => Run a custom command instead of my shell

Then use the following command:

env TERM=xterm-256color /bin/bash

Just replace /bin/bash with your preferred shell if it's different. And no, you can't use "$SHELL" in that line for shell auto-detection ;) You have to hard-code it

2
  • There is an option in those settings to "When Command Exits:." The options are "Exit the Terminal," "restart the command," and " hold the terminal open." Which should I select?
    – Caleb Jay
    Apr 9, 2019 at 21:50
  • @CalebJay: whichever you prefer, that option is not related to terminal colors. What to do when the command ends is purely a matter of preference.
    – MestreLion
    Apr 23, 2019 at 17:38
12

For connecting with a terminal that's not able to do 256 colors.

It'd be far better to detect the terminal specifically with $COLORTERM. Look for gnome-terminal, xfce4-terminal, etc, and then set the $TERM variable to xterm-256color.

I do it with:

if [ "$COLORTERM" = "gnome-terminal" ] || [ "$COLORTERM" = "xfce4-terminal" ]
then
    export TERM=xterm-256color
elif [ "$COLORTERM" = "rxvt-xpm" ]
then
    export TERM=rxvt-256color
fi
2
  • 5
    Just be aware that Gnome Terminal from 3.13 onwards do not set COLORTERM anymore.
    – MestreLion
    Jan 29, 2015 at 11:55
  • 3
    For versions that no longer set COLORTERM you can rely on VTE_VERSION.
    – egmont
    Apr 19, 2015 at 15:37
6

While it's true that terminfo has xterm+256color (/usr/share/terminfo/x/xterm+256color), termcap has just xterm (/usr/share/vte/termcap/xterm), so changing $TERM shouldn't be advisable.

Anyhow, $TERM is not set by gnome-terminal, but by vte. The default value for that environment variable can be changed either at compile time (giving an option to the configure script) or by calling the vte_pty_set_term() function. Looking at the sources of gnome-terminal, I see that vte_pty_set_term gets never called, so I can say that there are no ways to modify $TERM by editing gnome-terminal's settings.

What you have to do is to place the following piece of code in your ~/.profile:

if [ "$TERM" = "xterm" ]
then
    export TERM=xterm-256color
fi
5
  • == is a bash-only syntax, do not use it for snippets intended to be sourced by sh (which is the case with ~/.profile in DEs). Also, the "x$.." syntax is not needed as you are correctly quoting the var expansion.
    – MestreLion
    Jan 28, 2015 at 11:03
  • @MestreLion: feel free to edit my question. Jan 28, 2015 at 11:06
  • I tried, but AU requires changes to be more than 6 chars, hence the comment
    – MestreLion
    Jan 28, 2015 at 11:19
  • It does not work,the value set in .profile gets overridden.
    – Hibou57
    May 7, 2020 at 19:29
  • @Hibou57: something is resetting it. Run bash -xi to see what is going on May 8, 2020 at 18:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .