Color output in console: Difference between revisions

From ArchWiki
m (Add zh-cn link.)
(update status of external links)
 
(150 intermediate revisions by 56 users not shown)
Line 1: Line 1:
[[Category:Linux console]]
[[Category:Eye candy]]
[[Category:Eye candy]]
[[Category:Command shells]]
[[ja:コンソールのカラー出力]]
[[zh-cn:Color output in console]]
[[ru:Color output in console]]
[[zh-hans:Color output in console]]
{{Expansion|And maybe create something bigger. Please take active approach if you can add some valuable information. Mention {{Pkg|python-pywal}}.|Talk:Color output in console#Why and what this page is about}}
{{Style|Page is partly a collection of information on different applications, partly begin to lay theory to color output process. Please contribute your knowledge, if you can.}}
{{Related articles start}}
{{Related articles start}}
{{Related|Emacs#Custom colors and theme}}
{{Related|Emacs#Custom colors and theme}}
{{Related|nano#Syntax highlighting}}
{{Related|nano#Syntax highlighting}}
{{Related articles end}}
{{Related articles end}}
{{Expansion|And maybe create something bigger. Please take active approach if you can add some valuable information.|Talk:Color output in console#Why and what this page is about}}
This page was created to consolidate colorization of CLI outputs.
This page was created to consolidate colorization of CLI outputs.


== diff ==
== Background ==


diffutils from version 3.4 includes the {{ic|--color}} option ([https://lists.gnu.org/archive/html/info-gnu/2016-08/msg00004.html GNU mailing list]).
{{Expansion|Probably a good idea to move the list-color scripts here now.}}
 
=== Escape sequences ===
 
The [[Wikipedia:ANSI escape sequence|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 practise, but they will appear in the terminfo database.)


$ alias diff 'diff --color=auto'
Less commonly supported is the truecolor mode, allowing one to use 16.7 million (2<sup>24</sup>) colors in RGB (each value ranging from 0 to 255).


Alternatively, the following wrappers can be used:
=== Termcap and terminfo ===


* {{App|colordiff|A Perl script wrapper for 'diff' that produces the same output but with pretty 'syntax' highlighting|http://www.colordiff.org/|{{Pkg|colordiff}}}}
Termcap and terminfo, part of {{Pkg|ncurses}}, are databases that provide information on the escape sequences terminals (usually specified by the {{ic|TERM}} env-var) understand. The {{man|1|tput}} and {{man|1|infocmp}} commands can be used to access them from command-line.
* {{App|cwdiff|A (w)diff wrapper to support directories and colorize the output|https://github.com/junghans/cwdiff|{{AUR|cwdiff}}, {{AUR|cwdiff-git}}}}


== grep ==
== Applications ==


{{ic|grep}}'s color output can be helpful for learning [[Wikipedia:regexp|regexp]] and additional {{ic|grep}} functionality.
=== diff ===


To enable ''grep'' coloring write the following entry to the shell configuration file (e.g. if using [[Bash]]):
diffutils from version 3.4 includes the {{ic|--color}} option ([https://lists.gnu.org/archive/html/info-gnu/2016-08/msg00004.html GNU mailing list]).


{{hc|~/.bashrc|2=alias grep='grep --color=auto'}}
$ alias diff='diff --color=auto'


To include file line numbers in the output, add the option {{ic|-n}} to the line.
=== grep ===


The environment variable {{ic|GREP_COLOR}} can be used to define the default highlight color (the default is red). To change the color find the [http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html ANSI escape sequence] for the color liked and add it:
The {{ic|1=--color=auto}} option enables color highlighting. Color codes are emitted only on standard output; not in pipes or redirection.


export GREP_COLOR="1;32"
Color output in ''grep'' is also useful with [[Wikipedia:regexp|regexp]] tasks.


{{ic|GREP_COLORS}} may be used to define specific searches.
Use an [[alias]] to permanently enable this option:


== less ==
alias grep='grep --color=auto'


=== Environment variables ===
The {{ic|GREP_COLORS}} variable is used to define colors, and it configures various parts of highlighting. To change the colors, find the needed [https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html ANSI escape sequence] and apply it. See {{man|1|grep|GREP COLORS}} for more information.


Add the following lines to your shell configuration file:
The {{ic|-n}} option includes file line numbers in the output.


{{hc|~/.bashrc|2=
=== ip ===
export LESS=-R
export LESS_TERMCAP_mb=$'\E[1;31m'
export LESS_TERMCAP_md=$'\E[1;36m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[1;32m'
}}


Change the values ([[Wikipedia:ANSI escape code#Colors|ANSI escape code]]) as you like.
{{man|8|ip}} command from {{Pkg|iproute2}} supports colors with {{ic|-c}} option. You can use an [[alias]] to enable colored output. When using {{ic|auto}} parameter, colored output will be enabled only when stdout is a terminal.


{{note|The {{ic|LESS_TERMCAL_''xx''}} variables is currently undocumented in less(1), for a detailed explanation on these sequences, see this [http://unix.stackexchange.com/questions/108699/documentation-on-less-termcap-variables/108840#108840 answer].}}
alias ip='ip -color=auto'


=== Wrappers ===
=== less ===


You can enable code syntax coloring in ''less''. First, [[install]] {{Pkg|source-highlight}}, then add these lines to your shell configuration file:
==== Environment variables ====
{{hc|~/.bashrc|<nowiki>
export LESSOPEN="| /usr/bin/source-highlight-esc.sh %s"
export LESS='-R '
</nowiki>}}


Frequent users of the command line interface might want to install {{Pkg|lesspipe}}.
As with the [[#man]] case, we can tell less to emit colors when it is meaning to make bold text and other formatting effects.


Users may now list the compressed files inside of an archive using their pager:
Add the following lines to your shell configuration file:


{{hc|$ less ''compressed_file''.tar.gz|2=
export LESS='-R --use-color -Dd+r$Du+b$'
==> 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)
}}


''lesspipe'' 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 {{Pkg|python-html2text}}).
It will set red for bold and blue for underlined.


Re-login after installing ''lesspipe'' in order to activate it, or source {{ic|/etc/profile.d/lesspipe.sh}}.
For more information about the {{ic|--use-color}} and {{ic|-D}} options, see {{man|1|less|D}} or [https://felipec.wordpress.com/2021/06/05/adventures-with-man-color/].


=== Reading from stdin ===
==== Reading from stdin ====


{{Note|It is recommended to add colored output through [[#Environment variables]] to your {{ic|~/.bashrc}} or {{ic|~/.zshrc}}, as the below is based on {{ic|1=export LESS=R}}}}
{{Note|It is recommended to add colored output through [[#Environment variables]] to your {{ic|~/.bashrc}} or {{ic|~/.zshrc}}, as the below is based on {{ic|1=export LESS='-R'}}}}


When you run a command and pipe its [[Wikipedia:Standard output|standard output]] (''stdout'') to ''less'' for a paged view (e.g. {{ic|<nowiki>pacman -Qe | less</nowiki>}}), 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. {{ic|<nowiki>pacman -Qe > pkglst-backup.txt</nowiki>}}, but less suited when you want to view output in {{ic|less}}.
When you run a command and pipe its [[Wikipedia:Standard output|standard output]] (''stdout'') to ''less'' for a paged view (e.g. {{ic|<nowiki>pacman -Qe | less</nowiki>}}), 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. {{ic|<nowiki>pacman -Qe > pkglst-backup.txt</nowiki>}}, but less suited when you want to view output in {{ic|less}}.
Line 92: Line 84:
In 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:
In 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:


* {{App|stdoutisatty|A small program which catches the {{ic|isatty}} function call.|https://github.com/lilydjwg/stdoutisatty.|{{AUR|stdoutisatty-git}}}}
* {{App|ColorThis|Force colored output of a program by running it within a (group of) pty, support forwarding stdin.|https://github.com/Sasasu/ColorThis|{{AUR|colorthis-git}}}}
* {{App|stdoutisatty|A small program and a {{ic|LD_PRELOAD}}-able library that catches the {{man|3|isatty}} function call.|https://github.com/lilydjwg/stdoutisatty.|{{pkg|stdoutisatty}}}}
:Example: {{ic|stdoutisatty ''program'' <nowiki>| less</nowiki>}}
:Example: {{ic|stdoutisatty ''program'' <nowiki>| less</nowiki>}}
* {{App|unbuffer|A tclsh script comes with expect, it invokes desired program within a pty.|http://expect.sourceforge.net/example/unbuffer.man.html|{{Pkg|expect}}}}
* {{App|unbuffer|A tclsh script comes with expect, it invokes desired program within a pty.|http://expect.sourceforge.net/example/unbuffer.man.html|{{Pkg|expect}}}}
:Example: {{ic|unbuffer ''program'' <nowiki>| less</nowiki>}}
:Example: {{ic|unbuffer ''program'' <nowiki>| less</nowiki>}}


Alternatively, using [http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002fzpty-Module zpty] module from [[zsh]]: [http://lilydjwg.is-programmer.com/2011/6/29/using-zpty-module-of-zsh.27677.html]
Alternatively, using [http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002fzpty-Module zpty] module from [[zsh]]: [https://blog.lilydjwg.me/2011/6/29/using-zpty-module-of-zsh.27677.html]


{{hc|~/.zshrc|
{{hc|~/.zshrc|<nowiki>
<nowiki>zmodload zsh/zpty
zmodload zsh/zpty


pty() {
pty() {
Line 114: Line 107:
ptyless() {
ptyless() {
pty $@ | less
pty $@ | less
}</nowiki>}}
}
</nowiki>}}


Usage:
Usage:
Line 124: Line 118:
  $ pty ''program'' | less
  $ pty ''program'' | less


== ls ==
=== ls ===
 
The {{ic|1=--color=auto}} option enables color highlighting. Color codes are emitted only on standard output; not in pipes or redirection.


Colored output can be enabled with a simple alias. File {{ic|~/.bashrc}} should already have the following entry copied from {{ic|/etc/skel/.bashrc}}:
Use an [[alias]] to permanently enable this option:


  alias ls='ls --color=auto'
  alias ls='ls --color=auto'


The next step will further enhance the colored ''ls'' output; for example, broken (orphan) symlinks will start showing in a red hue. Add the following to your shell configuration file:
The {{ic|LS_COLORS}} variable is used to define colors, and it configures various parts of highlighting. Use the {{man|1|dircolors}} command to set it.
 
An advanced alternative to ''dircolors'' that ships with many themes is the {{Pkg|vivid}} package, see {{ic|vivid --help}} for usage.
 
{{Note|Using the {{ic|--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 {{man|1|stat}} 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 {{ic|LS_COLORS}} environment variable like this:
 
<nowiki>eval $(dircolors -p | perl -pe 's/^((CAP|S[ET]|O[TR]|M|E)\w+).*/$1 00/' | dircolors -)</nowiki>
 
}}
 
See {{man|1|ls}} for more information.
 
=== man ===


eval $(dircolors -b)
{{Style|The ''grotty'' utility is mentioned only in the intro - where is it actually used? Also, there are more subsections than just {{Ic|less}} and {{Ic|most}}.}}


== man ==
There is a real color facility in {{man|1|grotty}}, but it is strongly discouraged for man pages. Here we fake a colored {{Ic|man}} by hacking two main pagers, {{Ic|less}} and {{Ic|most}}: we replace the sequences for bold, standout, and underline with spiced ones that contain color.


Color-enabled man pages allow for a clearer presentation and easier digestion of the content.
==== Using bat ====
There are two prevalent methods for achieving colored man pages: using {{Ic|less}}, or opting for {{Ic|most}}.


=== Using less ===
{{Pkg|bat}} can be used as a colorizing pager for man, by setting the {{ic|MANPAGER}} environment variable
[https://github.com/sharkdp/bat#man as documented here].


You can set various {{ic|LESS_TERMCAP_''*''}} environment variables to change how it highlights text. For example, {{ic|LESS_TERMCAP_md}} is used for bold text and {{ic|LESS_TERMCAP_me}} is used to reset to normal text formatting[http://boredzo.org/blog/archives/2016-08-15/colorized-man-pages-understood-and-customized].
==== Using less ====


In Bash, the escape sequences you can use are the same ones from [[Bash/Prompt customization]] and these variables can be defined in a function wrapping the {{ic|man}} command:
See [[#less]] for a more detailed description.


{{hc|~/.bashrc|
export MANPAGER="less -R --use-color -Dd+r -Du+b"
<nowiki>man() {
export MANROFFOPT="-P -c"
    LESS_TERMCAP_md=$'\e[01;31m' \
    LESS_TERMCAP_me=$'\e[0m' \
    LESS_TERMCAP_se=$'\e[0m' \
    LESS_TERMCAP_so=$'\e[01;44;33m' \
    LESS_TERMCAP_ue=$'\e[0m' \
    LESS_TERMCAP_us=$'\e[01;32m' \
    command man "$@"
}
</nowiki>}}


For [[Fish]] you could accomplish this with:
For [[Fish]] you could accomplish this with:


{{hc|~/.config/fish/config.fish|<nowiki>
{{hc|~/.config/fish/config.fish|2=
set -xU LESS_TERMCAP_md (printf "\e[01;31m")
set -xU MANPAGER 'less -R --use-color -Dd+r -Du+b'
set -xU LESS_TERMCAP_me (printf "\e[0m")
set -xU MANROFFOPT '-P -c'
set -xU LESS_TERMCAP_se (printf "\e[0m")
}}
set -xU LESS_TERMCAP_so (printf "\e[01;44;33m")
set -xU LESS_TERMCAP_ue (printf "\e[0m")
set -xU LESS_TERMCAP_us (printf "\e[01;32m")
</nowiki>}}


Remember to source your config or restart your shell to make the changes take effect.
Remember to source your config or restart your shell to make the changes take effect.


For a detailed explanation on these variables, see [http://unix.stackexchange.com/questions/108699/documentation-on-less-termcap-variables/108840#108840 this answer]. [[Bash/Prompt customization#Examples]] has some (non-Bash-specific) examples of escape sequences that can be used.
==== Using most ====
 
=== Using most ===


The basic function of 'most' is similar to {{Ic|less}} and {{Ic|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.
The basic function of 'most' is similar to {{Ic|less}} and {{Ic|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.
Line 192: Line 185:
  color underline yellow black
  color underline yellow black
  color overstrike brightblue black
  color overstrike brightblue black
{{Remove|How is the following relevant to ''Color output in console''?}}
A list of all keybindings may be found at {{ic|/usr/share/doc/most/most-fun.txt}}. To get a basic {{ic|less}}/{{ic|vim}}-like configuration, you can copy {{ic|/usr/share/doc/most/lesskeys.rc}} to {{ic|~/.mostrc}}. The lesskeys rc included with most does not include 'g' or 'G', so you will also have to add these lines to {{ic|~/.mostrc}}:
setkey bob "g"
setkey eob "G"
setkey page_down "d"
setkey page_up "u"
You may also want to set the {{ic|goto_line}} keybinding in the rc if you do not like the default of 'J'.


Another example showing keybindings similar to {{Ic|less}} (jump to line is set to 'J'):
Another example showing keybindings similar to {{Ic|less}} (jump to line is set to 'J'):
Line 232: Line 236:
  setkey edit "v"
  setkey edit "v"


=== Using X resources ===
==== Using X resources ====


A quick way to add color to manual pages viewed on {{Pkg|xterm}}/{{Ic|uxterm}} or {{Pkg|rxvt-unicode}} is to modify {{ic|~/.Xresources}}.
A quick way to add color to manual pages viewed on {{Pkg|xterm}}/{{Ic|uxterm}} or {{Pkg|rxvt-unicode}} is to modify {{ic|~/.Xresources}}.


==== xterm ====
===== xterm =====


  *VT100.colorBDMode:    true
  *VT100.colorBDMode:    true
Line 247: Line 251:
  *VT100.veryBoldColors: 6
  *VT100.veryBoldColors: 6


if you want colors and decorations (bold or underline) ''at the same time''.  See {{ic|man xterm}} for a description of the {{ic|veryBoldColors}} resource.
if you want colors and decorations (bold or underline) ''at the same time''.  See {{man|1|xterm|veryBoldColors}} for more information.


==== rxvt-unicode ====
===== rxvt-unicode =====


  URxvt.colorIT:      #87af5f
  URxvt.colorIT:      #87af5f
Line 261: Line 265:
Launch a new {{Ic|xterm/uxterm}} or {{Ic|rxvt-unicode}} and you should see colorful man pages.
Launch a new {{Ic|xterm/uxterm}} or {{Ic|rxvt-unicode}} and you should see colorful man pages.


This combination puts colors to '''bold''' and <u>underlined</u> words in {{Ic|xterm/uxterm}} or to '''bold''', <u>underlined</u>, and ''italicized'' text in {{Ic|rxvt-unicode}}. You can play with different combinations of these attributes (see the [http://pub.ligatura.org/fs/xfree86/xresources/xterm sources]{{Dead link|2013|09|10}} of this item).
This combination puts colors to '''bold''' and <u>underlined</u> words in {{Ic|xterm/uxterm}} or to '''bold''', <u>underlined</u>, and ''italicized'' text in {{Ic|rxvt-unicode}}. You can play with different combinations of these attributes. See the [https://web.archive.org/web/20210512042458/http://vger.cz/setup/XFree86/app-defaults/XTerm sources] (archived) of this item.


== pacman ==
=== pacman ===


[[Pacman]] has a color option. Uncomment the {{ic|Color}} line in {{ic|/etc/pacman.conf}}.
[[Pacman]] has a color option. Uncomment the {{ic|Color}} line in {{ic|/etc/pacman.conf}}.


== Wrappers for other programs ==
== Wrappers ==


{{move||Some of these could be made into sections of their own, or moved to existing sections such as [[#diff]]}}
{{move||Some of these could be made into sections of their own, or moved to existing sections such as [[#diff]]}}


(most of them outdated but still functioning)
=== 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.


=== Universal wrappers with multiple preconfigured presets ===
{{Warning|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.}}


* {{App|rainbow|Colorize commands output or STDIN using patterns.<br>Presets: df, diff, env, host, ifconfig, java-stack-trace, jboss, jonas, md5sum, mvn2, mvn3, ping, tomcat, top, traceroute.|https://github.com/nicoulaj/rainbow}}
* {{App|rainbow|Colorize commands output or STDIN using patterns.<br>Presets: df, diff, env, host, ifconfig, java-stack-trace, jboss, jonas, md5sum, mvn2, mvn3, ping, tomcat, top, traceroute.|https://github.com/nicoulaj/rainbow|{{AUR|rainbow}}}}
* {{App|grc|Yet another colouriser for beautifying your logfiles or output of commands.<br>Presets: configure, cvs, df, diff, esperanto, gcc, irclog, ldap, log, mount, netstat, ping, proftpd, traceroute, wdiff.|https://github.com/pengwynn/grc|{{Pkg|grc}}}}
* {{App|grc|Yet another colouriser for beautifying your logfiles or output of commands.<br>Presets: cat, cvs, df, diff, dig, gcc, g++, ls, ifconfig, make, mount, mtr, netstat, ping, ps, tail, traceroute, wdiff, blkid, du, dnf, docker, docker-machine, env, id, ip, iostat, last, lsattr, lsblk, lspci, lsmod, lsof, getfacl, getsebool, ulimit, uptime, nmap, fdisk, findmnt, free, semanage, sar, ss, sysctl, systemctl, stat, showmount, tune2fs and tcpdump.|https://github.com/garabik/grc|{{Pkg|grc}}}}
* {{App|colorlogs|Colorize commands output or STDIN using patterns.<br>Presets: logs, git status, ant, maven.|https://github.com/memorius/colorlogs}}
* {{App|cope|A colourful wrapper for terminal programs.<br>Presets: acpi, arp, cc, df, dprofpp, fdisk, free, g++, gcc, id, ifconfig, ls, lspci, lsusb, make, md5sum, mpc, netstat, nm, nmap, nocope, ping, pmap, ps, readelf, route, screen, sha1sum, sha224sum, sha256sum, sha384sum, sha512sum, shasum, socklist, stat, strace, tcpdump, tracepath, traceroute, w, wget, who, xrandr.|https://github.com/yogan/cope|{{AUR|cope-git}}}}
* {{App|cope|A colourful wrapper for terminal programs.<br>Presets: acpi, arp, cc, df, dprofpp, fdisk, free, g++, gcc, id, ifconfig, ls, lspci, lsusb, make, md5sum, mpc, netstat, nm, nmap, nocope, ping, pmap, ps, readelf, route, screen, sha1sum, sha224sum, sha256sum, sha384sum, sha512sum, shasum, socklist, stat, strace, tcpdump, tracepath, traceroute, w, wget, who, xrandr.|https://github.com/yogan/cope|{{AUR|cope-git}}}}
* {{App|cw|A non-intrusive real-time ANSI color wrapper for common unix-based commands.<br>Presets: arp, arping, auth.log@, blockdev, cal, cksum, clock, configure, cpuinfo@, crontab@, cw-pipe, cw-test.cgi, date, df, diff, dig, dmesg, du, env, figlet, file, find, finger, free, fstab@, fuser, g++, gcc, group@, groups, hdparm, hexdump, host, hosts@, id, ifconfig, inittab@, iptables, last, lastlog, lsattr, lsmod, lsof, ltrace-color, make, md5sum, meminfo@, messages@, mount, mpg123, netstat, nfsstat, nmap, nslookup, objdump, passwd@, ping, pmap, pmap_dump, praliases, profile@, protocols@, ps, pstree, quota, quotastats, resolv.conf@, route, routel, sdiff, services@, showmount, smbstatus, stat, strace-color, sysctl, syslog, tar, tcpdump, tracepath, traceroute, umount, uname, uptime, users, vmstat, w, wc, whereis, who, xferlog.|http://cwrapper.sourceforge.net/|{{AUR|cw}}}}
* {{App|cw|A non-intrusive real-time ANSI color wrapper for common unix-based commands. Wraps {{Pkg|file}} which can cause issues.<br>Presets: arp, arping, auth.log@, blockdev, cal, cksum, clock, configure, cpuinfo@, crontab@, cw-pipe, cw-test.cgi, date, df, diff, dig, dmesg, du, env, figlet, file, find, finger, free, fstab@, fuser, g++, gcc, group@, groups, hdparm, hexdump, host, hosts@, id, ifconfig, inittab@, iptables, last, lastlog, lsattr, lsmod, lsof, ltrace-color, make, md5sum, meminfo@, messages@, mount, mpg123, netstat, nfsstat, nmap, nslookup, objdump, passwd@, ping, pmap, pmap_dump, praliases, profile@, protocols@, ps, pstree, quota, quotastats, resolv.conf@, route, routel, sdiff, services@, showmount, smbstatus, stat, strace-color, sysctl, syslog, tar, tcpdump, tracepath, traceroute, umount, uname, uptime, users, vmstat, w, wc, whereis, who, xferlog.|http://cwrapper.sourceforge.net/|{{AUR|cw}}}}
* {{App|ccze|A fast log colorizer written in C, intended to be a drop-in replacement for colorize|https://github.com/cornet/ccze/|{{Pkg|ccze}}}}
 
=== Libraries for colorizing an output ===
 
* {{App|libtextstyle|A C library for styling text output to terminals|https://www.gnu.org/software/gettext/libtextstyle/manual/index.html|{{Pkg|gettext}}}}
* {{App|ruby-rainbow|Rainbow is extension to ruby's String class adding support for colorizing text on ANSI terminal|https://rubygems.org/gems/rainbow/|{{Pkg|ruby-rainbow}}}}
* {{App|python-blessings|A thin, practical wrapper around terminal coloring, styling, and positioning|https://github.com/erikrose/blessings|{{AUR|python-blessings}}}}
* {{App|lolcat|Ruby program that makes the output colorful like a rainbow|https://github.com/busyloop/lolcat/|{{Pkg|lolcat}}}}
 
=== Application specific ===
 
==== Compilers ====
 
* {{App|colorgcc|A Perl wrapper to colorize the output of compilers with warning/error messages matching the gcc output format|https://schlueters.de/colorgcc.html{{Dead link|2024|03|03|status=404}}|{{Pkg|colorgcc}}}}
 
==== diff ====
 
Diff has [[#diff|built-in color output]], which is reasonable to use. But the following wrappers can be used:
 
* {{App|colordiff|Perl script for ''diff'' highlighting.|https://www.colordiff.org/|{{Pkg|colordiff}}}}
* {{App|cwdiff|''(w)diff'' wrapper with directories support and highlighting.|https://github.com/junghans/cwdiff|{{AUR|cwdiff}}}}
* {{App|git-delta|A syntax-highlighting pager for git and diff output.|https://github.com/dandavison/delta|{{Pkg|git-delta}}}}
 
==== cat ====
 
* {{App|bat|Cat clone with syntax highlighting and git integration.|https://github.com/sharkdp/bat|{{Pkg|bat}}}}
 
==== less ====
 
===== source-highlight =====
 
You can enable code syntax coloring in ''less''. First, [[install]] {{Pkg|source-highlight}}, then add these lines to your shell configuration file:
 
{{hc|~/.bashrc|<nowiki>
export LESSOPEN="| /usr/bin/source-highlight-esc.sh %s"
export LESS='-R '
</nowiki>}}
 
===== lesspipe =====
 
Frequent users of the command line interface might want to install {{Pkg|lesspipe}}.


=== Compilers ===
Users may now list the compressed files inside of an archive using their pager:


* {{App|colorgcc|A Perl wrapper to colorize the output of compilers with warning/error messages matching the gcc output format|https://schlueters.de/colorgcc.html|{{Pkg|colorgcc}}}}
{{hc|$ less ''compressed_file''.tar.gz|2=
==> 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)
}}


=== Ping ===
''lesspipe'' 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 {{Pkg|python-html2text}}).


* {{App|prettyping|Add some great features to ping monitoring. A wrapper around the standard ping tool with the objective of making the output prettier, more colorful, more compact, and easier to read.|http://denilson.sa.nom.br/prettyping/|{{AUR|prettyping}}}}
Re-login after installing ''lesspipe'' in order to activate it, or source {{ic|/etc/profile.d/lesspipe.sh}}.


=== Make ===
==== Make ====


* {{App|colormake|A simple wrapper around make to make it's output more readable.|http://bre.klaki.net/programs/colormake/|{{AUR|colormake}}, {{AUR|colormake-git}}}}
* {{App|colormake|A simple wrapper around make to make its output more readable.|https://github.com/pagekite/Colormake/|{{AUR|colormake}}}}


=== Libraries ===
==== Ping ====


* {{App|ruby-rainbow|Rainbow is extension to ruby's String class adding support for colorizing text on ANSI terminal|https://rubygems.org/gems/rainbow/|{{Pkg|ruby-rainbow}}}}
* {{App|prettyping|Add some great features to ping monitoring. A wrapper around the standard ping tool with the objective of making the output prettier, more colorful, more compact, and easier to read.|https://denilson.sa.nom.br/prettyping/|{{Pkg|prettyping}}}}
* {{App|python-blessings|A thin, practical wrapper around terminal coloring, styling, and positioning|https://pypi.python.org/pypi/blessings|{{Pkg|python-blessings}}, {{Pkg|python2-blessings}}}}


== Shells ==
== Shells ==
Line 303: Line 355:


See [[Bash/Prompt customization#Colors]].
See [[Bash/Prompt customization#Colors]].
=== Fish ===
See [[Fish#Web interface]].
=== xonsh ===
See [https://xon.sh/tutorial.html#customizing-the-prompt Customizing the Prompt].


=== zsh ===
=== zsh ===


See [[Zsh#Colors]].
See [[Zsh#Colors]].
=== Fish ===
See [[Fish#Web interface]].


== Terminal emulators ==
== Terminal emulators ==
Line 318: Line 374:
{{Style|Lacks clarity on what "the colors" are, i.e. in [[#Virtual console]] they are about the ''representations'' of the 16 base colors (RGB values for yellow, red, blue, etc.), while in [[#Login screen]] they are about the base colors themselves. See also {{man|4|console_codes}} and [[User:Isacdaavid/Linux_Console]]}}
{{Style|Lacks clarity on what "the colors" are, i.e. in [[#Virtual console]] they are about the ''representations'' of the 16 base colors (RGB values for yellow, red, blue, etc.), while in [[#Login screen]] they are about the base colors themselves. See also {{man|4|console_codes}} and [[User:Isacdaavid/Linux_Console]]}}


The colors in the [[w:Virtual console|Linux virtual console]]—see {{man|4|console}}—running on the framebuffer can be changed. This is done by writing the escape code {{ic|\\e]PXRRGGBB}}, where {{ic|X}} is the hexadecimal index of the color from 0-F, and {{ic|RRGGBB}} is a traditional hexadecimal RGB code.  
The colors in the [[w:Virtual console|Linux virtual console]] running on the framebuffer can be changed. This is done by writing the escape code {{ic|\\e]PXRRGGBB}}, where {{ic|X}} is the hexadecimal index of the color from 0-F, and {{ic|RRGGBB}} is a traditional hexadecimal RGB code.  


For example, to reuse existing colors defined in {{ic|~/.Xresources}}, add the following to the shell initialization file (such as {{ic|~/.bashrc}}):
For example, to reuse existing colors defined in {{ic|~/.Xresources}}, add the following to the shell initialization file (such as {{ic|~/.bashrc}}):
Line 334: Line 390:
The below is a colored example of the virtual console login screen in {{ic|/etc/issue}}. Create a backup of the original file with {{ic|mv /etc/issue /etc/issue.bak}} as root, and create a new {{ic|/etc/issue}}:
The below is a colored example of the virtual console login screen in {{ic|/etc/issue}}. Create a backup of the original file with {{ic|mv /etc/issue /etc/issue.bak}} as root, and create a new {{ic|/etc/issue}}:


  echo -e '\e[H\e[2J' > issue
  \e[H\e[2J
echo -e '                                                            \e[1;30m| \e[34m\\s \\r' >> issue
                                                              \e[1;30m| \e[34m\r \s
echo -e '      \e[36;1m/\\\\                     \e[37m||     \e[36m| |                  \e[30m|' >> issue
      \e[36;1m/\\\\                       \e[37m||     \e[36m| =                \e[30m|
echo -e '      \e[36m/  \\\\                     \e[37m||     \e[36m|     _              \e[30m| \e[32m\\t' >> issue
      \e[36m/  \\\\                       \e[37m||     \e[36m|                   \e[30m| \e[32m\t
echo -e '    \e[1;36m/ \e[0;36m.. \e[1m\\\\  \e[37m//==\\\\\\\\ ||/= /==\\\\ ||/=\\\\  \e[36m| | |/ \\\\ |  | \\\\ /     \e[30m| \e[32m\\d' >> issue
    \e[1;36m/ \e[0;36m.. \e[1m\\\\  \e[37m//==\\\\\\ ||/= /==\\\\ ||/=\\\\  \e[36m| | |/\\\\ |  | \\\\ / \e[30m| \e[32m\d
echo -e '    \e[0;36m/ .  . \\\\ \e[37m|| || ||   |   ||  || \e[36m| | |  | |  | X     \e[1;30m|' >> issue
    \e[0;36m/ .  . \\\\ \e[37m||   || || ||     ||  || \e[36m| | |  | |  |   X   \e[1;30m|
echo -e '  \e[0;36m/  .  .  \\\\ \e[37m\\\\\\\\==/| ||  \\\\==/ ||  || \e[36m| | |  | \\\\_/| / \\\\     \e[1;30m| \e[31m\\U' >> issue
  \e[0;36m/  .  .  \\\\ \e[37m\\\\\\==/| ||  \\\\==/ ||  || \e[36m| | |  |\ \\/| / \\\\ \e[1;30m| \e[31m\U
echo -e '  \e[0;36m/ ..    .. \\\\  \e[0;37mA simple, lightweight linux distribution. \e[1;30m|' >> issue
  \e[0;36m/ ..    .. \\\\  \e[0;37mA simple, lightweight linux distribution.   \e[1;30m|
  echo -e ' \e[0;36m/_\x27       `_\\\\                                             \e[1;30m| \e[35m\\l \e[0mon \e[1;33m\\n' >> issue
  \e[0;36m/_'       `_\\\\                                             \e[1;30m| \e[35m\l \e[0mon \e[1;33m\n
  echo -e ' \e[0m' >> issue
  \e[0m  
echo -e '' >> issue
 
Save the file and make it executable with {{ic|chmod +x /etc/issue}}.


See also:
See also:


* https://bbs.archlinux.org/viewtopic.php?pid=386429#p386429
* https://bbs.archlinux.org/viewtopic.php?pid=386429#p386429
* http://www.linuxfromscratch.org/blfs/view/svn/postlfs/logon.html
* https://www.linuxfromscratch.org/blfs/view/svn/postlfs/logon.html


=== X window system ===
=== X window system ===
Line 390: Line 443:
* [[#Using X resources]] for how to color bold and underlined text automatically.
* [[#Using X resources]] for how to color bold and underlined text automatically.
* [https://web.archive.org/web/20090130061234/http://phraktured.net/terminal-colors/ Color Themes] - Extensive list of terminal color themes by Phraktured.
* [https://web.archive.org/web/20090130061234/http://phraktured.net/terminal-colors/ Color Themes] - Extensive list of terminal color themes by Phraktured.
* [http://xcolors.net/ Xcolors.net] List of user-contributed terminal color themes.
* [https://github.com/dkeg/xcolors Xcolors by dkeg] (see files with paths matching {{ic|theme/dkeg - ''theme''}} in the repository)
* [http://beta.andrewrcraig.us/index.php?page=xcolors Xcolors by dkeg]
* [https://github.com/chriskempson/base16 base16 color schemes]


* [https://github.com/chriskempson/base16 base16 color schemes]
=== Display the 256 colors ===


=== Display all 256 colors ===
{{Expansion|256 is far from "all" the colors. We really need a bit of introduction to the various color modes.}}


Prints all 256 colors across the screen.
Prints the 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)
  $ (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)
Line 407: Line 460:
Replace {{ic|tput op}} with whatever tput you want to trace. {{ic|op}} is the default foreground and background color.
Replace {{ic|tput op}} with whatever tput you want to trace. {{ic|op}} is the default foreground and background color.


{{hc
{{hc|<nowiki>$ ( strace -s5000 -e write tput op 2>&2 2>&1 ) | tee -a /dev/stderr | grep -o '"[^"]*"'</nowiki>|
|<nowiki>$ ( strace -s5000 -e write tput op 2>&2 2>&1 ) | tee -a /dev/stderr | grep -o '"[^"]*"'</nowiki>
033[\033[1;34m"\33[39;49m"\033[00m
|033[\033[1;34m"\33[39;49m"\033[00m
}}
}}


Line 416: Line 468:
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.
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.


{{hc
{{hc|<nowiki>$ for T in `find /usr/share/terminfo -type f -printf '%f '`;do echo "$T `tput -T $T colors`";done|sort -nk2</nowiki>|
|<nowiki>$ for T in `find /usr/share/terminfo -type f -printf '%f '`;do echo "$T `tput -T $T colors`";done|sort -nk2</nowiki>
Eterm-88color 88
|Eterm-88color 88
rxvt-88color 88
rxvt-88color 88
xterm+88color 88
xterm+88color 88
Line 441: Line 492:
This command is useful to see what features that are supported by your terminal.
This command is useful to see what features that are supported by your terminal.


{{hc
{{hc|<nowiki>$ infocmp -1 | tr -d '\0\t,' | cut -f1 -d'=' | grep -v "$TERM" | sort | column -c80</nowiki>|
|<nowiki>$ infocmp -1 | sed -nu 's/^[ \000\t]*//;s/[ \000\t]*$//;/[^ \t\000]\{1,\}/!d;/acsc/d;s/=.*,//p'|column -c80</nowiki>
acsc ed kcuu1 kich1 rmso
|bel cuu ich kb2 kf15 kf3 kf44 kf59 mc0 rmso smul
am el kDC kLFT rmul
blink cuu1 il kbs kf16 kf30 kf45 kf6 mc4 rmul tbc
bce el1 kdch1 km rs1
bold cvvis il1 kcbt kf17 kf31 kf46 kf60 mc5 rs1 u6
bel enacs kel kmous rs2
cbt dch ind kcub1 kf18 kf32 kf47 kf61 meml rs2 u7
blink eo kend knp s0ds
civis dch1 indn kcud1 kf19 kf33 kf48 kf62 memu sc u8
bold flash kEND kNXT s1ds
clear dl initc kcuf1 kf2 kf34 kf49 kf63 op setab u9
btns#5 fsl kent kpp s2ds
cnorm dl1 invis kcuu1 kf20 kf35 kf5 kf7 rc setaf vpa
bw home kf1 kPRV s3ds
ccc hpa kf10 kRIT sc
civis hs kf11 kslt setab
clear ht kf12 lines#24 setaf
cnorm hts kf13 lm#0 setb
colors#0x100 ich kf14 mc0 setf
cols#80 ich1 kf15 mc4 sgr
cr il kf16 mc5 sgr0
csr il1 kf17 mc5i sitm
cub ind kf18 mir smacs
cub1 indn kf19 msgr smam
cud initc kf2 ncv#0 smcup
cud1 is1 kf20 npc smir
cuf is2 kf3 op smkx
cuf1 it#8 kf4 pairs#0x7fff smso
cup ka1 kf5 rc smul
cuu ka3 kf6 rev tbc
cuu1 kb2 kf7 ri tsl
cvvis kbs kf8 rin u6
dch kc1 kf9 ritm u7
dch1 kc3 kfnd rmacs u8
dl kcbt kFND rmam u9
dl1 kcub1 kHOM rmcup vpa
dsl kcud1 khome rmir xenl
ech kcuf1 kIC rmkx xon
 
}}
}}


Line 455: Line 531:


See [https://paste.xinu.at/m-dAiJ/] for scripts which display a chart of your current terminal scheme.
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 {{ic|xterm-direct}}, {{ic|konsole-direct}}, {{ic|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 {{ic|rgb_foreground}}, {{ic|rgb_background}}). For this reason, there is no terminfo entry {{ic|linux-direct}}.


== See also ==
== See also ==


* [https://gkbrk.com/2016/07/lolcat-clone-in-x64-assembly/ lolcat clone in x64 assembly]
* [https://gkbrk.com/2016/07/lolcat-clone-in-x64-assembly/ lolcat clone in x64 assembly]

Latest revision as of 09:56, 3 March 2024

This article or section needs expansion.

Reason: And maybe create something bigger. Please take active approach if you can add some valuable information. Mention python-pywal. (Discuss in Talk:Color output in console#Why and what this page is about)

This article or section needs language, wiki syntax or style improvements. See Help:Style for reference.

Reason: Page is partly a collection of information on different applications, partly begin to lay theory to color output process. Please contribute your knowledge, if you can. (Discuss in Talk:Color output in console)

This page was created to consolidate colorization of CLI outputs.

Background

This article or section needs expansion.

Reason: Probably a good idea to move the list-color scripts here now. (Discuss in Talk:Color output in console)

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 practise, 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) § GREP COLORS for more information.

The -n option includes file line numbers in the output.

ip

ip(8) command from iproute2 supports colors with -c option. You can use an alias to enable colored output. When using auto parameter, colored output will be enabled only when stdout is a terminal.

alias ip='ip -color=auto'

less

Environment variables

As with the #man case, we can tell less to emit colors when it is meaning to make bold text and other formatting effects.

Add the following lines to your shell configuration file:

export LESS='-R --use-color -Dd+r$Du+b$'

It will set red for bold and blue for underlined.

For more information about the --use-color and -D options, see less(1) § D or [1].

Reading from stdin

Note: It is recommended to add colored output through #Environment variables to your ~/.bashrc or ~/.zshrc, as the below is based on export LESS='-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 | less

In 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:

  • ColorThis — Force colored output of a program by running it within a (group of) pty, support forwarding stdin.
https://github.com/Sasasu/ColorThis || colorthis-gitAUR
  • stdoutisatty — A small program and a LD_PRELOAD-able library that catches the isatty(3) function call.
https://github.com/lilydjwg/stdoutisatty. || stdoutisatty
Example: stdoutisatty program | less
  • unbuffer — A tclsh script comes with expect, it invokes desired program within a pty.
http://expect.sourceforge.net/example/unbuffer.man.html || expect
Example: unbuffer program | less

Alternatively, using zpty module from zsh: [2]

~/.zshrc
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
}

Usage:

$ ptyless program

To pipe it to other pager (less in this example):

$ pty program | less

ls

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.

Note: 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 environment variable like this:
eval $(dircolors -p | perl -pe 's/^((CAP|S[ET]|O[TR]|M|E)\w+).*/$1 00/' | dircolors -)

See ls(1) for more information.

man

This article or section needs language, wiki syntax or style improvements. See Help:Style for reference.

Reason: The grotty utility is mentioned only in the intro - where is it actually used? Also, there are more subsections than just less and most. (Discuss in Talk:Color output in console)

There is a real color facility in grotty(1), but it is strongly discouraged for man pages. Here we fake a colored man by hacking two main pagers, less and most: we replace the sequences for bold, standout, and underline with spiced ones that contain color.

Using bat

bat can be used as a colorizing pager for man, by setting the MANPAGER environment variable as documented here.

Using less

See #less for a more detailed description.

export MANPAGER="less -R --use-color -Dd+r -Du+b"
export MANROFFOPT="-P -c"

For Fish you could accomplish this with:

~/.config/fish/config.fish
set -xU MANPAGER 'less -R --use-color -Dd+r -Du+b'
set -xU MANROFFOPT '-P -c'

Remember to source your config or restart your shell to make the changes take effect.

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 -s

Test the new setup by typing:

$ man whatever_man_page

Modifying 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 black

This article or section is being considered for removal.

Reason: How is the following relevant to Color output in console? (Discuss in Talk:Color output in console)

A list of all keybindings may be found at /usr/share/doc/most/most-fun.txt. To get a basic less/vim-like configuration, you can copy /usr/share/doc/most/lesskeys.rc to ~/.mostrc. The lesskeys rc included with most does not include 'g' or 'G', so you will also have to add these lines to ~/.mostrc:

setkey bob "g"
setkey eob "G"
setkey page_down "d"
setkey page_up "u"

You may also want to set the goto_line keybinding in the rc if you do not like the default of 'J'.

Another example showing keybindings similar to less (jump to line is set to 'J'):

% less-like keybindings
unsetkey "^K"
unsetkey "g"
unsetkey "G"
unsetkey ":"

setkey next_file ":n"
setkey find_file ":e"
setkey next_file ":p"
setkey toggle_options ":o"
setkey toggle_case ":c"
setkey delete_file ":d"
setkey exit ":q"

setkey bob "g"
setkey eob "G"
setkey down "e"
setkey down "E"
setkey down "j"
setkey down "^N"
setkey up "y"
setkey up "^Y"
setkey up "k"
setkey up "^P"
setkey up "^K"
setkey page_down "f"
setkey page_down "^F"
setkey page_up "b"
setkey page_up "^B"
setkey other_window "z"
setkey other_window "w"
setkey search_backward "?"
setkey bob "p"
setkey goto_mark "'"
setkey find_file "E"
setkey edit "v"

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:         cyan

which replaces the decorations with the colors. Also add:

*VT100.veryBoldColors: 6

if you want colors and decorations (bold or underline) at the same time. See xterm(1) § veryBoldColors for more information.

rxvt-unicode
URxvt.colorIT:      #87af5f
URxvt.colorBD:      #d7d7d7
URxvt.colorUL:      #87afd7

Run:

$ xrdb -load ~/.Xresources

Launch 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

This article or section is a candidate for moving to [[]].

Notes: Some of these could be made into sections of their own, or moved to existing sections such as #diff (Discuss in Talk:Color output in console)

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.

Warning: 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.
  • rainbow — Colorize commands output or STDIN using patterns.
    Presets: df, diff, env, host, ifconfig, java-stack-trace, jboss, jonas, md5sum, mvn2, mvn3, ping, tomcat, top, traceroute.
https://github.com/nicoulaj/rainbow || rainbowAUR
  • grc — Yet another colouriser for beautifying your logfiles or output of commands.
    Presets: cat, cvs, df, diff, dig, gcc, g++, ls, ifconfig, make, mount, mtr, netstat, ping, ps, tail, traceroute, wdiff, blkid, du, dnf, docker, docker-machine, env, id, ip, iostat, last, lsattr, lsblk, lspci, lsmod, lsof, getfacl, getsebool, ulimit, uptime, nmap, fdisk, findmnt, free, semanage, sar, ss, sysctl, systemctl, stat, showmount, tune2fs and tcpdump.
https://github.com/garabik/grc || grc
  • cope — A colourful wrapper for terminal programs.
    Presets: acpi, arp, cc, df, dprofpp, fdisk, free, g++, gcc, id, ifconfig, ls, lspci, lsusb, make, md5sum, mpc, netstat, nm, nmap, nocope, ping, pmap, ps, readelf, route, screen, sha1sum, sha224sum, sha256sum, sha384sum, sha512sum, shasum, socklist, stat, strace, tcpdump, tracepath, traceroute, w, wget, who, xrandr.
https://github.com/yogan/cope || cope-gitAUR
  • cw — A non-intrusive real-time ANSI color wrapper for common unix-based commands. Wraps file which can cause issues.
    Presets: arp, arping, auth.log@, blockdev, cal, cksum, clock, configure, cpuinfo@, crontab@, cw-pipe, cw-test.cgi, date, df, diff, dig, dmesg, du, env, figlet, file, find, finger, free, fstab@, fuser, g++, gcc, group@, groups, hdparm, hexdump, host, hosts@, id, ifconfig, inittab@, iptables, last, lastlog, lsattr, lsmod, lsof, ltrace-color, make, md5sum, meminfo@, messages@, mount, mpg123, netstat, nfsstat, nmap, nslookup, objdump, passwd@, ping, pmap, pmap_dump, praliases, profile@, protocols@, ps, pstree, quota, quotastats, resolv.conf@, route, routel, sdiff, services@, showmount, smbstatus, stat, strace-color, sysctl, syslog, tar, tcpdump, tracepath, traceroute, umount, uname, uptime, users, vmstat, w, wc, whereis, who, xferlog.
http://cwrapper.sourceforge.net/ || cwAUR
  • ccze — A fast log colorizer written in C, intended to be a drop-in replacement for colorize
https://github.com/cornet/ccze/ || ccze

Libraries for colorizing an output

  • libtextstyle — A C library for styling text output to terminals
https://www.gnu.org/software/gettext/libtextstyle/manual/index.html || gettext
  • ruby-rainbow — Rainbow is extension to ruby's String class adding support for colorizing text on ANSI terminal
https://rubygems.org/gems/rainbow/ || ruby-rainbow
  • python-blessings — A thin, practical wrapper around terminal coloring, styling, and positioning
https://github.com/erikrose/blessings || python-blessingsAUR
  • lolcat — Ruby program that makes the output colorful like a rainbow
https://github.com/busyloop/lolcat/ || lolcat

Application specific

Compilers

  • colorgcc — A Perl wrapper to colorize the output of compilers with warning/error messages matching the gcc output format
https://schlueters.de/colorgcc.html[dead link 2024-03-03 ⓘ] || colorgcc

diff

Diff has built-in color output, which is reasonable to use. But the following wrappers can be used:

  • colordiff — Perl script for diff highlighting.
https://www.colordiff.org/ || colordiff
  • cwdiff(w)diff wrapper with directories support and highlighting.
https://github.com/junghans/cwdiff || cwdiffAUR
  • git-delta — A syntax-highlighting pager for git and diff output.
https://github.com/dandavison/delta || git-delta

cat

  • bat — Cat clone with syntax highlighting and git integration.
https://github.com/sharkdp/bat || bat

less

source-highlight

You can enable code syntax coloring in less. First, install source-highlight, then add these lines to your shell configuration file:

~/.bashrc
export LESSOPEN="| /usr/bin/source-highlight-esc.sh %s"
export LESS='-R '
lesspipe

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:

$ less compressed_file.tar.gz
==> 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)

lesspipe 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

  • colormake — A simple wrapper around make to make its output more readable.
https://github.com/pagekite/Colormake/ || colormakeAUR

Ping

  • prettyping — Add some great features to ping monitoring. A wrapper around the standard ping tool with the objective of making the output prettier, more colorful, more compact, and easier to read.
https://denilson.sa.nom.br/prettyping/ || prettyping

Shells

bash

See Bash/Prompt customization#Colors.

Fish

See Fish#Web interface.

xonsh

See Customizing the Prompt.

zsh

See Zsh#Colors.

Terminal emulators

Virtual console

This article or section needs language, wiki syntax or style improvements. See Help:Style for reference.

Reason: Lacks clarity on what "the colors" are, i.e. in #Virtual console they are about the representations of the 16 base colors (RGB values for yellow, red, blue, etc.), while in #Login screen they are about the base colors themselves. See also console_codes(4) and User:Isacdaavid/Linux_Console (Discuss in Talk:Color output in 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
fi

Login 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[0m 

See also:

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:

~/.Xresources
! 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
Warning: Color 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:

Display the 256 colors

This article or section needs expansion.

Reason: 256 is far from "all" the colors. We really need a bit of introduction to the various color modes. (Discuss in Talk:Color output in console)

Prints the 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

This article or section is a candidate for merging with Bash/Prompt_customization.

Notes: More context on tput is provided in that article (Discuss in Talk:Color output in console)

Replace tput op with whatever tput you want to trace. op is the default foreground and background color.

$ ( strace -s5000 -e write tput op 2>&2 2>&1 ) | tee -a /dev/stderr | grep -o '"[^"]*"'
033[\033[1;34m"\33[39;49m"\033[00m

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.

$ for T in `find /usr/share/terminfo -type f -printf '%f '`;do echo "$T `tput -T $T colors`";done|sort -nk2
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

Enumerate terminal capabilities

This article or section is a candidate for merging with Bash/Prompt_customization.

Notes: More context on tput is provided in that article (Discuss in Talk:Color output in console)

This command is useful to see what features that are supported by your terminal.

$ infocmp -1 | tr -d '\0\t,' | cut -f1 -d'=' | grep -v "$TERM" | sort | column -c80
acsc		ed		kcuu1		kich1		rmso
am		el		kDC		kLFT		rmul
bce		el1		kdch1		km		rs1
bel		enacs		kel		kmous		rs2
blink		eo		kend		knp		s0ds
bold		flash		kEND		kNXT		s1ds
btns#5		fsl		kent		kpp		s2ds
bw		home		kf1		kPRV		s3ds
ccc		hpa		kf10		kRIT		sc
civis		hs		kf11		kslt		setab
clear		ht		kf12		lines#24	setaf
cnorm		hts		kf13		lm#0		setb
colors#0x100	ich		kf14		mc0		setf
cols#80		ich1		kf15		mc4		sgr
cr		il		kf16		mc5		sgr0
csr		il1		kf17		mc5i		sitm
cub		ind		kf18		mir		smacs
cub1		indn		kf19		msgr		smam
cud		initc		kf2		ncv#0		smcup
cud1		is1		kf20		npc		smir
cuf		is2		kf3		op		smkx
cuf1		it#8		kf4		pairs#0x7fff	smso
cup		ka1		kf5		rc		smul
cuu		ka3		kf6		rev		tbc
cuu1		kb2		kf7		ri		tsl
cvvis		kbs		kf8		rin		u6
dch		kc1		kf9		ritm		u7
dch1		kc3		kfnd		rmacs		u8
dl		kcbt		kFND		rmam		u9
dl1		kcub1		kHOM		rmcup		vpa
dsl		kcud1		khome		rmir		xenl
ech		kcuf1		kIC		rmkx		xon

Color scheme scripts

See [3] 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 [4]. For more info about terminal emulators and applications that support true color, see [5].

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.

See also