提示符效果:

代码:
# Sexy Solarized Bash Prompt, inspired by "Extravagant Zsh Prompt"
# Customized for the Solarized color scheme by Sean O‘Neil
if tput setaf 1 &> /dev/null; then
if [[ $(tput colors) -ge 256 ]] 2>/dev/null; then
BASE03=$(tput setaf 234)
BASE02=$(tput setaf 235)
BASE01=$(tput setaf 240)
BASE00=$(tput setaf 241)
BASE0=$(tput setaf 244)
BASE1=$(tput setaf 245)
BASE2=$(tput setaf 254)
BASE3=$(tput setaf 230)
YELLOW=$(tput setaf 136)
ORANGE=$(tput setaf 166)
RED=$(tput setaf 160)
MAGENTA=$(tput setaf 125)
VIOLET=$(tput setaf 61)
BLUE=$(tput setaf 33)
CYAN=$(tput setaf 37)
GREEN=$(tput setaf 64)
else
BASE03=$(tput setaf 8)
BASE02=$(tput setaf 0)
BASE01=$(tput setaf 10)
BASE00=$(tput setaf 11)
BASE0=$(tput setaf 12)
BASE1=$(tput setaf 14)
BASE2=$(tput setaf 7)
BASE3=$(tput setaf 15)
YELLOW=$(tput setaf 3)
ORANGE=$(tput setaf 9)
RED=$(tput setaf 1)
MAGENTA=$(tput setaf 5)
VIOLET=$(tput setaf 13)
BLUE=$(tput setaf 4)
CYAN=$(tput setaf 6)
GREEN=$(tput setaf 2)
fi
BOLD=$(tput bold)
RESET=$(tput sgr0)
else
# Linux console colors. I don‘t have the energy
# to figure out the Solarized values
MAGENTA="\033[1;31m"
ORANGE="\033[1;33m"
GREEN="\033[1;32m"
PURPLE="\033[1;35m"
WHITE="\033[1;37m"
BOLD=""
RESET="\033[m"
fi
parse_git_dirty () {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] && echo "*"
}
parse_git_branch () {
branch=$(git branch --no-color 2> /dev/null | sed -e ‘/^[^*]/d‘ -e "s/* \(.*\)/\1$(parse_git_dirty)/")
echo $branch
}
parse_svn_branch() {
svn info &> /dev/null && parse_svn_url
}
parse_svn_url() {
local url=$(svn info 2>/dev/null | sed -ne ‘s#^Relative URL: ##p‘)
if [[ $url =~ trunk ]]; then
echo trunk
elif [[ $url =~ /branches/ ]]; then
echo $url | sed -e ‘s#^.*/branches/\([^/]*\).*$#branch:\1#‘
elif [[ $url =~ /tags/ ]]; then
echo $url | sed -e ‘s#^.*/tags/\([^/]*\).*$#tag:\1#‘
fi
}
parse_vcs() {
branch=$(parse_git_branch)
if [ -n "$branch" ]; then
echo $branch
else
parse_svn_branch
fi
}
PS1="\[${BOLD}${CYAN}\]\u \[$BASE0\]in \[$BLUE\]\w\[$BASE0\]\$([[ -n \$(git branch 2> /dev/null) || -n \$(svn info 2> /dev/null) ]] && echo \" on \")\[$YELLOW\]\$(parse_vcs)\[$BASE0\] \$ \[$RESET\]"
使用方法:
. 代码保存到 ~/prompt.sh 编辑.bashrc, 追加一行脚本: . ~/prompt.sh
注意事项:
. 可以先手动执行一下脚本,调试下效果: . ~/prompt.sh 如果有回车符\r之类的报错, 请编辑下prompt.sh, 删除其中的windows回车符\r, 因为linux下只是别换行符\n。
推荐一款shell自定义提示符Sexy Solarized Bash Prompt
原文:http://my.oschina.net/u/2400083/blog/514530