Color commands in linux

Color output in console

This article or section needs expansion.

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

This page was created to consolidate colorization of CLI outputs.

Contents

Background

This article or section needs expansion.

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 (2 24 ) 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 what escapes a terminal (usually specified by the TERM env-var) understands. The tput(1) and infocmp(1) commands can be used to access them from command-line.

Applications

diffutils from version 3.4 includes the —color option (GNU mailing list).

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:

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.

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.

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:

It will set red for bold and blue for underlined.

For more information about the —use-color option, check the less(1) man page or [1].

Reading from stdin

/.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 .

Читайте также:  Windows 10 pro 1909 оригинальный образ 2020

Some programs provide an option to disable the interactive tty detection:

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 which catches the isatty function call.

https://github.com/lilydjwg/stdoutisatty. || stdoutisatty-gitAUR 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]

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

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:

The LS_COLORS variable is used to define colors, and it configures various parts of highlighting. Use the dircolors(1) command to set it.

See ls(1) for more information.

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 less

See #less for a more detailed description.

For Fish you could accomplish this with:

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:

Test the new setup by typing:

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

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

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’):

Using X resources

A quick way to add color to manual pages viewed on xterm / uxterm or rxvt-unicode is to modify

xterm

which replaces the decorations with the colors. Also add:

if you want colors and decorations (bold or underline) at the same time. See xterm(1) for a description of the veryBoldColors resource.

rxvt-unicode

Launch a new xterm/uxterm or rxvt-unicode and you should see colorful man pages.

Читайте также:  Можете обновиться до windows 10

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 [[]].

Universal wrappers

(most of them outdated but still functioning)

They go with multiple preconfigured presets that can be changed, and new can be created/contributed.

  • 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, digg, 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

  • colorlogs — Colorize commands output or STDIN using patterns.
    Presets: logs, git status, ant, maven.

https://github.com/memorius/colorlogs || not packaged? search in AUR

  • 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-blessings

  • 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 || colorgcc

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

  • bat — Cat clone with syntax highlighting and git integration.

https://github.com/sharkdp/bat || bat

source-highlight

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

lesspipe

Frequent users of the command line interface might want to install lesspipe .

Читайте также:  Windows не удается прочитать диск

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

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 .

  • colormake — A simple wrapper around make to make its output more readable.

https://github.com/pagekite/Colormake/ || colormakeAUR

  • 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

xonsh

Terminal emulators

Virtual console

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

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

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 :

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:

  • #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 — theme in the repository)
  • base16 color schemes

Display all 256 colors

The factual accuracy of this article or section is disputed.

Prints all 256 colors across the screen.

Display tput escape codes

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

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

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.

Enumerate terminal capabilities

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

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

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

Источник

Оцените статью