在终端输出彩色 - Arch Linux 中文维基

跳转到内容

# 在终端输出彩色

来自 Arch Linux 中文维基

这篇文章的某些内容需要扩充。

原因：And maybe create something bigger. Please take active approach if you can add some valuable information. (在 Talk:在终端输出彩色 中讨论)

本文或本章节的语言、语法或风格需要改进。参考：帮助:风格

原因：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.（在Talk:在终端输出彩色讨论）

相关文章

- Emacs#Custom colors and theme
- nano#Syntax highlighting

翻译状态：

- 本文（或部分内容）译自 Color_output_in_console，最近一次同步于 202510-10，若英文版本有所更改，则您可以帮助同步与翻译更改的内容。
- 您可以在 ArchWiki 的对应页面找到本文翻译的原始修订历史。
- 本文可能与英文原文存在出入。

本文或本节需要翻译。要贡献翻译，请访问简体中文翻译团队。

附注： Not translated yet.（在 Talk:在终端输出彩色# 中讨论）

This page was created to consolidate colorization of CLI outputs.

## Background

这篇文章的某些内容需要扩充。

原因：把列出颜色的脚本放在这里也许是好的。 (在 Talk:在终端输出彩色 中讨论)

### Escape sequences

ANSI escape sequences 定义了一种把附加信息追加在终端输出，并且颜色也是“附加信息”的一部分的方法。从最开始的八种颜色到24位全真式色彩，终端颜色在迅速的扩展。

基本颜色编码具有8种亮度颜色和8种这些颜色更亮的版本。现代终端模拟器，包括 Linux 控制台本身，可以指定翻译为更精确的 RGB 值。这种模式大多数仿真终端都是支持的。

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) § 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`/`-color` option. When using the `auto` parameter, colored output will be enabled only when stdout is a terminal. iproute2包 can be built with `auto` as the default, but such a change has been rejected by the Arch package maintainer.

You can use an alias to enable colored output, e.g.:

    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

注意：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.

  unbuffer(1) || 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.

注意：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

本文或本章节的语言、语法或风格需要改进。参考：帮助:风格

原因：The grotty utility is mentioned only in the intro - where is it actually used? Also, there are more subsections than just `less` and `most`.（在Talk:在终端输出彩色讨论）

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

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

此页面或章节适合移动到 [[]]。

附注： Some of these could be made into sections of their own, or moved to existing sections such as #diff（在 Talk:在终端输出彩色 讨论）

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

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

  https://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/ || cczeAUR

### 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://github.com/colorgcc/colorgcc || 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

本文或本章节的语言、语法或风格需要改进。参考：帮助:风格

原因：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（在Talk:在终端输出彩色讨论）

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:

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

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

警告：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:

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

### Setting foreground and background colors with tput

Example to set bold, yellow text, with blue background:

    $ tput bold; tput setaf 3; tput setab 4

tput command line option cheatsheet
tput command | Description
--- | ---
`tput bold` | Bold text
`tput setaf [CODE]` | Set foreground color
`tput setab [CODE]` | Set background color
`tput rev` | Inverse colors
`tput sgr0` | Reset everything

tput color codes
tput color [CODE] | Color
--- | ---
0 | Black
1 | Red
2 | Green
3 | Yellow
4 | Blue
5 | Magenta
6 | Cyan
7 | White

Example 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

本文或本章节可能需要合并到Bash/Prompt_customization。

附注： More context on tput is provided in that article（在 Talk:在终端输出彩色 中讨论）

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

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

- lolcat clone in x64 assembly

分类：​

- 小提升
- 命令行解释器
- 使用“模板:风格”标记的页面或章节
- 使用“模板:Move”标记的页面或章节

隐藏分类：​

- 使用“模板:Translateme”标记的页面或章节
- 使用“模板:合并”标记的页面或章节

在终端输出彩色
添加话题