ArchWiki / Color output in console
ArchWiki / Color output in console
hu:Color output in console ja:コンソールのカラー出力 ru:Color output in console zh-hans:Color output in console This page was created to consolidate colorization of CLI outputs.
Background
Escape sequences
The ANSI escape sequences define a way to put additional information into terminal output, and color is part of this "additional information". Throughout the years the range of terminal colors has been vastly expanded, from the initial eight colors to a full 24-bit truecolor.
The basic color encoding provides 8 normal-brightness colors and 8 brighter versions of these colors. Modern terminal emulators, including the Linux console itself, allows you to specify the precise RGB values that the colors translate to. This mode is supported by almost all terminal emulators.
With the advent of 256-color displays came the 256-color escape. The 256 colors are the 16 basic colors, the 216 RGB colors (laid out in a 6x6x6 cube), and 24 levels of greyscale. Except for the first 16 colors, the scheme is usually not customizable as it has a well-defined mapping to RGB. This mode is supported by most terminal emulators. (A minority of emulators use a similar but incompatible encoding with only 88 colors. You are very unlikely to use them in practice, but they will appear in the terminfo database.)
Less commonly supported is the truecolor mode, allowing one to use 16.7 million (224) colors in RGB (each value ranging from 0 to 255).
Termcap and terminfo
Termcap and terminfo, part of ncurses, are databases that provide information on the escape sequences terminals (usually specified by the TERM env-var) understand. The tput(1) and infocmp(1) commands can be used to access them from command-line.
Applications
diff
diffutils from version 3.4 includes the --color option (GNU mailing list).
$ alias diff='diff --color=auto'grep
The --color=auto option enables color highlighting. Color codes are emitted only on standard output; not in pipes or redirection.
Color output in grep is also useful with regexp tasks.
Use an alias to permanently enable this option:
alias grep='grep --color=auto'The GREP_COLORS variable is used to define colors, and it configures various parts of highlighting. To change the colors, find the needed ANSI escape sequence and apply it. See grep(1) for more information.
The -n option includes file line numbers in the output.
less
Environment variables
We can tell less to emit colors when it is meaning to make bold text and other formatting effects using the --RAW-CONTROL-CHARS and --color/-D arguments. We can also add --use-color to enable coloring of the less UI.
As an example, setting the LESS environment variable to --RAW-CONTROL-CHARS --use-color -Dd+r -Du+b will set red for bold and blue for underlined.
For more info, see less(1) or https://felipec.wordpress.com/2021/06/05/adventures-with-man-color/.
Reading from stdin
It is recommended to add colored output through #Environment variables, as the below assumes that LESS is set to include -R
When you run a command and pipe its standard output (stdout) to less for a paged view (e.g. pacman -Qe | less), you may find that the output is no longer colored. This is usually because the program tries to detect if its stdout is an interactive terminal, in which case it prints colored text, and otherwise prints uncolored text. This is good behaviour when you want to redirect stdout to a file, e.g. pacman -Qe > pkglst-backup.txt, but less suited when you want to view output in less.
Some programs provide an option to disable the interactive tty detection:
# dmesg --color=always | lessIn case that the program does not provide any similar option, it is possible to trick the program into thinking its stdout is an interactive terminal with the following utilities:
Example:
stdoutisatty program| lessExample:
unbuffer program| less
Alternatively, using zpty module from zsh: https://blog.lilydjwg.me/2011/6/29/using-zpty-module-of-zsh.27677.html
zmodload zsh/zpty
pty() {
zpty pty-${UID} ${1+$@}
if [[ ! -t 1 ]];then
setopt local_traps
trap '' INT
fi
zpty -r pty-${UID}
zpty -d pty-${UID}
}
ptyless() {
pty $@ | less
}
~/.zshrcUsage:
$ ptyless programTo pipe it to other pager (less in this example):
$ pty program | lessls
The --color=auto option enables color highlighting. Color codes are emitted only on standard output; not in pipes or redirection.
Use an alias to permanently enable this option:
alias ls='ls --color=auto'The LS_COLORS variable is used to define colors, and it configures various parts of highlighting. Use the dircolors(1) command to set it.
An advanced alternative to dircolors that ships with many themes is the vivid package, see vivid --help for usage.
Using the --color option may incur a noticeable performance penalty when ls is run in a directory with very many entries. The default settings require ls to stat(1) every single file it lists. However, if you would like most of the file-type coloring but can live without the other coloring options (e.g. executable, orphan, sticky, other-writable, capability), use dircolors to set the LS_COLORS
See ls(1) for more information.
man
There is a real color facility in grotty(1) (which is the default postprocessor used by man), but it is strongly discouraged for man pages. Most of the below options instead fake a colored manpage by replacing the sequences for bold, standout, and underline with spiced ones that contain color.
Using less
First, either:
Enable coloring in less everywhere, as in #less.
Enable coloring in less just with man, by setting the
MANPAGERenvironment variable to (for example)less --use-color -Dd+r -Du+b. The--RAW-CONTROL-CHARSoption is not required, due to the below option.
You then need to disable SGR/OSC escape sequences in the grotty(1) postprocessor, as they don't work with less. https://felipec.wordpress.com/2021/06/05/adventures-with-man-color/#comment-47932 This is passed through the -P option of nroff(1), which you can apply by setting the MANROFFOPT environment variable to -P -c
Using most
The basic function of 'most' is similar to less and more, but it has a smaller feature set. Configuring most to use colors is easier than using less, but additional configuration is necessary to make most behave like less.
Install the most package.
Edit /etc/man_db.conf, uncomment the pager definition and change it to:
DEFINE pager most -sTest the new setup by typing:
$ man whatever_man_pageModifying the color values requires editing ~/.mostrc (creating the file if it is not present) or editing /etc/most.conf for system-wide changes. Example ~/.mostrc:
% Color settings
color normal lightgray black
color status yellow blue
color underline yellow black
color overstrike brightblue blackUsing bat
bat can be used as a colorizing pager for man, by setting the MANPAGER environment variable
as documented here.
Using X resources
A quick way to add color to manual pages viewed on xterm/uxterm or rxvt-unicode is to modify ~/.Xresources.
xterm
*VT100.colorBDMode: true
*VT100.colorBD: red
*VT100.colorULMode: true
*VT100.colorUL: cyanwhich replaces the decorations with the colors. Also add:
*VT100.veryBoldColors: 6if you want colors and decorations (bold or underline) at the same time. See xterm(1) for more information.
rxvt-unicode
URxvt.colorIT: #87af5f
URxvt.colorBD: #d7d7d7
URxvt.colorUL: #87afd7Run:
$ xrdb -load ~/.XresourcesLaunch a new xterm/uxterm or rxvt-unicode and you should see colorful man pages.
This combination puts colors to bold and underlined words in xterm/uxterm or to bold, underlined, and italicized text in rxvt-unicode. You can play with different combinations of these attributes. See the sources (archived) of this item.
pacman
Pacman has a color option. Uncomment the Color line in /etc/pacman.conf.
Wrappers
Universal wrappers
(most of them outdated, but still functioning)
They go with multiple preconfigured presets that can be changed, and new ones can be created/contributed.
Wrappers replace output of commands with escape sequences. Some shell scripts and programs which use the output of standard shell utilities may not work correctly.
Libraries for colorizing an output
Application specific
Compilers
diff
Diff has built-in color output, which is reasonable to use. But the following wrappers can be used:
cat
less
source-highlight
You can enable code syntax coloring in less. First, install source-highlight, then add these lines to your shell configuration file:
export LESSOPEN="| /usr/bin/source-highlight-esc.sh %s"
export LESS='-R '
~/.bashrclesspipe
Frequent users of the command line interface might want to install lesspipe.
Users may now list the compressed files inside of an archive using their pager:
==> use tar_file:contained_file to view a file in the archive
-rw------- username/group 695 2008-01-04 19:24 compressed_file/content1
-rw------- username/group 43 2007-11-07 11:17 compressed_file/content2
compressed_file.tar.gz (END)$ less compressed_file.tar.gzlesspipe also grants less the ability of interfacing with files other than archives, serving as an alternative for the specific command associated for that file-type (such as viewing HTML via python-html2text).
Re-login after installing lesspipe in order to activate it, or source /etc/profile.d/lesspipe.sh.
Make
Ping
Shells
bash
See Bash/Prompt customization#Colors.
Fish
See Fish#Web interface.
xonsh
zsh
See Zsh#Colors.
Terminal emulators
Virtual console
The colors in the Linux virtual console running on the framebuffer can be changed. This is done by writing the escape code \\e]PXRRGGBB, where X is the hexadecimal index of the color from 0-F, and RRGGBB is a traditional hexadecimal RGB code.
For example, to reuse existing colors defined in ~/.Xresources, add the following to the shell initialization file (such as ~/.bashrc):
if [ "$TERM" = "linux" ]; then
_SEDCMD='s/.*\*color\([0-9]\{1,\}\).*#\([0-9a-fA-F]\{6\}\).*/\1 \2/p'
for i in $(sed -n "$_SEDCMD" $HOME/.Xresources | awk '$1 < 16 {printf "\\e]P%X%s", $1, $2}'); do
echo -en "$i"
done
clear
fiLogin screen
The below is a colored example of the virtual console login screen in /etc/issue. Create a backup of the original file with mv /etc/issue /etc/issue.bak as root, and create a new /etc/issue:
\e[H\e[2J
\e[1;30m| \e[34m\r \s
\e[36;1m/\\\\ \e[37m|| \e[36m| = \e[30m|
\e[36m/ \\\\ \e[37m|| \e[36m| \e[30m| \e[32m\t
\e[1;36m/ \e[0;36m.. \e[1m\\\\ \e[37m//==\\\\\\ ||/= /==\\\\ ||/=\\\\ \e[36m| | |/\\\\ | | \\\\ / \e[30m| \e[32m\d
\e[0;36m/ . . \\\\ \e[37m|| || || || || || \e[36m| | | | | | X \e[1;30m|
\e[0;36m/ . . \\\\ \e[37m\\\\\\==/| || \\\\==/ || || \e[36m| | | |\ \\/| / \\\\ \e[1;30m| \e[31m\U
\e[0;36m/ .. .. \\\\ \e[0;37mA simple, lightweight linux distribution. \e[1;30m|
\e[0;36m/_' `_\\\\ \e[1;30m| \e[35m\l \e[0mon \e[1;33m\n
\e[0mSee also:
https://bbs.archlinux.org/viewtopic.php?pid=386429#p386429
https://www.linuxfromscratch.org/blfs/view/svn/postlfs/logon.html
X window system
Most Xorg terminals, including xterm and urxvt, support at least 16 basic colors. The colors 0-7 are the 'normal' colors. Colors 8-15 are their 'bright' counterparts, used for highlighting. These colors can be modified through X resources, or through specific terminal settings. For example:
! Black + DarkGrey
*color0: #000000
*color8: #555753
! DarkRed + Red
*color1: #ff6565
*color9: #ff8d8d
! DarkGreen + Green
*color2: #93d44f
*color10: #c8e7a8
! DarkYellow + Yellow
*color3: #eab93d
*color11: #ffc123
! DarkBlue + Blue
*color4: #204a87
*color12: #3465a4
! DarkMagenta + Magenta
*color5: #ce5c00
*color13: #f57900
!DarkCyan + Cyan (both not tango)
*color6: #89b6e2
*color14: #46a4ff
! LightGrey + White
*color7: #cccccc
*color15: #ffffff~/.XresourcesColor resources such as foreground and background can be read by other applications (such as emacs). This can be avoided by specifiying the class name, for example XTerm.foreground.
See also:
#Using X resources for how to color bold and underlined text automatically.
Color Themes - Extensive list of terminal color themes by Phraktured.
Xcolors by dkeg (see files with paths matching
theme/dkeg - themein the repository)
Setting foreground and background colors with tput
Example to set bold, yellow text, with blue background:
$ tput bold; tput setaf 3; tput setab 4Example to print 256 colors across the screen:
$ (x=`tput op` y=`printf %76s`;for i in {0..256};do o=00$i;echo -e ${o:${#o}-3:3} `tput setaf $i;tput setab $i`${y// /=}$x;done)Display tput escape codes
Replace tput op with whatever tput you want to trace. op is the default foreground and background color.
033[\033[1;34m"\33[39;49m"\033[00m$ ( strace -s5000 -e write tput op 2>&2 2>&1 ) | tee -a /dev/stderr | grep -o '"[^"]*"'Enumerate supported colors
The following command will let you discover all the terminals you have terminfo support for, and the number of colors each terminal supports. The possible values are: 8, 15, 16, 52, 64, 88 and 256.
Eterm-88color 88
rxvt-88color 88
xterm+88color 88
xterm-88color 88
Eterm-256color 256
gnome-256color 256
konsole-256color 256
putty-256color 256
rxvt-256color 256
screen-256color 256
screen-256color-bce 256
screen-256color-bce-s 256
screen-256color-s 256
xterm+256color 256
xterm-256color 256$ for T in `find /usr/share/terminfo -type f -printf '%f '`;do echo "$T `tput -T $T colors`";done|sort -nk2Color scheme scripts
See https://paste.xinu.at/m-dAiJ/ for scripts which display a chart of your current terminal scheme.
True color support
Some terminals support the full range of 16 million colors (RGB, each with 8 bit resolution): xterm, konsole, st, etc. The corresponding TERM values xterm-direct, konsole-direct, st-direct, etc. are supported starting with ncurses version 6.1 https://lists.gnu.org/archive/html/bug-ncurses/2018-01/msg00045.html. For more info about terminal emulators and applications that support true color, see https://github.com/termstandard/colors/blob/master/README.md.
Note that the Linux kernel supports the SGR (Select Graphic Rendition) escape sequences for true-color, but it is pointless to use it, because the driver maps the 24-bit color specifications to a 256-colors color map in the kernel (see the functions rgb_foreground, rgb_background). For this reason, there is no terminfo entry linux-direct.