2014-01-18 33 views
0

在使用git時,我決定將.git-prompt.sh添加到我的.bashrc文件中。該文件顯示了我目前正在使用的分支的名稱。 我也決定添加一些顏色。終端顏色導致提示跳回到行的開頭

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 

使用任何終端(gnome-terminal,xterm,quake)都會導致同樣的問題。當輸入了一行變得很長,

enter image description here

迅速跳回行的開頭。這可能是因爲彩色文字的實際長度更長。

enter image description here

我如何得到終端的顏色沒有這個打嗝?

回答

1

我認爲你需要寫

#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\]" 

所以,你必須\[\]周圍所有的顏色指令。

+0

這解決了我的問題,因爲終端提示現在正確移動。 –