
我大部分時間都在 Unix 環境中工作並使用終端模擬器。我嘗試在命令列上使用顏色,因為顏色使輸出更加有用和直觀。
有哪些選項可以為我的終端環境添加顏色?你用什麼技巧?你遇到過哪些陷阱?
不幸的是,對顏色的支援因終端類型、作業系統、術語設定、實用程式、有缺陷的實現等而異。
經過大量實驗後,以下是我的設定的一些提示:
- 我傾向於設置
TERM=xterm-color
,大多數主機(但不是全部)都支持它。 - 我在許多不同的主機、不同的作業系統版本等上工作。如果可能的話,我會盡量讓事情變得簡單和通用。
- 我使用 GNU 做了很多工作
screen
,這又增加了一層樂趣。 - 許多作業系統
dircolors
預設設定了諸如 和 之類的東西,我不想在一百個不同的主機上修改它。所以我嘗試堅持使用預設值。相反,我調整了終端的顏色配置。 對某些部分使用顏色Unix指令(
ls
,grep
,less
,vim
) 和bash 提示符。這些命令似乎使用標準“ANSI 轉義序列“。 例如:alias less='less --RAW-CONTROL-CHARS' export LS_OPTS='--color=auto' alias ls='ls ${LS_OPTS}'
我將發布.bashrc
並回答我自己的問題 Jeopardy Style。
答案1
您可以執行以下幾項操作:
編輯+程式碼
許多編輯器都支援語法突出顯示。vim
並emacs
預設啟用它。你也可以在下面啟用它nano
。
您也可以使用以下命令在終端機上語法突出顯示代碼皮格門斯作為命令列工具。
grep
grep --color=auto
突出顯示所有比賽。您也可以使用它export GREP_OPTIONS='--color=auto'
來使其持久化而無需別名。如果你使用的話--color=always
,它會即使在管道時也使用顏色,這讓事情變得混亂。
LS
ls --color=always
顏色指定:
export LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33'
(提示:dircolors
可能有幫助)
PS1
您可以將 PS1(shell 提示字元)設定為使用顏色。例如:
PS1='\e[33;1m\u@\h: \e[31m\W\e[0m\$ '
將產生如下 PS1:
[黃色]lucas@ubuntu: [紅色]~[正常]$
你可以透過這個發揮真正的創意。作為一個想法:
PS1='\e[s\e[0;0H\e[1;33m\h \t\n\e[1;32mThis is my computer\e[u[\u@\h: \w]\$ '
在終端頂部放置一個欄,其中包含一些隨機資訊。 (為了獲得最佳結果,也可以使用alias clear="echo -e '\e[2J\n\n'"
。)
擺脫轉義序列
如果某些東西在您不希望的情況下卡住輸出顏色,我使用此sed
行來刪除轉義序列:
sed "s/\[^[[0-9;]*[a-zA-Z]//gi"
如果您想要更真實的體驗,您也可以刪除以 開頭的行\e[8m
,它指示終端隱藏文字。 (未得到廣泛支持。)
sed "s/^\[^[8m.*$//gi"
另請注意,這些 ^[ 應該是實際的、文字的 ^[ 。您可以透過在 bash 中按 ^V^[ 來輸入它們,即Ctrl+ V, Ctrl+ [。
答案2
我也用:
export TERM=xterm-color
export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad
如果您喜歡對提示進行著色,定義的顏色變數可能會很有用:
export COLOR_NC='\e[0m' # No Color
export COLOR_BLACK='\e[0;30m'
export COLOR_GRAY='\e[1;30m'
export COLOR_RED='\e[0;31m'
export COLOR_LIGHT_RED='\e[1;31m'
export COLOR_GREEN='\e[0;32m'
export COLOR_LIGHT_GREEN='\e[1;32m'
export COLOR_BROWN='\e[0;33m'
export COLOR_YELLOW='\e[1;33m'
export COLOR_BLUE='\e[0;34m'
export COLOR_LIGHT_BLUE='\e[1;34m'
export COLOR_PURPLE='\e[0;35m'
export COLOR_LIGHT_PURPLE='\e[1;35m'
export COLOR_CYAN='\e[0;36m'
export COLOR_LIGHT_CYAN='\e[1;36m'
export COLOR_LIGHT_GRAY='\e[0;37m'
export COLOR_WHITE='\e[1;37m'
然後我的提示是這樣的:
case $TERM in
xterm*|rxvt*)
local TITLEBAR='\[\033]0;\u ${NEW_PWD}\007\]'
;;
*)
local TITLEBAR=""
;;
esac
local UC=$COLOR_WHITE # user's color
[ $UID -eq "0" ] && UC=$COLOR_RED # root's color
PS1="$TITLEBAR\n\[${UC}\]\u \[${COLOR_LIGHT_BLUE}\]\${PWD} \[${COLOR_BLACK}\]\$(vcprompt) \n\[${COLOR_LIGHT_GREEN}\]→\[${COLOR_NC}\] "
$(vcprompt) 正在我的 ~/sbin 中呼叫 python 腳本,該腳本列印有關當前路徑的版本控制資訊。它包括對 Mercurial、Git、Svn、Cvs 等的支持。來源在這裡。
這是完整原始碼我的提示配置:
答案3
grep
並且ls
已經提到過,如果您想要更多顏色,請查看通用著色器,其最初的目的是為日誌檔案著色,但開箱即用,它也為ping
, traceroute
, gcc
, make
, netstat
, diff
, last
, ldap
, 和著色cvs
。
如果您了解正規表示式,它很容易擴展。我已將ps
和添加nmap
到列表中(如果您加入,grc
我將非常樂意分享這兩個工具的 .conf 檔案)
synaptic
(順便說一句,要透過、等方式安裝它,pacman
您可能會更幸運地搜尋「grc」)
答案4
顏色為手冊頁(更多詳情):
function _colorman() {
env \
LESS_TERMCAP_mb=$'\e[1;35m' \
LESS_TERMCAP_md=$'\e[1;34m' \
LESS_TERMCAP_me=$'\e[0m' \
LESS_TERMCAP_se=$'\e[0m' \
LESS_TERMCAP_so=$'\e[7;40m' \
LESS_TERMCAP_ue=$'\e[0m' \
LESS_TERMCAP_us=$'\e[1;33m' \
LESS_TERMCAP_mr=$(tput rev) \
LESS_TERMCAP_mh=$(tput dim) \
LESS_TERMCAP_ZN=$(tput ssubm) \
LESS_TERMCAP_ZV=$(tput rsubm) \
LESS_TERMCAP_ZO=$(tput ssupm) \
LESS_TERMCAP_ZW=$(tput rsupm) \
GROFF_NO_SGR=1 \
"$@"
}
alias man="LANG=C _colorman man"
function perldoc() { command perldoc -n less "$@" |man -l -; }
顏色為grep(1;32
是亮綠色,其他顏色請參閱此處的其他帖子):
GREP_OPTS='--color=auto' # for aliases since $GREP_OPTIONS is deprecated
GREP_COLOR='1;32' # (legacy) bright green rather than default red
# (new) Matching text in Selected line = green, line numbers dark yellow
GREP_COLORS="ms=${GREP_COLOR}:mc=${GREP_COLOR}:ln=33"
alias grep='grep $GREP_OPTS'
alias egrep='grep -E $GREP_OPTS'
alias fgrep='LC_ALL=C grep -F $GREP_OPTS'
使用LC_ALL=C
fgrep 可以提供效能提升 140 倍
更多顏色GNU 語言:
# use the config at ~/.dircolors if it exists, otherwise generate anew
eval "$( dircolors --sh $(find ~/.dircolors -size +0 2>/dev/null) )"
# Usage: _ls_colors_add BASE NEW [NEW...]
# Have LS color given NEW extensions the way BASE extension is colored
_ls_colors_add() {
local BASE_COLOR="${LS_COLORS##*:?.$1=}" NEW
if [ "$LS_COLORS" != "$BASE_COLOR" ]; then
BASE_COLOR="${BASE_COLOR%%:*}"
shift
for NEW in "$@"; do
if [ "$LS_COLORS" = "${LS_COLORS#*.$NEW=}" ]; then
LS_COLORS="${LS_COLORS%%:}:*.$NEW=$BASE_COLOR:"
fi
done
fi
export LS_COLORS
}
_ls_colors_add zip jar xpi # archives
_ls_colors_add jpg ico JPG PNG webp # images
_ls_colors_add ogg opus # audio (opus now included by default)
export CLICOLOR=1 # BSD auto-color trigger (like ls -G but for everything)
if ls -ld --color=auto / >/dev/null 2>&1
then alias ls="ls -ph --color=auto"
else alias ls="ls -ph"
fi
安裝grc
(通用著色劑) 並將其添加到您的別名中:
if type grc grcat >/dev/null 2>&1; then
colourify() { # using this as a function allows easier calling down lower
if [[ -t 1 || -n "$CLICOLOR_FORCE" ]]
then ${GRC:-grc} -es --colour=auto "$@"
else "$@"
fi
}
# loop through known commands plus all those with named conf files
for cmd in g++ head ld ping6 tail traceroute6 `locate grc/conf.`; do
cmd="${cmd##*grc/conf.}" # we want just the command
type "$cmd" >/dev/null 2>&1 && alias "$cmd"="colourify $cmd"
done
# This needs run-time detection. We even fake the 'command not found' error.
configure() {
if [[ -x ./configure ]]; then
colourify ./configure "$@"
else
echo "configure: command not found" >&2
return 127
fi
}
unalias ll 2>/dev/null
ll() {
if [[ -n "$CLICOLOR_FORCE" || -t 1 ]]; then # re-implement --color=auto
ls -l --color=always "$@" |grcat conf.ls
return ${PIPESTATUS[0]} ${pipestatus[1]} # exit code of ls via bash or zsh
fi
ls -l "$@"
}
fi
顏色為差異:函數內容太多,請使用腳本並在 rc 檔案中為其指定別名(如果安裝了則不需要grc
):
#!/usr/bin/perl
use strict;
use warnings;
open (DIFF, "-|", "diff", @ARGV) or die $!;
my $ydiff = 1;
while (<DIFF>) {
if (not -t 1) {
print;
next;
}
chomp;
$ydiff = 0 if /^[ <>\@+-]/ or ($. == 1 && /^\d+[a-z]{1,5}\d+$/);
my $color = "";
if (! $ydiff && /^[\@+-<>]/) {
$color = (/^[<-](?!--$)/ ? 1 : /^[+>]/ ? 2 : 5);
} elsif ($ydiff && /\t {6}([<|>])(?:\t|$)/) {
$color = ($1 eq "<" ? 1 : $1 eq ">" ? 2 : 4);
}
$color ? printf ("\e[1;3%dm%s\e[0;0m\n",$color,$_) : print "$_\n";
}
close DIFF;
顏色為bash 提示符:
# Shorten home dir, Cygwin drives, paths that are too long
function PSWD() {
local p="$*" space A B cols="${COLUMNS:-`tput cols 2>/dev/null || echo 80`}"
p="${p/$HOME/\~}" # shrink home down to a tilde
if [ -d /cygdrive ] && [ "${p#/cygdrive/?/}" != "$p" ]; then
p="${p:10:1}:${p:11}" # /cygdrive/c/hi -> c:/hi
fi
space="$((${#USER}+${#HOSTNAME}+6))" # width w/out the path
if [ "$cols" -lt 60 ]; then echo -n "$N "; space=-29; p="$p$N\b"; fi
if [ "$cols" -lt "$((space+${#p}+20))" ]; then # < 20 chars for the command
A=$(( (cols-20-space)/4 )) # a quarter of the space (-20 for cmd)
if [ $A -lt 4 ]; then A=4; fi # 4+ chars from beginning
B=$(( cols-20-space-A*2 )) # half (plus rounding) of the space
if [ $B -lt 8 ]; then B=8; fi # 8+ chars from end
p="${p:0:$A}..${p: -$B}"
fi
echo "$p"
}
PSC() { printf $'\[\e[%sm\]' "${*:-0;0}"; }
PR="0;32" # default color used in prompt is green
if [ "$(id -u)" = 0 ]; then
sudo=41 # root is red background
elif [ "$USER" != "${SUDO_USER:-$USER}" ]; then
sudo=31 # not root, not self: red text
else sudo="$PR" # standard user color
fi
PROMPT_COMMAND='[ $? = 0 ] && PS1=${PS1[1]} || PS1=${PS1[2]}'
PSbase="$(PSC $sudo)\u$(PSC $PR)@\h $(PSC 33)\$(PSWD \w)"
PS1[1]="$PSbase$(PSC $PR)\$ $(PSC)"
PS1[2]="$PSbase$(PSC 31)\$ $(PSC)"
PS1="${PS1[1]}"
unset sudo PR PSbase