dircolors:全局修改颜色设置

use*_*963 48 ls colors bash

我了解ls用于dircolors显示彩色输出。dircolors具有与文件扩展名关联的默认颜色数据库,可以使用命令打印

dircolors --print-database
Run Code Online (Sandbox Code Playgroud)

man dir_colors我所知,系统范围的数据库应该位于/etc/DIR_COLORS. 但是这个文件在我的系统(Debian)上不存在。如何修改系统范围的颜色设置dircolors?当不dircolors --print-database存在文件时,该命令从何处获取设置。

我知道用户可以~/.dircolors使用他的设置拥有特定于用户的文件,但这不适合我,因为我需要为每个人更改设置。

第二个问题是,dircolors 是否可以使用 8 位颜色。我的终端是xterm-256color.

Mat*_*att 44

ls从环境变量中获取颜色设置LS_COLORSdircolors只是一种生成此环境变量的便捷方式。为了让这个环境变量在系统范围内生效,把它放在你的 shell 的启动文件中。

对于bash,您可以将其放入/etc/profile

# `dircolors` prints out `LS_COLORS='...'; export LS_COLORS`, so eval'ing
# $(dircolors) effectively sets the LS_COLORS environment variable.

eval "$(dircolors /etc/DIR_COLORS)"
Run Code Online (Sandbox Code Playgroud)

对于zsh,您要么将其放入,/etc/zshrc要么安排在启动时zsh阅读/etc/profile。您的发行版可能已经zsh这样做了。我只是想指出dircolors,真正适合每个人的设置取决于他们使用的 shell。

至于从何处dircolors获取其设置,当您不指定文件时,它仅使用一些内置默认值。

您可以xterm在 dircolors 文件中使用的 256 色转义码,但请注意,它们仅适用于xterm兼容的终端。例如,它们不能在 Linux 文本控制台上工作。

256 色转义码的格式用于38;5;colorN前景色和48;5;colorN背景色。例如:

.mp3  38;5;160                   # Set fg color to color 160      
.flac 48;5;240                   # Set bg color to color 240
.ogg  38;5;160;48;5;240          # Set fg color 160 *and* bg color 240.
.wav  01;04;05;38;5;160;48;5;240 # Pure madness: make bold (01), underlined (04), blink (05), fg color 160, and bg color 240!
Run Code Online (Sandbox Code Playgroud)


Ell*_*iew 11

简短回答

\n

dircolors(目录颜色的缩写)不能用于全局修改颜色。 为此,您可能需要调整终端模拟器中的调色板。

\n

例如,dircolors不用于调整grepvitmux或所要求的颜色man

\n

dircolors用于调整命令lstree 请求在显示文件名时显示的颜色。

\n

我强调显示请求,因为命令请求的颜色只是发送到终端模拟器的颜色。然后,您的终端模拟器可以选择显示与所要求的颜色不同的颜色。所请求的内容以及最终输出到您将看到的终端屏幕的内容可以是两组不同的颜色。

\n
\n

色彩处理概述

\n

1) 任何基于文本的程序或内置的 shell,都可以通过设计输出 ANSI(美国国家标准协会)颜色请求转义码。这里我强调一下,这些只是对终端的请求,如何显示它们有最终决定权。ANSI 颜色代码系统是一个非常古老的系统,最初起源于独立的硬件终端。

\n

彩色文本请求看起来像:

\n
<requested-color><text-to-be-displayed-in-that-color><next-requested-color><next-text...\n
Run Code Online (Sandbox Code Playgroud)\n

不显示“requested-color”部分。相反,终端模拟器使用它来让屏幕以请求的颜色显示“要以该颜色显示的文本”。

\n

程序可以调用[ncurses][1]将 ANSI 代码输出到终端仿真器。您基本上有 8 种标准颜色,以及可以要求的 8 种更大胆/明亮的颜色。 (ncurses 是 New Curses 的缩写,其中“curses”是光标优化的双关语,或者是用于将基于文本的高效输出到终端屏幕的软件库。)

\n

许多常见的基于文本的命令可以配置为输出 ANSI 颜色代码,以请求终端对它们的输出进行着色。示例有:lstreevimgreptmuxman

\n

这些输出颜色代码(转义前缀控制代码)请求在输出下一个文本之前将显示更改为给定的颜色组合(前景、背景和属性)。同样,这些转义码实际上并不决定您看到的颜色。相反,他们指定特定的调色板选择。

\n

过去有一个标准的调色板。如果您请求绿色,那么您会看到绿色。

\n

但是终端模拟器(例如GNOME Terminal)可以改变这种行为。例如,如果程序通过 ANSI 绿色转义码请求green ,您可以决定在屏幕上 以樱桃红色显示。重要的是要记住,这意味着:每个终端程序中所有请求的绿色都将转换为红色。 换句话说,您不能像grep这样只调整一个特定程序的输出颜色。

\n

2A) 目录命令(例如ls和 )tree有些特殊,因为用户可以将它们配置为根据您的规范按文件名类型对文件名进行着色。man例如,这与除黑色和白色外还使用一组固定的替代颜色不同。

\n

例如,文件夹可能被要求以海军蓝色显示,而链接被要求以浅蓝色显示。

\n

您可以为指定的终端格式(如 ansi 或 putty)、文件基本类型(如文件夹、块或粘性)和/或匹配的文件扩展名(如 *.sh 或 *.png)请求颜色。这是通过使用 LS_COLORS 环境变量给出的设置按文件名类型更改 ANSI 输出颜色代码来完成的。请参阅下文了解具体方法。

\n

2B)还有一些特殊的配色方案,vim例如使用:colorscheme default。(改天再说。)

\n

3)然后你的终端(在我的例子中是 GNOME 终端)解释任何 ANSI 代码以生成你看到的实际显示颜色。

\n
\n

在终端模拟器中全局调整颜色

\n

如果您调整GNOME Terminal(或者可能支持此功能的任何终端仿真器)中的颜色设置,它将影响输出 ANSI 颜色代码的任何和所有文本程序的可观察颜色。这些颜色变化将在所有打开的终端窗口上同时调整(假设您对所有窗口使用相同的终端模拟器)。

\n

如果您使用 2 个不同的终端模拟器(例如GNOME TerminalXTerm),那么每个模拟器都可以以不同的方式调整最终输出颜色。

\n

通过 GNOME 终端首选项,您可以将系统范围的“调色板”(即基于文本的应用程序可用的 16 种颜色集)微调为您自己的自定义颜色集。

\n

GNOME Terminal帮助说:

\n
\n

传统上,终端仿真器提供 16 种调色板,\n您可以在此处进行更改。终端支持扩展的 256\n颜色集,但无法在此处编辑额外的 240 种颜色。终端\n甚至提供对超过1600万种颜色的直接访问,这称为\n\xe2\x80\x9c真彩色\xe2\x80\x9d模式。

\n

如果您对调色板所做的更改似乎没有效果,\n您看到的内容可能是由此类扩展调色板\n颜色或真实颜色组成,而不是 16 种基色。

\n
\n
\n

使用双色

\n

这是它的工作原理:

\n
a)  Run  $ dircolors -p >/etc/DIR_COLORS   # to produce DIR_COLORS template file\n\nb)  Hand edit /etc/DIR_COLORS to adjust ANSI color codes that will be output\n    for various things to display.  For example, any filenames like *.png can \n    request a particular ANSI color combination.  (ANSI color codes are escape\n    prefixes.)\n\nc)  Run dircolors again, this time in both \n       /etc/profile       (for logins)           and\n       /etc/bash.bashrc   (for terminal windows)\n    to read DIR_COLORS and create and export the LS_COLORS \n      environment variable, like this:\n\n        $ eval "$(dircolors -b /etc/DIR_COLORS)"\n
Run Code Online (Sandbox Code Playgroud)\n

dircolors很早就打电话给我/etc/profile,我用它来设置和导出LC_COLORS

\n
# === Create DEFAULT LS_COLORS ENVIRONMENT VARIABLE ===========================\n# /etc/DIR_COLORS is a data file from:  $ dircolors -p > /etc/DIR_COLORS\neval "$(dircolors -b /etc/DIR_COLORS)"      # This reads /etc/DIR_COLORS \n# and then runs these two commands:   \n#\n#   $ LS_COLORS=...     # <-- where this value is created from /etc/DIR_COLORS\n#   $ export LS_COLORS\n
Run Code Online (Sandbox Code Playgroud)\n

作为参考,我如何在 /etc/profile 中的各种命令中启用颜色(每当登录完成时都会读取,(包括$ bash --login))如下,位于本答案的最底部。

\n

请注意,有一些反向视频文件夹和文件显示得很有趣ls(例如 DOS 备份文件)。GNOME 终端(如下)中的任何混乱似乎都不会影响这些颜色。但是您可以编辑 DIR_COLORS 使用dircolors(请参阅上面的注释)来调整 LS_COLORS 的内容。

\n

在此输入图像描述

\n
\n

有关如何调整您自己的系统颜色的提示

\n

从 GNOME 终端菜单中选择Edit| Preferences。在左侧窗格中选择要调整的配置文件,然后在右侧窗格中选择 选项卡Colors

\n

!! 在继续之前,我强烈建议您备份现有的首选项。 屏幕截图会很好。请参阅下面我的屏幕截图。

\n

TL;DR - 如果您不想设置此复选框:\n在此输入图像描述

\n

使用最基本的默认设置,但您想要一个黑色的工作背景和彩色的bash, vim, grep, ls, ... 应用程序,然后尝试像这样设置颜色:

\n

在此输入图像描述

\n

我发现这个颜色对话框一开始有点令人困惑,但一旦你弄清楚它就很合乎逻辑了。最大的问题是我所有的文本应用程序都共享相同的调色板,但它们的使用方式有所不同,尤其是ls.

\n

这是我正在使用的确切颜色:

\n
black           aqua            green           orange          light yellow    purple          blue, primary   gray (X)        <-- X=Don\'t care\n#000000         #01B9E0         #389400         #FF8A00         #F4FF92         #C401C4         #005EEA         #AAAAAA\n0               1               2               3               4               5               6               7\n------------    ------------    ------------    ------------    ------------    ------------    ------------    ------------    <-- 1st set are     "normal   colors"\n\n------------    ------------    ------------    ------------    ------------    ------------    ------------    ------------    <-- 2nd set are the "brighter colors" (for check box below)\n8               9               10              11              12              13              14              15\n#555555         #FF5555         #55FF55         #FFFF55         #5397FE         #FF55FF         #55FFFF         #D6D6D6\ndark gray (X)   red/orange      lime            yellow (X)      blue, medium    pink            aqua, bright    light gray (X)  <-- X=Don\'t care\n
Run Code Online (Sandbox Code Playgroud)\n
\n

了解 GNOME 终端“首选项 - 配置文件”对话框

\n

顶部和底部部分(Text and Background ColorPalette)似乎几乎完全独立运行,除了Palette选择是位于(即进一步修改)顶部设置之上的一层Text and Background Color。(此外,即使Use colors from system theme选中,Show bold text in bright colors底部仍然很重要,至少对于 而言ls。)

\n
\n

首先让我们搞乱设置Text and Background Color

\n

这些确定基本颜色、非特殊颜色TextBackground颜色的默认颜色。如果您愿意,您还可以为BoldHighlight文本以及固定光标颜色选择可选修饰符,例如亮橙色可能会让某些人感到高兴。

\n

Use colors from system theme复选框似乎决定您是否要继承颜色设置或自己定义它们。当您取消选中它时,您可以从下拉列表中选择内置或自定义方案。

\n

您选择的内容将被填充到下面的TextBackground样本中,除了Custom

\n

请注意,您在此处的选择不会影响其下方的其他 3 个复选框。

\n

另请注意,如果您首先单击TextBackground样本,您将获得一个颜色选择器,一旦您Select在其中选择,它会将Built-in schemes下拉菜单设置为“自定义”。

\n

如果您检查Bold color并设置颜色,则由程序指定为粗体的文本将覆盖此颜色。

\n

如果选中Cursor color,则可以为光标选择固定颜色。(注意,您必须将光标移动到文本上才能看到它的变化。)如果没有选中此选项,光标似乎会随着文本颜色的变化而变化。

\n

如果您选中Highlight color,您将在例如 vim 中拖动鼠标复制某些文本时看到这两个样本的效果。

\n
\n

现在是时候搞乱设置了Palette

\n

与上面类似,下拉菜单将一组颜色放入其下方的样本中。您会注意到有两行,每行 8 个。这些色板覆盖了 terminfo 设置的 8 种 ANSI 前景色和 8 种背景色。

\n

重要的是要看到,您可以将这些样本设置为微妙的颜色变化。

\n

您在此处所做的更改还会影响所有或大部分启用了颜色的基于文本的程序。 所以这里要小心。 (我不能告诉你如何首先备份它,因为我还不知道。至少拍一张屏幕截图。)

\n

虽然我不知道所有这些样本的用途是什么,但一些实验得出了以下结论。

\n

在开始测试之前,请从以下位置打开一些内容GNOME Terminal: 的vim代码编辑会话bash、主目录的另一个c代码编辑会话、运行和会话。当您更改下面的内容时,您将能够更好地看到正在发生的变化。lstreetmux

\n

首先尝试做一些小的改变。请务必单击并取消单击底部的复选框(下面将进一步说明)。

\n

注意:Palette如果您将设为Custom,然后按照我的建议手动设置 16 种色样,请务必小心,因为如果您再次在 中选择其他内容Built-in schemes:,它将清除您之前的Palette设置,如果您再次需要,Custom您将拥有一切从头开始,这是一种痛苦。(似乎没有办法保存您的自定义颜色设置,除非使用屏幕截图或将它们一一写下来。)

\n

Built-in schemes: Custom-Color palette:设置

\n
tmux    status text                     status-backgr\ntmux vi                 literals                        flow (orange)   comments&fcns   $names          var  names      ?\nvi C    -               literals        types           flow (brown)    comments&fcns   macros          -               ?\nvi Bash -               literals        -               flow (brown)    comments&fcns   Environ         var  names      ?               flow: brown (can\'t change with gnome)\nBash    -                               user@dir $\nBash ls -                               Reverse-backg                   Reverse-foregr\ngcc\n\n        black           aqua            green           orange          dark yellow    purple          blue, primary   gray (X)        <-- X=Don\'t care\n        #000000         #01B9E0         #389400         #FF8A00         #C6CF76         #C401C4         #005EEA         #AAAAAA\n        0               1               2               3               4               5               6               7\n        ------------    ------------    ------------    ------------    ------------    ------------    ------------    ------------    <-- 1st set are     "normal   colors"\n\n        ------------    ------------    ------------    ------------    ------------    ------------    ------------    ------------    <-- 2nd set are the "brighter colors" (for check box below)\n        8               9               10              11              12              13              14              15\n        #555555         #FF5555         #55FF55         #FFFF55         #5397FE         #FF55FF         #55FFFF         #D6D6D6\n        dark gray (X)   red/orange      lime            yellow (X)      blue, medium    pink            aqua, bright    light gray (X)  <-- X=Don\'t care\n\ntmux    ?                                               ?                                                               ?\ntmux vi ?                                               ?                                                               ?\nvi C    ?                                               ?                                                               ?\nvi Bash ?                                               ?                                                               ?\nBash    ?               root@dir #                      ?                                                               ?\nBash ls ?                               x.sh            ?               directories     media files     links           ?\ngcc     ?               errors                          ?                                                               ?\n
Run Code Online (Sandbox Code Playgroud)\n

最后,在底部有一个Show bold text in bright colors复选框。GNOME 帮助说:

\n
\n

颜色鲜艳,文字粗体

\n

传统上,终端并没有明确区分粗体字体粗细和浓重颜色,通常两者都是一起启用的。最近的改进,例如引入真彩色支持和某些配色方案(例如 Solarized),需要将这两个概念分开,即使亮度独立于字体粗细。

\n

...

\n
Disable Show bold text in bright colors for the new default behavior, the complete separation of color intensity and font weight;\n
Run Code Online (Sandbox Code Playgroud)\n

或启用此选项以提高向后兼容性。

\n
\n

选中后,它将采用顶行中显示的内容,并显示底行的颜色。如果您检查此选项,您会注意到其中的一些名称ls会稍微改变颜色。我认为它与 ISO 6429 颜色序列 #1 相关,“为了更明亮的颜色” $ man dir_colors。看来这影响主要ls而不是vim

\n
\n

内置方案的颜色是什么样的

\n

built-in schemes 使用此处描述的颜色测试实用程序进行测试时,我看到以下结果(提示编译并链接到此$ gcc -Wall -lcurses color-demo.C):

\n

请注意该Show bold text in bright colors复选框已被选中。未经检查,高强度和低强度颜色是相同的。

\n
    \n
  • Linux console(似乎是原始的 CGI 颜色集):
  • \n
\n

在此输入图像描述

\n
    \n
  • xterm:
  • \n
\n

在此输入图像描述

\n
    \n
  • rxvt(乱序,因为这更接近上面的内容):
  • \n
\n

在此输入图像描述

\n
    \n
  • Tango(乱序,因为这更接近下面的 Solarized):
  • \n
\n

在此输入图像描述

\n
    \n
  • Solarized:
  • \n
\n

在此输入图像描述

\n
\n

如何配置我的命令以颜色显示?

\n

在你的(由and/etc/.bashrc.shared调用)中,或者如果你的and中没有:~/.bashrc/root/.bashrc~/.bashrc/root/.bashrc

\n
# === ADD COLOR UNLESS DUMB TERMINAL ========================================\nif [ "$TERM" != "dumb" ]; then\n    # ENVIRONMENT: \n    #   You can turn off these colorizing aliases in the interactive environment by invoking \n    #       shell with  TERM="dumb bash".\n    \n    #   Assume LS_COLORS is already set by dircolors in /etc/profile.  It can be adjusted later.\n    \n    # Using color to distinguish file types is disabled both by default and with --color=never.  \n    #   With --color=auto, ls emits color codes only when standard output is connected to a terminal.  \n     export COLOR_MODE=\'--color=auto\'\n\n\n    #   The use of  alias  limits color effects to the interactive command usage. \n    #       It has advantage over exporting environment variable "export COLOR_MODE=\'--color=auto\'" \n    #       since color can be seen under pager programs such as less(1). If you wish to suppress \n    #       color when piping to other programs, use "--color=auto" instead in the above example \n    #       for "~/.bashrc".\n    #       !! NOTE - alias is very picky about extra spaces around = sign\n\n    # --- grep\n    # Surround the matched (non-empty) strings, matching lines, context lines, file names, \n    #   line numbers, byte offsets, and separators (for fields and groups of context lines) \n    #   with escape sequences to display them in color on the terminal.  \n    #   The colors are defined by the environment variable GREP_COLORS, (plural).\n    #       See grep man page for GREP_COLORS instructions.\n     alias  grep=\' grep ${COLOR_MODE}\'\n     alias egrep=\'egrep ${COLOR_MODE}\'\n     alias fgrep=\'fgrep ${COLOR_MODE}\'\n     alias zgrep=\'zgrep ${COLOR_MODE}\'\n\n    # --- ls ------------------------\n     alias ls=\'ls ${COLOR_MODE}\'\n\n    #   These next two use the ls alias above, i.e. the first one below is really \'ls ${LS_OPTS} -l\'\n     alias ll=\'ls -l\'                   # -l = long\n     alias  l=\'ls -lA\'                  # -A = amost all (.*, but not . nor ..)\n\n    # --- tree ----------------------\n     alias tree=\'tree -C\'               # -C adds color to tree\n\n    # --- less ----------------------\n     alias  less=\'less -R\'              # Output raw control chars for ANSI colors\n\n    # --- man -----------------------\n    # ref: http://www.tuxarena.com/2012/04/tutorial-colored-man-pages-how-it-works/\n    #   NOTE these seem to use TERMCAP which is obsolite.\n    # ANSI "color" escape sequences are sequences of the form:      ESC [ ... m\n        export LESS_TERMCAP_mb=$(printf \'\\e[01;31m\')        # enter blinking            mode\n        export LESS_TERMCAP_md=$(printf \'\\e[01;38;5;75m\')   # enter double-bright       mode\n        export LESS_TERMCAP_me=$(printf \'\\e[0m\')            # turn off all appearance   modes (mb, md, so, us)\n\n        export LESS_TERMCAP_so=$(printf \'\\e[01;33m\')        # enter standout            mode\n        export LESS_TERMCAP_se=$(printf \'\\e[0m\')            #  leave standout           mode\n\n        export LESS_TERMCAP_us=$(printf \'\\e[04;38;5;200m\')  # enter underline           mode\n        export LESS_TERMCAP_ue=$(printf \'\\e[0m\')            #  leave underline          mode\n\nfi\n
Run Code Online (Sandbox Code Playgroud)\n


dev*_*ull 10

当不dircolors --print-database存在文件时,该命令从何处获取设置。

根据手册,它在没有文件的情况下使用预编译的数据库。

如果指定了文件,则 dircolors 读取它以确定要为哪些文件类型和扩展名使用哪些颜色。否则,使用预编译的数据库。有关这些文件格式的详细信息,请运行“ dircolors --print-database”。

为了更改每个人的设置,您可以创建一个/etc/dircolors文件并将以下内容添加到/etc/bashrc

d=/etc/dircolors
test -r $d && eval "$(dircolors $d)"
Run Code Online (Sandbox Code Playgroud)


Eri*_*ski 8

Linux 使用 dircolors 设置控制台背景颜色:

您的 dircolors 文件控制通过 ls 在控制台上显示的单词的颜色。.dircolors为您的发行版找到此文件,帮助链接:

http://www.linuxfromscratch.org/blfs/view/svn/postlfs/profile.html

对于 Fedora 17 上的我来说,我的 dircolors 文件是: /etc/DIR_COLORS

复制/etc/DIR_COLORS到您的/home/el/.dircolors目录中。如果它不存在,则创建它。

编辑 /home/el/.dircolors,查找文本“dir”。

改变这个:

DIR 01;34   # directory
Run Code Online (Sandbox Code Playgroud)

对此:

DIR 01;36   # directory
Run Code Online (Sandbox Code Playgroud)

保存并关闭并重新启动外壳。目录从黑底深蓝(不可读)到黑底亮蓝绿色(可读)。

  • -1:在 OP 的发行版 Debian 中没有 `/etc/DIR_COLORS`。他已经知道`~/.dircolors`,那不是问题 (4认同)