On the linux virtual terminal ("console"):
To set the background to color 1 (red):
printf %b '\e[41m' '\e[8]' '\e[H\e[J'
The first escape (setab
) sets the background to color 1, the second escape defines the current foreground-background pair as the default, and the third (clear
) clears the screen.
From now on, 'red' will be default background:
printf %b 'World\e[42mWide\e[mWeb\n'
Notice how setting the background to green with setab 2
(\e[42
) and subsequent clearing of attributes with sgr0
(\e[m
) do not wipe off our setting.
Alternatively, you can redefine what color 0 (the default background, black) means via the "set palette" (aka initc
= "initialize_color") escape:
printf %b '\e[40m' '\e[8]' # set default background to color 0 (black)
printf %b '\e]P0ff0000' # redefine color 0 as 'red'
The latter takes an argument of the form nRRGGBB
, where n
is the color number, and RRGGBB
is a hex triplet defining the color.
This escape is also supported by putty.
For an explanation of these functions and more, check the console_codes(4) and terminfo(5) manpages.
I wasn't able to either:
a) change the background color of the screen without wiping everything from it with clear
.
b) force it to update the non-character margin of the screen without switching back-and-forth between virtual terminals with Control-Alt-Fn.
On xterm-like terminal emulators:
This sets the background to a light green without having to clear the screen:
printf %b '\e]11;#ccffcc\a'
It also works fine on mlterm
, rxvt
, gnome-terminal
, konsole
etc.
The alternative form \e]11;rgb:cc/ff/cc\a
(taken from here) is not supported in konsole
(tested with with 12.16.0 version).
This will not work inside tmux
or screen
.
All of xterm
's escapes are documented in the ctlseqs.txt file from its source code.
Note:
Some of the escapes (eg. \e[8]
or \e]11;...\a
) are not included in terminfo and cannot be used with tput
at all, while others (eg. initc
) are too awkward to be used in a clean way.
A greppable version of the terminfo database could be obtained by a command like: find /usr/share/terminfo -type f -printf '%f\n' | xargs -n1 infocmp -0 -A /usr/share/terminfo
Best Answer
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: