سؤال

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