如何設定命令列終端機的ANSI顏色

如何設定命令列終端機的ANSI顏色

我專門在 Mac OS X 上使用 bash,但我認為答案是 Unix 通用的。

我已經在使用 ANSI 顏色,但我想為資料夾、隱藏檔案等重新分配不同的顏色。

更新:這沒有我預期的那麼有用,因為您不能為隱藏文件分配特定的顏色,而且目錄不能總是顏色相同,因為粘性權限位顏色優先於目錄(橄欖綠在屏幕截圖中)

答案1

使用以下內容修改您的 ~/.profile。您也可以將其放入 ~/.bashrc 或 ~/.bash_profile 中,具體取決於您已經擁有和使用的內容。注意:這顯示預設顏色設定。如果您想看到紅色目錄,請將第一個 e 更改為 B。

    LSCOLORS=exfxcxdxbxegedabagacad

    export LSCOLORS

    export CLICOLOR=1    

儲存您的 ~/.profile 然後執行以下操作:

    . .profile

從命令列。

以下是 ls 手冊頁中的不同顏色設定和屬性順序:

LSCOLORS        The value of this variable describes what color to use for which attribute when colors are enabled with CLICOLOR.  This string is a
                 concatenation of pairs of the format fb, where f is the foreground color and b is the background color.
                 The color designators are as follows:

                       a     black
                       b     red
                       c     green
                       d     brown
                       e     blue
                       f     magenta
                       g     cyan
                       h     light grey
                       A     bold black, usually shows up as dark grey
                       B     bold red
                       C     bold green
                       D     bold brown, usually shows up as yellow
                       E     bold blue
                       F     bold magenta
                       G     bold cyan
                       H     bold light grey; looks like bright white
                       x     default foreground or background

                 Note that the above are standard ANSI colors.  The actual display may differ depending on the color capabilities of the terminal in
                 use.

                 The order of the attributes are as follows:

                       1.   directory
                       2.   symbolic link
                       3.   socket
                       4.   pipe
                       5.   executable
                       6.   block special
                       7.   character special
                       8.   executable with setuid bit set
                       9.   executable with setgid bit set
                       10.  directory writable to others, with sticky bit
                       11.  directory writable to others, without sticky bit

                 The default is "exfxcxdxbxegedabagacad", i.e. blue foreground and default background for regular directories, black foreground and red
                 background for setuid executables, etc.

答案2

dircolors(1) 指令可讓您產生一個指令,您可以在 shell 啟動腳本中評估該指令以設定環境變數 LS_COLORS,該變數告訴 /bin/ls 如何為其輸出著色。詳情請參閱http://linux.die.net/man/5/dir_colors

相關內容