在bashrc中为我的PS1命令添加一个函数调用



我在.bashrc中添加了以下函数,以显示我在终端中的哪个git分支

function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}

我知道我需要这样称呼它:

PS1="h:W u$BLUE$(parse_git_branch) $DEFAULT$"

但我目前的.bashrc已经在多个地方为PS1分配了一个stiring。如何更新这些字符串,以便在调用此附加函数时保持当前的颜色格式?

这就是文件中分配PS1的部分当前的样子:

if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="[e]0;${debian_chroot:+($debian_chroot)}u@h: wa]$PS1"
;;
*)
;;
esac

感谢

将函数调用插入到现有提示中。

if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m] $BLUE$(parse_git_branch) $DEFAULT$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w $(parse_git_branch) $ '
fi

颜色提示还包括$BLUE$DEFAULT变量来更改颜色,非颜色提示只有函数调用。

最新更新