URxvt和Bash的自定义配色

来源:互联网 发布:拉曼光谱数据库 编辑:程序博客网 时间:2024/05/21 15:06

本文适用于:

虚拟终端:Rxvt-Unicode-256color

Shell:Bash/Zsh

WHY?

对于我这样的从 Gnome 下的 Terminal 转到 URxvt 的人来说,URxvt 的默认配色实在有点太古老/刺眼/难看,我个人喜欢柔和/易分辨的颜色组合。于是便有了这篇对 URxvt + Bash 的显示颜色自定义。

URxvt 颜色配置

URxvt 是个非常容易个性化配置的虚拟终端软件,一切配置信息都在 “~/.Xdefaults” 或 “~/.Xresources” 中。我们可以借助链接 1 中的颜色显示脚本来查看我们的配置:

?Download colors
123456789101112131415161718192021222324252627
#!/bin/bash##   This file echoes a bunch of color codes to the #   terminal to demonstrate what's available.  Each #   line is the color code of one forground color,#   out of 17 (default + 16 escapes), followed by a #   test use of that color on all nine background #   colors (default + 8 escapes).# T='gYw'   # The test text echo -e "\n                 40m     41m     42m     43m\     44m     45m     46m     47m";  for FGs in  '    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' \            '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' \            '  36m' '1;36m' '  37m' '1;37m';    do FG=${FGs// /}    echo -en " $FGs \033[$FG  $T  "    for BG in 40m 41m 42m 43m 44m 45m 46m 47m;        do echo -en "$EINS \033[$FG\033[$BG  $T  \033[0m";    done    echo;doneecho

把上面的内容保存到 “~/colors” ,并添加执行权限:

$ chmod +x ~/colors

$ ~/colors

下面是由 “colors” 输出的 URxvt 缺省配色:

Xdefaults_default_color

URxvt 的配色由 “.Xdefaults” 中的 “background”, “foreground”, “color x {x = 0~15}” 一共 18 个颜色属性构成。其中 color0 到 color7 是相对较暗的色调,是上图中 40m 到 47m 列的背景色,30m 到 37m 行也是这8个颜色;color8 到color15 是相对明亮的色调,对应上图的 1;30m 到 1;37m,它们只能做为文字的颜色,而不能做背景色;剩下的 m 和 1m 是 background 和 foreground。

下面是我自定义的颜色代码:

URxvt.background:        #000000
URxvt.foreground:        #cccccc
! black
URxvt.color0:           #353535
URxvt.color8:           #666666
! red
URxvt.color1:           #d14646
URxvt.color9:           #ee6363
! green
URxvt.color2:           #7c984c
URxvt.color10:          #9acd32
! brown/yellow
URxvt.color3:           #daa520
URxvt.color11:          #ffc125
! blue
URxvt.color4:           #6f99b4
URxvt.color12:          #7c96b0
! magenta
URxvt.color5:           #993b99
URxvt.color13:          #da4eda
! cyan
URxvt.color6:           #a7a15e
URxvt.color14:          #f0e68c
! white
URxvt.color7:           #bbbbbb
URxvt.color15:          #eeeeee

修改 “.Xdefaults” 后要运行命令:

$ xrdb ~/.Xdefaults

并新开一个终端。下面是我的配色运行 ./colors 后的结果:

额。自我感觉比上面那坨配色顺眼多了哈:)

ls 命令显示自定义颜色

记得哪个网站统计过,ls 好像是使用次数最多的 Linux 命令。经过上面对 URxvt 颜色的配置,本以为会影响到 ls 命令显示的字符颜色,当然它没有什么改变,和缺省的颜色一样。

这是因为 ls 列出文件目录的颜色是由另外一个文件控制的。

环境变量 LS_COLORS 里存放着这些参数,你可以用下面的命令查看之:

$ echo $LS_COLORS

当然,我们看不懂里面写的什么。

默认 ls 颜色配置文件在  “/etc/DIR_COLORS”。用另外的命令可以把配置文件输出出来:

$ dircolors -p >~/.dir_colors

$ vi ~/.dir_colors

这时可以查看系统默认的 ls 输出内容的颜色配置,文件中包含注释,很好理解,并且我们看到了 30m……47m 这样的数字,完全对应上面的 colors 命令输出的颜色。在你的 ~/.bashrc 中加入下面一行代码:

eval `dircolors ~/.dir_colors`

重新开启一个 Bash,用 ls 列目录看下,就会有变化了。下面是我列 home 目录的截图:

另外,你可以在 ~/.dir_colors 文件中添加自定义的文件类型和文件名显示颜色。例如下面我添加的:

# programming languages
.c                   01;35
.cpp               01;35
.h                  00;33
*makefile    01;31
*Makefile   01;31

源文件和头文件用不同颜色显示,同样的用于 makefile 文件。这里要注意的是,针对单个文件名来改变颜色的话,要在文件名前 加上 “*” 。

 

到此,你就可以通过修改 ~/.Xdefaults 和 ~/.dir_colors 两个文件,来配置个性化的颜色方案。

Have fun~~

相关链接:

1. http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html

2. http://linux.die.net/man/5/dir_colors

3. http://forum.ubuntu.org.cn/viewtopic.php?highlight=rxvt-unicode&t=66302

4. http://ubuntuforums.org/showthread.php?p=2889625

5. https://bbs.archlinux.org/viewtopic.php?id=66482

声明:文章未经说明都是原创,转载请注明: 转载自XiFage's Blog

本文链接地址: http://xifage.com/custom-color-of-urxvt-and-bash/

原创粉丝点击