문제

Here is my current PS1:

$ echo $PS1
\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$

I have installed git bash completion and it isn't showing the current branch in my command prompt. I think this needs to be edited but I've got no idea what to change to make it show the current branch. Note that I like the colors and general structure (i.e. with $ on its own line), so I'd like to keep that if possible.

도움이 되었습니까?

해결책

Modify the prompt string and add $(__git_ps1 " (%s)") somewhere.

For example, try typing this into Bash:

export PS1="\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]$(__git_ps1 ' (%s)')\n\$"

Once you find something you like, add that line to one of Bash's startup files, e.g. $HOME/.bashrc.

The source for git-prompt.sh is well-commented and worth browsing if you're curious about other options.

Note that you may have to manually source path/to/git-prompt.sh before this will work. If so, add this line to your config file as well, above the export PS1 bit.

다른 팁

My two cents. This has been my absurdly complex Bash prompt for years, that also integrates git-prompt.sh. My smarter friend :) wrote most of it!

export HOSTNAME_FQDN=$(hostname)
#export GIT_PS1_SHOWDIRTYSTATE=true
#export GIT_PS1_SHOWSTASHSTATE=true
#export GIT_PS1_SHOWUNTRACKEDFILES=true
#export GIT_PS1_SHOWUNTRACKEDFILES=true
#export GIT_PS1_SHOWUPSTREAM=verbose
#export GIT_PS1_DESCRIBE_STYLE=default
#export GIT_PS1_SHOWCOLORHINTS=true
source /usr/share/git-core/contrib/completion/git-prompt.sh

__set_prompt()
{
    local exit_code="$?"
    local git_ps1="$(__git_ps1 "%s")"
    # Update XTerm window title: "user@hostname: pwd"
    PS1="\[\e]0;\u@$HOSTNAME_FQDN: \w\a\]"
    # Visible prompt
    if [ ! -z "$HOST_DESC" ]
    then
        # Newline
        PS1+="\n"
        # Bright cyan color (1=bright; 36=cyan)
        PS1+="\[\e[1;36m\]"
        PS1+="$HOST_DESC"
    fi
    # Newline
    PS1+="\n"
    if [ $exit_code != 0 ]
    then
        # Bright red color (1=bright; 31=red)
        PS1+="\[\e[1;31m\]"
        # "[$?=x]"
        PS1+="[\\\$?=$exit_code] "
    fi
    # Bright green color (1=bright; 32=green)
    PS1+="\[\e[1;32m\]"
    # "user@hostname"
    PS1+="\u@$HOSTNAME_FQDN"
    if [ ! -z "$git_ps1" ]
        then
        # Newline
        #PS1+="\n"
        # Purple color (0=not bright; 37=white)
        PS1+="\[\e[0;35m\]"
        PS1+=" (Git: $git_ps1)"
    fi
    # Bright cyan color (1=bright; 36=cyan)
    PS1+="\[\e[1;36m\]"
    # " date"
    # Example: " Fri 2013-11-08 22:20:36 HKT+08:00"
    PS1+=" [$(date '+%a %Y-%m-%d %H:%M:%S %Z%z')]"
    # Newline
    PS1+="\n"
    # Yellow color (1=bright; 33=yellow)
    PS1+="\[\e[1;33m\]"
    # pwd
    PS1+="\w"
    # Reset color
    PS1+="\[\e[0m\]"
    # Newline
    PS1+="\n"
    # "$ "
    PS1+="\$ "
}

export PROMPT_COMMAND=__set_prompt

Sample (multi-line) prompt:

username@hostname (Git: git-branch-name) [Fri 2021-09-03 16:06:00 CST+0800]
/current/path
$
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top