彩色的Shell
我常在命令行下工作,以前老被同事说"你整天在那个黑窗口上倒腾什么?" 现在这个问题变成了"你这个花花绿绿的窗口是什么东西?"
其实都是同一个东西:一个兼容于xterm的终端窗口,要么是PuTTY/KiTTY,要么是GNOME Terminal/Xfce Terminal/LXTerminal.不过我这半年将很多东西配上了颜色.
倒不是说我想搞得很花哨,而是有彩色的文字夹杂其中的话,文字的辨识度要高一些--你是不是有很多时候在一大段输出当中找寻提示符上一次出现的位置? 是不是在里面找有没有某个单词?
1. Shell本身
1.1 提示符
bash
通过修改环境变量 PS1 实现,只要里面包含适当的ANSI color sequence即可实现彩色.Debian/Ubuntu上bash提示符号默认有一份彩色配置,但要求自己编辑 ~/.bashrc,设置 force_color_prompt=yes 即可(找到 force_color_prompt=yes 这一行并删除前面的注释符号).
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
CentOS默认没有彩色配置,可以用这两个在线工具来配置: .bashrc generator: create your .bashrc PS1 with a drag and drop interface 和 Bash $PS1 Generator 2.0 (后面这个好像被蔷了).不过我在一些不想"深度定制"的机器上都是用下面的zsh方法来快速搞定.
zsh
没仔细研究怎么配置,好像是通过修改环境变量PROMPT实现.不过用zsh的人一般都会用oh-my-zsh或者prezto套件,里面都提供了丰富的配置模板.
不过zsh自带的也不少(效果参见 Zsh themes for prompts screenshots | blog post ),我在不特别常用的机器上一般都是如下几行:
autoload -Uz promptinit
promptinit
prompt adam2
# 用 prompt -l 可以列出所有的提示符配置,有兴趣可以逐个体验
我比较喜欢adam2这个,因为它可以醒目地将一次次的输入输出分隔开来.

图片来自: Zsh themes for prompts screenshots | blog post
1.2 命令行语法高亮
曾经试过用 fish 作为日常shell,但最后因为跟bash差异太大而放弃了(太多东西不兼容了,而zsh在兼容bash这方面就非常好),不过fish彩色的命令行给我留下了很深的印象:

但bash上面是搞不了这个功能了,zsh倒是有一个zsh-syntax-highlighting,安装方法如下:
mkdir -p ~/.zsh/plugins
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.zsh/plugins/zsh-syntax-highlighting
echo "source ~/.zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ~/.zshrc
source ~/.zshrc

2. ls
2.1 ls --color
大多数情况下,你执行 ls --color 就可以了,它会用彩色来区分不同的文件类型.

(此图来自: More than just colors: My bash settings | blogginess )
你可以添加 alias ls='ls --color=auto' 到你的 ~/.bashrc . auto 这个值可以确保你将 ls 的输出重定向到一个文件时不会产生问题.
如果你想要详细了解这个功能,可以阅读 coreutils 文档里面的相应章节: GNU Coreutils: General output formatting, GNU Coreutils: dircolors invocation (注意配置文件的详细说明其实在 dircolors --print-database 这个命令的输出里面).这里还有一个在线配置工具: LSCOLORS Generator --不过我从来没去配过细节.
2016/06/02 补充: 默认配置下主要文件类型的对应颜色如下(另外默认配置里面还有不少按扩展名配置的颜色,这里不详细说明了)
2.2 k
ls --color 基本上是按照文件类型来确定颜色的.如果你想按照文件大小,新旧乃至git 状态来显示不同颜色,那么可以用这个针对zsh的小脚本: https://github.com/supercrabtree/k
wget -c https://github.com/supercrabtree/k/archive/master.zip -O k.zip
unzip k.zip
mkdir ~/.zsh/plugins
cp k-master/k.sh ~/.zsh/plugins
source ~/.zsh/plugins/k.sh
然后执行 k 就可以了(不过跟 ls --color 不同的是,颜色不是显示在文件名上的,而是大小/时间等列.

2016/06/02 补充: 看到两个ls的彩色版
exahttps://github.com/ogham/exa
ls++https://github.com/trapd00r/ls--
3. 彩色的文档阅读
3.1 彩色的manpage阅读器:
第一个办法如下,不用安装任何其它东西(前提是采用 less 作为pager -- 虽然一般情况下默认都是用这个):
export LESS_TERMCAP_mb=$(tput bold; tput setaf 2) # green
export LESS_TERMCAP_md=$(tput bold; tput setaf 6) # cyan
export LESS_TERMCAP_me=$(tput sgr0)
export LESS_TERMCAP_so=$(tput bold; tput setaf 3; tput setab 4) # yellow on blue
export LESS_TERMCAP_se=$(tput rmso; tput sgr0)
export LESS_TERMCAP_us=$(tput smul; tput bold; tput setaf 7) # white
export LESS_TERMCAP_ue=$(tput rmul; tput sgr0)
export LESS_TERMCAP_mr=$(tput rev)
export LESS_TERMCAP_mh=$(tput dim)
export LESS_TERMCAP_ZN=$(tput ssubm)
export LESS_TERMCAP_ZV=$(tput rsubm)
export LESS_TERMCAP_ZO=$(tput ssupm)
export LESS_TERMCAP_ZW=$(tput rsupm)
然后用 man date 就可以看到彩色的manpage了.
如果觉得这个麻烦,不太容易记得住的话,可以用另一个方法:安装 most ,然后执行 PAGER=most man date 就能看到彩色的mapage了(你也可以将 export MANPAGER=most 添加到 ~/.bashrc ,这样以后 man date 就能直接调用这个 most 来阅读 manpage).

出处: Unix / Linux: Display Color Man Pages
3.2 彩色的texinfo阅读器
安装 pinfo 就可以了.

参考: pinfo Linux / Unix Command: Read Info Documentation in Colors
4. git与mercurial的彩色配置
4.1 git
比较新一点的git (>= 1.8.4,因为该版本中将 ''color.ui'' 的默认值改为了 ''auto'' )都会自动在 git status , git log , git diff 等等地方采用颜色.
如果你的git比较老,可以用下面的方法来开启颜色支持:
git config --global color.ui auto
当然也可以详细设置各个子命令是否要启用颜色支持:
git config --global color.status always
git config --global color.grep auto
git config --global color.diff auto
git config --global color.branch never
git config --global color.interactive auto
```sh
详细的说明请参考 [git -config 的手册](https://git-scm.com/docs/git-config#_variables)(或者运行 `git config --help `)
参考:
- [Git显示漂亮日志的小技巧](http://www.coolshell.cn/articles/7755.html)
- [让Git的输出更友好: 多种颜色和自定义log格式](https://www.pureweber.com/article/git-pretty-output/%20)
#### 4.2 mercurial
启用 [Color 扩展](http://mercurial.selenic.com/wiki/ColorExtension) 可以给 `hg status ` , `hg log `和 `hg diff ` 添加颜色.
**配置方法**: 编辑 `~/.hgrc `
[extensions]
color =
**效果**:

具体颜色还可以自行定义,详情请参考 [ColorExtension 的说明](https://www.mercurial-scm.org/wiki/ColorExtension).
参考: [mercurial的几个易用性小技巧 - 巴蛮子 - 博客园](http://www.cnblogs.com/bamanzi/p/mercurial-a-few-tips.html)
### 5 高亮输出中的指定文字
有时候我们需要在一个程序的输出中找到某个单词, `grep --color ` 是比较常用的一个方法:
`grep --color ` 虽然挺不错(尤其是不需要另行安装),但它是一个 **过滤** 工具.然后有些时候我们只是要 **高亮** 输出中的某些文字,而 **不想过滤** 掉其它行,但很奇怪这样的诉求没有在基本工具集里面提供。
#### 5.1 hhighlighter (bash插件)
<<https://github.com/paoloantinori/hhighlighter>>
这是一个bash脚本,它提供了一个h()函数(shell函数也是一种命令),可以用来高亮
安装:
```sh
apt-get install ack-grep
# or: curl http://beyondgrep.com/ack-2.14-single-file > ~/bin/ack && chmod 0755 ~/bin/ack
mkdir -p ~/.bash/plugins/
wget 'https://github.com/paoloantinori/hhighlighter/raw/master/h.sh' -O ~/.bash/plugins/h.sh
echo "source ~/.bash/plugins/h.sh"
source ~/.bash/plugins/h.sh
使用:
$ h
usage: YOUR_COMMAND | h [-i] [-d] pattern1 pattern2...
-i : ignore case
-d : disable regexp
-n : invert colors
说明: hhighlighter传递多个参数时,每个参数会自动采用不同的颜色来高亮
5.2 colorex
<https://github.com/Scopart/colorex/>
这个小工具跟 hhighlighter 功能差不多,区别主要在实现层面: colorex是采用python实现的,而且是个独立脚本,比较容易在其它shell下面用.另外,它也不需要其它工具(比如hhighlighter需要ack)
安装:
wget 'https://github.com/Scopart/colorex/raw/master/colorex' -O ~/bin/colorex
chmod a+x ~/bin/colorex
使用:
# to display every word "ERROR" in red of foo.txt
colorex --red ERROR foo.txt
# to watch logfile.txt displaying "WARNING" in yellow and "INFO" in green
tail -f logfile.txt | colorex -y WARNING --green INFO
# to display "(" and ")" contained in foo.txt in blue
colorex -b '\(|\)' foo.txt
略微有点不爽的是,必须指定颜色参数,多个关键字时得指定多个颜色参数(不要被 colorex --help 显示的 -N 参数误导了,虽然它说是 " display with random colors ",但并不是我们通常理解的随机选用一个颜色
彩色的Shell的更多相关文章
- shell模板-跨目录执行,彩色输出,临时文件,行遍历文件
参数检查 #!/bin/bash set -e if [ ! -n "$1" ];then echo "Usage: #cmd <> []" exi ...
- shell打印彩色输出
字体颜色(8种,3开头) 重置=0,黑色=30,红色=31,绿色=32, 黄色=33,蓝色=34, 洋红=35, 青色=36, 白色=37. 背景颜色(8种,4开头) 重置=0,黑色=40,红色=41 ...
- shell脚本的“奇迹暖暖“之行 -- printf彩色输出
printf \n" \n代表换行 "\e[1;30m \e[0m" 深灰 "\e[1;31m \e[0m" 红色 "\e[1;32m \e ...
- shell常用命令归类整理
shell 命令整理 bash shell 含有许多功能,因此有许多可用的命令:本文档仅罗列了一些常用命令及其使用频率较高的参数.#本文档仅罗列了一些常用命令及其使用频率较高的参数.#vers ...
- Linux shell之打印输出
介绍 经常需要和shell命令打交道,但是一直没有系统的学习,接下来会花1到2个月的时间系统的学习一下shell命令,接下来就开启shell命令的奇妙旅行吧.本章主要介绍shell的打印输出. 知识要 ...
- Shell基础整理
Shell的作用是将用户输入的文本命令转换成内核能识别的数据指令交给内核进行执行,内核需要翻译成二进制交由CPU底层来执行 用户层->Shell->调用对应应用程序->ke ...
- [SHELL进阶] (转)最牛B的 Linux Shell 命令 (四)
1.查看ASCII码表 man 7 ascii 很多人初学编程都会接触到ascii码的概念,有时候为了查某个符号的ascii值,可能还得翻箱倒柜找出当年的课本?Linux Manpage里面其实包含 ...
- Linux命令行–基本的bash shell命令
启动shell: /etc/passwd:包含系统用户账户列表以及每个用户的基本配置信息 每个条目有七个字段,每个字段用冒号隔开 用户名 用户密码 用户的系统UID 用户的系统GID 用户的全名 用户 ...
- Linux Shell脚本攻略 读书笔记
Linux Shell脚本攻略 读书笔记 这是一本小书,总共253页,但内容却很丰富,书中的示例小巧而实用,对我这样总是在shell门前徘徊的人来说真是如获至宝:最有价值的当属文本处理,对这块我单独整 ...
随机推荐
- PHP会话Session
<?php //开启会话,PHP会话也提供多种存储方式,文件.数据库等 session_start(); if(isset($_GET['user'])) { $_SESSION['user'] ...
- Common Scenarios to avoid with DataWarehousing
Database Design Rule Description Value Source Problem Description 1 Excessive sorting and RID lookup ...
- 用VS连接oracle数据库时ORA-12504错误
在用VS2008连接oracle数据库时,可能会出现: ORA-12504: TNS: 监听程序在 CONNECT_DATA 中未获得 SERVICE_NAME 只需在web.config文件Data ...
- 2014-3-11 星期二 晴 [卓有成效 master 摸索计划方案]
今日总结: [汇编]:StudyNew+ReView-->[中]上机,还行,学点新知识 [英语]:Study-->[中]发现这个要变得好啦 [----]:lib [----]:lib--& ...
- [ACM_水题] UVA 11292 Dragon of Loowater [勇士斗恶龙 双数组排序 贪心]
Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shor ...
- Linux:环境变量
环境变量 变量 变量定义:declare tmp,declare是可选的. 变量赋值:tmp=1,=号左右不要有空格. 变量引用:echo $tmp,不要忘记了$号. 环境变量 简单理解了变量的概念, ...
- Html 字体大小单位 px em pt
网页上定义字体大小有常见三种单位,px.em.pt px px是pixel缩写,是基于像素的单位.在浏览网页过程中,屏幕上的文字.图片等会随屏幕的分辨率变化而变化,一个100px宽度大小的图片,在80 ...
- ie下不显示图片
IE支持的图片是必须为RGB三原色的,保存图片时,必须“另存为web可用的格式...”
- 如何利用tomcat搭建一个动态服务器
这篇文章只记录已解压缩包的方式安装,通常linux服务器上也是这样. 1.下载tomcat.zip压缩包. http://tomcat.apache.org/download-70.cgi 2.把zi ...
- Android:改变Activity切换方式(转载)
overridePendingTransition(enterAnim, exitAnim); Intent intent =new Intent(this,item2.class); startAc ...


