Append git's branch name to command prompt

后端 未结 2 1579
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 11:32

I wanted to use one of the Git-completion.bash features but I can\'t customize the look I\'d like to have. Here is relevant part of my .bash_profile:

source ~/.g         


        
相关标签:
2条回答
  • 2021-01-30 12:08

    Not sure, but vcprompt might solve it better for you?

    0 讨论(0)
  • 2021-01-30 12:29

    The trick to get the quoting right is to have eveything double-quoted, except for $(__git_ps1 "(%s)"), which is single-quoted.

    source ~/.git-completion.bash
    function prompt
    {
    local WHITE="\[\033[1;37m\]"
    local GREEN="\[\033[0;32m\]"
    local CYAN="\[\033[0;36m\]"
    local GRAY="\[\033[0;37m\]"
    local BLUE="\[\033[0;34m\]"
    export PS1="
    ${GREEN}\u${CYAN}@${BLUE}\h ${CYAN}\w"' $(__git_ps1 "(%s)") '"${GRAY}"
    }
    prompt
    

    An alternative solution is to replace $( with \$( in the code in the question.

    Background information: Two substitutions take place: first at export PS1="..." time, and later when the prompt is displayed. You want to execute __git_ps1 each time the prompt is displayed, so you have to make sure that the first substitution keeps $(...) intact. So you write either '$(...)' or "\$(...)". These are the two basic ideas behind the solutions I've proposed.

    0 讨论(0)
提交回复
热议问题