小玩具 -- 教你改造终端命令行提示符
看过 laracast 视频的同学应该都见过 jeffway 的命令行有一个云朵:cloud: 和一个闪电 :zap:,其实是oh-my-zsh的一个主题 cloud,用 Mac 的同学可以看一下超哥以前写的 Config Zsh On Osx。相信许多同学开发都是在虚拟机下进行的,我喜欢多端统一样式,所以今天说一下怎么配置虚拟机里的命令行提示符(prompt)!
什么会影响 bash 的命令行提示(prompt)?
Bash 配置它的命令行提示符主要使用两个环境变量 PS1 和 PS2。这里有关于PS1和PS2甚至PS3,PS4的详解Bash Shell: Take Control of PS1, PS2, PS3, PS4 and PROMPT_COMMAND
简单说一下 PS1 和 PS2
-
PS1就是我们最常看到的,比如每次登陆服务器时,我们输入Linux命令的前缀就是PS1所决定的。也是今天要说的重点。 -
PS2是当命令太长,我们需要\换行来完成一条很长很长的命令时起作用的!比如,
☁ ~ ⚡ echo this is first line \
> this is second line \
> ...
☁ ~ ⚡ echo $PS2
>
看到没,符号>就是PS2所定义的
PS3和PS4请看链接了解详细使用情况
有了修改的目标就好说啦!打开家目录下的.bashrc,找到PS1相关的行,
# 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
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='\[\033[1;36m\]☁ \[\033[1;32m\]\W \[\033[1;36m\]$(parse_git_branch)\]⚡ \[\033[0;37m\]'
else
PS1='☁ \W ⚡ '
fi
# This function lets us know which branch we are on when working in a local repo:
parse_git_branch(){
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/'
}
unset color_prompt force_color_prompt
起关键作用的就是这里啦。首先先注释掉force_color_prompt=yes前面的#号,让我们的客户端有颜色。解释一下PS1的值代表什么意思:
\[\033[1;36m\]表示一种颜色,会对后面的输出产生影响,颜色符号对照表看下面。其中\033是固定值,用来告诉bash使用八进制的ASCI码值来表示颜色。后面的1;36m分两部分,1:加粗高亮,对应的还有0:普通的文字,4:下划线。36就是对应的颜色码值了,它代表Cyan 蓝绿色。(m表示什么暂时不知道了)下面还有其他的颜色码值:
Bash Color Codes:
30: Black
31: Red
32: Green
33: Yellow
34: Blue
35: Purple
36: Cyan
37: White
-
后面的 ☁ 会在客户端显示一个云的标志(目前只在mac下测试过,不知道对window支持的怎么样),紧接着又是一个颜色的符号,它表示
淡绿色 -
\W表示当前目录名,对应的是\w表示当前目录全路径,下面还有其他一些符号表示:
Bash variables:
\a : an ASCII bell character (07)
\d : the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format} : 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
\e : an ASCII escape character (033)
\h : the hostname up to the first '.'
\H : the hostname
\j : the number of jobs currently managed by the shell
\l : the basename of the shell’s terminal device name
\n : newline
\r : carriage return
\s : the name of the shell, the basename of $0 (the portion following the final slash)
\t : the current time in 24-hour HH:MM:SS format
\T : the current time in 12-hour HH:MM:SS format
\@ : the current time in 12-hour am/pm format
\A : the current time in 24-hour HH:MM format
\u : the username of the current user
\v : the version of bash (e.g., 2.00)
\V : the release of bash, version + patch level (e.g., 2.00.0)
\w : the current working directory, with $HOME abbreviated with a tilde
\W : the basename of the current working directory, with $HOME abbreviated with a tilde
! : the history number of this command
# : the command number of this command
\$ : if the effective UID is 0, a #, otherwise a $
\nnn : the character corresponding to the octal number nnn
\ : a backslash
[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
] : end a sequence of non-printing characters
- 后面比较特殊的就是
$(parse_git_branch),因为我在下面定义了一个方法,用来获取当前git的分支名称,这里就是调用这个方法,把分支名称显示到当前命令行提示符中。
# This function lets us know which branch we are on when working in a local repo:
parse_git_branch(){
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/'
}
上面的就是我的配置,效果大概是下面这个样子的

本作品采用《CC 协议》,转载必须注明作者和本文链接
关于 LearnKu
推荐文章: