7

I have a problem with colors in my terminal emulator. I am using LXTerminal as my terminal emulator and LXDE as my desktop environment.

The following command is supposed to print red text on grey background:

printf "\n\033[1;31;40m"hello"\033[00m\n\n"

but that does not work. The grey background is missing (red text is OK). On my older machine (Gnome/Gnome Terminal) everything works fine

I have tried installing several other terminal emulators, but the problem persists. I am not even sure whether this issue is related to terminal emulator, or something else. In Console (CTRL+ALT+F1) the background grey color does not work on either machine)

This problem is limited to grey background only. All other colors work OK, for instance the following prints red text on green background.

printf "\n\033[1;31;42m"hello"\033[00m\n\n"

I would appreciate any help. I don't even know where to start troubleshooting.

  • What does echo $TERM print? – terdon Oct 6 '13 at 16:38
  • @terdon It won't make any difference. – goldilocks Oct 6 '13 at 16:38
  • @goldilocks isn't the $TERM read to determine how escape sequences should be handled? – terdon Oct 6 '13 at 16:40
  • 1
    @terdon: no, $TERM is read by (n)curses/slang/...-using programs to determine which escape sequences to send. – ninjalj Oct 6 '13 at 21:08
  • 1
    @goldilocks: konsole can do it via TERM=konsole-256color tput setab 8; echo foobar; tput setab 0 – ninjalj Oct 6 '13 at 21:16
10

There are eight standard ANSI colors, supported by every terminal emulator. Most terminal emulators also have eight bright variants of the standard ANSI colors.

However, the actual color values that the escape codes map to aren't standardized, and in fact they often slightly vary among terminal emulators. So if you do printf "\e[31;47mTest\n" to print red text on a white background, the actual hues of red and white you get may be different depending on the terminal emulator you use.

So that partly explains the problem: color values aren't standard, and LXTerminal may have different defaults for its color palette that you're not used to. If you look around in the settings, usually you can configure the color scheme to be whatever you like.

The other problem you face is that what the bold attribute actually does isn't standardized either. There are three possibilities: it can make the font bold, it can make the foreground color brighter, or it can both make the foreground color brighter and make the font bold.

Again, the default behavior here varies among terminal emulators, and you can usually change it if can you find the right setting. Grep for something mentioning 'bold' or 'bright'.

If you want to use a bright color, then you can use the so-called aixterm color escape codes instead of bold. These aren't standard, but they're supported in every modern terminal emulator I know of. Unlike bold, they always use bright colors, plus they can be used to display bright background colors.

So for example, if you wanted to print bright red text on a bright white background, you would do this: printf "\e[91;107mTest\n".

For reference, here's a table of all the color escape codes:

|    ANSI |    ANSI |    ANSI    |                | Aixterm | Aixterm 
|   Color | FG Code | BG Code    | Bright Color   | FG Code | BG Code 
+---------+---------+--------    +----------------+---------+-------- 
|   Black |      30 |      40    |   Bright Black |      90 |     100 
|     Red |      31 |      41    |     Bright Red |      91 |     101 
|   Green |      32 |      42    |   Bright Green |      92 |     102 
|  Yellow |      33 |      43    |  Bright Yellow |      93 |     103 
|    Blue |      34 |      44    |    Bright Blue |      94 |     104 
| Magenta |      35 |      45    | Bright Magenta |      95 |     105 
|    Cyan |      36 |      46    |    Bright Cyan |      96 |     106 
|   White |      37 |      47    |   Bright White |      97 |     107 
  • actually, the problem is not limited to these "escape codes". I have the same problem with joe editor. In gnome-terminal, I can see grey background. In LXTerminal it is missing. – Martin Vegter Oct 7 '13 at 10:35
  • And you are right about the actual colors varying between different terminal emulators. When I open midnight commander in LXTerminal and gnome-terminal, the blue color looks very different. But back to my original problem: the grey background in LXTerminal is not just different color than in gnome-terminal, but it is missing entirely. – Martin Vegter Oct 7 '13 at 10:49
  • 1
    What do you mean that the grey background is missing? It's probably there, but not visible because it's the same color as your background. Technically there is no grey color in ANSI, there is only black or white. So maybe you have your background color set to black, and ANSI black is mapped to black in LXTerminal but mapped to grey in gnome-terminal. I don't use LXTerminal, but I bet if you look in the settings and change any 'black' colors to 'grey' it will become visible. – Matt Oct 7 '13 at 18:25
  • You are absolutely right. When I change the background color of my LXTerminal to something other than black, than I can see that printf "\n\033[1;31;40m"hello"\033[00m\n\n" prints text on black backround. Thus, what gnome-terminal interprets as grey, LXTerminal interprets as black. Is there any way to display grey in LXTerminal, same as in gnome-terminal ? – Martin Vegter Oct 8 '13 at 7:52
  • 1
    Actually aixterm is SGR 90-97 and 100-107. Bright backgrounds for bold are from other systems (not original to Linux, but used there). – Thomas Dickey Jul 29 '16 at 1:06
5

The 1 attribute at the beginning of 1;31;40 turns on "bold" (aka. "bright") mode, but some terminals only apply this to the foreground color. I have no idea for what reason (and changing the $TERM variable makes no difference), since they do support 16 colors in the foreground.1 0 (from 40: 4 = background, 0 = the color) is actually black, but "bright black" is gray. Here's two solutions:

  • Color 7 (white) will probably turn out to be gray, not white (for true white, you need "bright white", meaning the background you actually can't have is white).

  • You should be able use gnome terminal in LXDE if you want. Just install it and start with gnome-terminal (I believe that's its command name). Then you'll have to configure whatever launchers to use that instead of the lx-terminal.

1 Here's my guess: Notice that setting bold applies brightness to the foreground color and adds a bold emphasis. Hence, you can't use a bright foreground color without also applying bold. Presumably back when monochrome terminals were common, just plain setting bold for emphasis, eg: \\033[1mBlah blah\\033[0m was common -- and in fact this will still work, but it will also make your foreground color bright. If we considered it undesirable for this to also brighten the background (because it is just supposed to be bold), then we'd make sure the implementation didn't do that. Oh well.

  • I have installed gnome-terminal and the colors are displayed correctly. Does it mean this problem is specific to LXTerminal ? Is there any way to fix this ? I would prefer to use LXTerminal, if this problem could be solved. – Martin Vegter Oct 6 '13 at 19:09
  • 2
    The problem isn't specific to LXTerminal; in fact gnome-terminal maybe the only terminal to do what you want -- I've been a linux user for ~15 years and learnt about ANSI sequences early on because of the prompt; it's always been like this. I might be able to figure out decisively why by researching the official ANSI specs and/or wading through source, but that would require a very indeterminate period of time ;) I haven't found anything hopeful after browsing the web, so yours is a good question but someone who already knows could well come along in the next day or so... – goldilocks Oct 6 '13 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.