문제

While using git, I decided to add .git-prompt.sh to my .bashrc file. This file shows me the name of the branch that I am currently working on. I also decided to add some color.

if [ -f ~/.git-prompt.sh ]; then

    #include the branch detection file
    source ~/.git-prompt.sh

    #add the current branch in yellow
    export PS1='$(__git_ps1 "[\e[38;5;220m%s\x1b[0m]")'

    #make the rest of the text in the prompt gray
    PS1+="\e[38;5;245m\w$ "

    #set colors back to default
    PS1+="\033[0m"
fi

Using any terminal(gnome-terminal, xterm, quake) results in the same problem. When the typed out line gets too long,

enter image description here

the prompt jumps back to the beginning of the line. This is perhaps because the actual length of the colored text is much longer.

enter image description here

How do I get terminal colors without this hiccup?

도움이 되었습니까?

해결책

I think you need to write

#add the current branch in yellow
export PS1='$(__git_ps1 "[\[\e[38;5;220m\]%s\[\x1b[0m\]]")'

#make the rest of the text in the prompt gray
PS1+="\[\e[38;5;245m\]\w$ "

#set colors back to default
PS1+="\[\033[0m\]"

So you have \[ and \] surrounding all the colour directives.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top