1. 配色方案原理
linux bash 配置分为两部分:
- 命令提示符,存放在 PS1 环境变量中
- 文件类型配色方案,存放在 LS_COLORS 环境变量中
我们可以通过如下命令来查看它们。
# 查看 bash 命令提示符配置
echo $PS1
# 查看 ls 命令颜色配置
echo $LS_COLORS
centos 执行结果如下图
这样,我们要修改配色方案只需要设置这两个变量即可,下面是一个示例方案:
cd ~
vi .bashrc
# 在最后添加如下两句脚本
PS1='[33[1;32m]u[33[00m]@h:[33[36m]w[33[1;32m]$ [33[00m]'
LS_COLORS="$LS_COLOR:di=1;4;33;40:*.c=00;31:*.JAVA=00;31:*.py=00;31:*.js=00;31:*.jar=00;32:*.sh=01;32:*.aac=00;33:*.au=00;33:*.flac=00;33:*.mid=00;33:*.midi=00;33:*.mka=00;33:*.mp3=00;33:*.mpc=00;33:*.ogg=00;33:*.ra=00;33:*.wav=00;33:*.axa=00;33:*.oga=00;33:*.spx=00;33:*.xspf=00;33:*.xls=04;34:*.xlsx=04;34:*.csv=00;34:*.doc=00;34:*.docx=00;34:*.ppt=00;34:*.pdf=00;34:*.jpg=00;35:*.jpeg=00;35:*.gif=00;35:*.bmp=00;35:*.tif=00;35:*.tiff=00;35:*.png=00;35:*.svg=00;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.webm=01;35:*.mp4=01;35:*.vob=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.txt=00;36:*.md=00;36"
效果示意图如下:
2. 文本属性和颜色值
Linux 预定义了可用于试用的文本属性和颜色值。
2.1 文本属性
|
文本属性 |
|
0 - normal text |
|
1- bold or light text (depends on terminal) |
|
2 - dim text |
|
4 - underlined text |
|
5 - blinking text |
|
7 - reversed text |
|
8 - hidden text |
同时设置多个属性时,用分号分隔。
2.2 颜色值
|
前景 |
背景 |
|
30 - black |
40 - black |
|
31 - red |
41 - red |
|
32 - green |
42 - green |
|
33 - yellow |
43 - yellow |
|
34 - blue |
44 - blue |
|
35 - purple |
45 - purple |
|
36 - cyan |
46 - cyan |
|
37 - white |
47 - white |
可以同时配置前景色和背景色,同样以分号分隔。比如:
# 文件夹 加粗、下划线、黄色字体,黑色背景显示
di=1;4;33;40
3. 配置 bash 提示符
默认命令提示符显示并不怎么让人赏心悦目,显示的内容也不一定符合你的心意,这时我们就可以通过自定义来配置自己喜欢的样子。
3.1 默认 bash 提示符
# 查看默认命令提示符配置
echo $PS1
# 默认 bash 提示符
PS1=`[u@h W]$`
- u - 表示用户名,username
- h - 表示主机名,hostname(computer name)
- w - 表示工作目录,working directory
- $ - 表示如果是普通用户则显示'$',如果是 root 用户则显示'#'
3.2 bash 提示符自定义配色方案
在配置颜色前,首先必须知道颜色基本单元的格式:
[33[COLORm]
- 颜色必须放在 [ 和 ]之间
- 颜色必须以33[或者 e[开头,以m结尾,e[和33[作用相同
- 在需要清除颜色信息时,使用 00,见下面例子
据此规则,我们就可以自己定自己的配色方案了:
PS1='[33[1;32m]u[33[00m]@h:[33[35m]W[33[1;32m]$ [33[00m]'
分段说明如下:
- [33[1;32m]u 绿色加粗显示用户名
- [33[00m]@h: 使用00清除颜色信息
- [33[35m]W 紫红色显示工作目录
- [33[1;32m]$ 绿色加粗显示 $ 或 #
- [33[00m] 使用00清除颜色信息
只需把该设置放到 ~/.bashrc 文件的最后,然后执行 source .bashrc 即可生效,效果如下图:
可用于配置的特殊字符项列表:
|
A bell character: a |
|
The date, in “Weekday Month Date” format (e.g., “Tue May 26”): d |
|
The format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required:D{format} |
|
An escape character: e |
|
The hostname, up to the first ‘.’: h |
|
The hostname: H |
|
The number of jobs currently managed by the shell: j |
|
The basename of the shell’s terminal device name: l |
|
A newline: n |
|
A carriage return: r |
|
The name of the shell, the basename of $0 (the portion following the final slash): s |
|
The time, in 24-hour HH:MM:SS format: t |
|
The time, in 12-hour HH:MM:SS format: T |
|
The time, in 12-hour am/pm format: @ |
|
The time, in 24-hour HH:MM format: A |
|
The username of the current user: u |
|
The version of Bash (e.g., 2.00): v |
|
The release of Bash, version + patchlevel (e.g., 2.00.0): V |
|
The current working directory, with $HOME abbreviated with a tilde (uses the $PROMPT_DIRTRIM variable): w |
|
The basename of $PWD, with $HOME abbreviated with a tilde: W |
|
The history number of this command: ! |
|
The command number of this command: # |
|
If the effective uid is 0, #, otherwise $: $ |
|
The character whose ASCII code is the octal value nnn: nnn |
|
A backslash: \ |
|
Begin a sequence of non-printing characters. This could be used to embed a terminal control sequence into the prompt: [ |
|
End a sequence of non-printing characters: ] |
4. 配置文件显示方案
4.1 默认颜色方案
# 查看默认颜色配置
echo $LS_COLORS
在 CentOS 中的默认设置如下:
4.2. 文件类型编码
Linux 中一切皆文件,我们可以对每种类型的文件分别配置不同的文本显示方案,下表列出了各种文件类型的编码。
|
di - directory |
cd - character device |
|
fi - file |
or - orphan symbolic link (points to a file that no longer exists) |
|
ln - symbolic link |
mi - missing file (a missing file that an orphan symbolic link points to) |
|
pi - named pipe(FIFO) |
ex - executable file (has the “x” permission) |
|
so - socket |
*.extension - any file ending with an extension |
|
bd - block device |
|
使用这些编码,可以为每一个类型都设置显示方案,以冒号分隔。比如:
# 目录粗体、红色显示
di=1;31
# 目录粗体、下划线、黄色显示
di=1;4;33
# 0 - normal 是默认值,不需要指定
di=33
# 配置目录、.c 和 .java 文件的颜色方案
di=1;4;33;40:*.c=00;31:*.java=00;31
4.3 自定义文件颜色方案
LS_COLORS="$LS_COLORS:di=1;4;33;40:*.c=00;31:*.java=00;31:*.py=00;31:*.js=00;31:*.jar=00;32:*.sh=01;32:*.aac=00;33:*.au=00;33:*.flac=00;33:*.mid=00;33:*.midi=00;33:*.mka=00;33:*.mp3=00;33:*.mpc=00;33:*.ogg=00;33:*.ra=00;33:*.wav=00;33:*.axa=00;33:*.oga=00;33:*.spx=00;33:*.xspf=00;33:*.xls=04;34:*.xlsx=04;34:*.csv=00;34:*.doc=00;34:*.docx=00;34:*.ppt=00;34:*.pdf=00;34:*.jpg=00;35:*.jpeg=00;35:*.gif=00;35:*.bmp=00;35:*.tif=00;35:*.tiff=00;35:*.png=00;35:*.svg=00;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.webm=01;35:*.mp4=01;35:*.vob=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.txt=00;36:*.md=00;36"
只需把该设置放到 ~/.bashrc 文件的最后,然后执行 source .bashrc 即可生效,效果如下图:
5. 完整配色方案
最后附上完整的配色方案,快来试试吧
# ~/.bashrc
# source ~/.bashrc
PS1='[33[1;32m]u[33[00m]@h:[33[35m]W[33[1;32m]$ [33[00m]'
LS_COLORS="$LS_COLORS:di=1;4;33;40:*.c=00;31:*.java=00;31:*.py=00;31:*.js=00;31:*.tgz=01;31:*.taz=01;31:*.dz=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.rar=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jar=00;32:*.sh=01;32:*.aac=00;33:*.au=00;33:*.flac=00;33:*.mid=00;33:*.midi=00;33:*.mka=00;33:*.mp3=00;33:*.mpc=00;33:*.ogg=00;33:*.ra=00;33:*.wav=00;33:*.axa=00;33:*.oga=00;33:*.spx=00;33:*.xspf=00;33:*.xls=04;34:*.xlsx=04;34:*.csv=00;34:*.doc=00;34:*.docx=00;34:*.ppt=00;34:*.pdf=00;34:*.jpg=00;35:*.jpeg=00;35:*.gif=00;35:*.bmp=00;35:*.tif=00;35:*.tiff=00;35:*.png=00;35:*.svg=00;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.webm=01;35:*.mp4=01;35:*.vob=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.txt=00;36:*.yml=00;36:*.cnf=00;36:*.conf=00;36:*.md=01;36:*.json=01;36"
export TERM=xterm-256color






