我在终端提示符和 vim 实例的最低部分中都显示了一些“损坏”的符号。这些似乎是某种颜色代码参考,但我无法确定它们可能来自哪里。
我在网上找到了parse_git_branch()
和strip_colors()
函数。 PS1 设置本身是从以下任一生成的
https://scriptim.github.io/bash-prompt-generator/
我这么说只是为了澄清我在 bash 方面“不太擅长”:p
有关如何解决的任何提示吗?先感谢您!
迅速的
vim
force_color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
strip_colors() {
sed 's/\\[eE][[0-9]*;[0-9]*m//g' <<< $PS1
# You can use it like this
# PS1="..."
# export PS1=$(strip_colors)
}
if [ "$color_prompt" = yes ]; then
...
# green with 'default' text colors
PS1="\n\[\e[32m\](\[\e[0m\]\u\[\e[32m\])(\[\e[0m\]jobs:\j\[\e[32m\])(\[\e[0m\]\w\[\e[32m\])\$(parse_git_branch)\[\033[00m\]\n\[\e[0m\]\$ "
else
PS1="\n\[\e[32m\](\[\e[0m\]\u\[\e[32m\])(\[\e[0m\]jobs:\j\[\e[32m\])(\[\e[0m\]\w\[\e[32m\])\$(parse_git_branch)\[\033[00m\]\n\[\e[0m\]\$ "
export PS1=$(strip_colors)
fi
unset color_prompt force_color_prompt
答案1
谢谢@Quasimodo 和@JdeBP!
我删除了该strip_colors()
函数并parse_git_branch()
用以下函数替换了该函数。从那以后我再也没有遇到过任何问题。
# get current branch in git repo, from ezprompt.net
function parse_git_branch() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]
then
STAT=`parse_git_dirty`
echo "[${BRANCH}${STAT}]"
else
echo ""
fi
}
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo " ${bits}"
else
echo ""
fi
}
# Use like:
# export PS1="\`parse_git_branch\` "