~/.bashrc
来修改设置。
打开文件:gedit ~/.bashrc
。
查找带有#force_color_prompt=yes
的行并取消注释(删除#
)。
查找下面的行if [ "$color_prompt" = yes ]; then
,它应该看起来像:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
注意部分\u@\h
,它表示“用户@主机”,数字\[\033[01;32m\]
之前表示颜色。这是您需要更改的内容。例如,让我们将用户更改为紫色,“@”更改为黑色,主机更改为绿色。编辑该行,使其如下所示:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;35m\]\u\[\033[01;30m\]@\[\033[01;32m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
export PS1="\[\e[01;37m\][\[\e[0m\]\[\e[01;32m\]\u\[\e[0m\]\[\e[00;37m\]@\[\e[0m\]\[\e[01;34m\]\h\[\e[0m\]\[\e[00;37m\] \[\e[0m\]\[\e[00;37m\]\t\[\e[0m\]\[\e[01;37m\] \W]\\$ \[\e[0m\]"
export PS1="\[\e[01;37m\][\[\e[0m\]\[\e[01;31m\]\u\[\e[0m\]\[\e[00;37m\]@\[\e[0m\]\[\e[01;34m\]\h\[\e[0m\]\[\e[00;37m\] \[\e[0m\]\[\e[00;37m\]\t\[\e[0m\]\[\e[01;37m\] \W]\\$ \[\e[0m\]"
export PS1="\[\e[01;33m\]\u\[\e[0m\]\[\e[00;37m\]@\[\e[0m\]\[\e[01;36m\]\h\[\e[0m\]\[\e[00;37m\] \t \[\e[0m\]\[\e[01;35m\]\w\[\e[0m\]\[\e[01;37m\] > \[\e[0m\]"
我现在最喜欢的是:
export PS1="\n\[\e[01;33m\]\u\[\e[0m\]\[\e[00;37m\]@\[\e[0m\]\[\e[01;36m\]\h\[\e[0m\]\[\e[00;37m\] \t \[\e[0m\]\[\e[01;35m\]\w\[\e[0m\]\[\e[01;37m\] \[\e[0m\]\n$ "
NEWLINE=$'\n'
DATE=$( date +"[%Y-%m-%d %H:%M:%S]" )
PROMPT="${NEWLINE}%F{yellow}${DATE} %(!.%F{red}.%F{white})%n%F{cyan}@%m %F{yellow}%d${NEWLINE}%F{reset}> "
详情请参见此详细的HOWTO。
简而言之,您可以通过编辑$PS1环境变量来更改提示符。这里有很多要说的,我只会展示我的提示符并引用上面的链接以获取更多详细信息。
与颜色相关的部分在函数setPrompt
中:
# This function from: https://wiki.archlinux.org/index.php/Color_Bash_Prompt_%28%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9%29#Wolfman.27s
##################################################
# Fancy PWD display function
##################################################
# The home directory (HOME) is replaced with a ~
# The last pwdmaxlen characters of the PWD are displayed
# Leading partial directory names are striped off
# /home/me/stuff -> ~/stuff if USER=me
# /usr/share/big_dir_name -> ../share/big_dir_name if pwdmaxlen=20
##################################################
bash_prompt_shortener() {
# How many characters of the $PWD should be kept
local pwdmaxlen=25
# Indicate that there has been dir truncation
local trunc_symbol=".."
local dir=${PWD##*/}
pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
NEW_PWD=${PWD/#$HOME/\~}
local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
if [ ${pwdoffset} -gt "0" ]
then
NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
fi
}
function setPrompt {
COLOR1="\[\033[1;33m\]" #First color
COLOR2="\[\033[0;33m\]" #Second color
NO_COLOR="\[\033[0m\]" #Transparent - don't change
case $TERM in
xterm*)
TITLEBAR="\[\033]0;\h - \w\007\]"
;;
*)
TITLEBAR=""
;;
esac
local dash_open="${COLOR1}-${COLOR2}-"
local dash_close="${COLOR2}-${COLOR1}-"
local spacer="${COLOR2}-"
local jobs_and_history="${COLOR2}(${COLOR1}\!${COLOR2}:${COLOR1}\j${COLOR2})"
local user_host="${COLOR2}(${COLOR1}\u${COLOR2}@${COLOR1}\H${COLOR2})"
local host="${COLOR2}(${COLOR1}\H${COLOR2})"
local root_or_not="${COLOR2}(${COLOR1}\\\$${COLOR2})"
local cwd="${COLOR2}(${COLOR1}\w${COLOR2})"
#PS1="${TITLEBAR}${COLOR1}-${COLOR2}-(${COLOR1}\!${COLOR2}:${COLOR1}\j${COLOR2})-(${COLOR1}\w${COLOR2})-${COLOR1}-\n-${COLOR2}-(${COLOR1}\u${COLOR2}@${COLOR1}\H${COLOR2})-(${COLOR1}\\\$${COLOR2})-${COLOR1}- ${NO_COLOR}"
#PS1="${TITLEBAR}${dash_open}${cwd}${spacer}${root_or_not}${dash_close}\n${dash_open}${jobs_and_history}${spacer}${host}${dash_close}${NO_COLOR} "
#PS2="${COLOR2}--${COLOR1}- ${NO_COLOR}"
PS1="${TITLEBAR}${COLOR1}"'${NEW_PWD}'"${COLOR2}:\$${NO_COLOR} "
PS2="$spacer$dash_close$NO_COLOR "
}
bash_prompt_shortener
setPrompt
unset setPrompt
#Determine and display the exit Status of the last command, if non-zero.
function checkExitStatus() {
local status="$?"
local signal=""
local COLOR1="\033[0;0;33m" #First color
local COLOR2="\033[0;0;36m" #Second color
local NO_COLOR="\033[0m" #Transparent - don't change
if [ ${status} -ne 0 -a ${status} != 128 ]; then
# If process exited by a signal, determine name of signal.
if [ ${status} -gt 128 ]; then
signal="$(builtin kill -l $((${status} - 128)) 2>/dev/null)"
if [ "$signal" ]; then
signal="$signal"
fi
fi
echo -e "${COLOR1}[Exit ${COLOR2}${status} ${signal}${COLOR1}]${NO_COLOR}" 1>&2
#echo -ne "${COLOR1}[Exit ${COLOR2}${status}${COLOR1} ${COLOR2}${signal}${COLOR1}]${NO_COLOR} " 1>&2
fi
return 0
}
print_prompt_time() {
printf "%*s\r" $(tput cols) "$(date '+%T')"
}
promptCmd() {
checkExitStatus
print_prompt_time
}
PROMPT_COMMAND=promptCmd
checkExitStatus
),以及在最右侧列显示时间(函数print_prompt_time
。PS1="\[\e[34m\][\[\e[m\] \[\e[36m\]\A\[\e[m\] \[\e[37m\]\u\[\e[m\]\[\e[31m\]@\[\e[m\]\[\e[32m\]\h\[\e[m\] \[\e[35m\]~\[\e[m\] \[\e[34m\]]\[\e[m\]\n$ "
sudo nano ~/.bashrc
并添加export
命令:
export PS1="\[\e[34m\][\[\e[m\] \[\e[36m\]\A\[\e[m\] \[\e[37m\]\u\[\e[m\]\[\e[31m\]@\[\e[m\]\[\e[32m\]\h\[\e[m\] \[\e[35m\]~\[\e[m\] \[\e[34m\]]\[\e[m\]\n$ "
Black 0;30 Dark Gray 1;30
Blue 0;34 Light Blue 1;34
Green 0;32 Light Green 1;32
Cyan 0;36 Light Cyan 1;36
Red 0;31 Light Red 1;31
Purple 0;35 Light Purple 1;35
Brown 0;33 Yellow 1;33
Light Gray 0;37 White 1;37
bash
设置(或者你偏好的shell的设置),而不是Gnome终端的设置。 - Scott Severance