Reputation: 19552
I have updated my bash file so that the current Git branch is displayed followed the instructions here. What I ended up using is
PS1="\u@\h \W \$(__git_ps1)\$ "
However...
(branch)
to [branch]
, i.e. show the branch name within brackets instead of parentheses. The original version had colours:
PS1="\[$GREEN\]\t\[$RED\]-\[$BLUE\]\u\[$YELLOW\]\[$YELLOW\]\w\[\033[m\]\[$MAGENTA\]\$(__git_ps1)\[$WHITE\]\$ "
but when I used this I did not see any colours. How could I see colours and what would be a standard setting for the shell to use?
Upvotes: 1
Views: 3284
Reputation: 66214
Here is a relevant passage of the .git-prompt.sh
file (which, in modern Git versions, contains the definition of the __git_ps1
function):
# 3a) Change your PS1 to call __git_ps1 as
# command-substitution:
# Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
# ZSH: setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
# the optional argument will be used as format string.
__git_ps1
accepts an optional argument that you can use to customize the format of the string. In your case, you should use
PS1="\u@\h \W \$(__git_ps1 '[%s]')\$ "
You can use colors as in the code in your question, but you need to make sure that the variables in question are defined. Put the following lines somewhere in your ~/.bashrc
file:
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
WHITE=$(tput setaf 7)
RESET=$(tput setaf 0)
After sourcing your ~/.bashrc
file, you'll be able to use those colors.
For instance, here is a simplified version of your prompt with the current branch name (and surrounding brackets) highlighted in red:
PS1="\W \[$RED\$(__git_ps1 '[%s]')\]\[$RESET\$\] "
Upvotes: 4
Reputation: 95242
The __git_ps1
function you're using takes a format string as an argument. So you can pass in whatever you want with %s
where you want the branch name to show up. For example:
PS1="\u@\h \W \$(__git_ps1 '[%s]')\$
No clue about the colors, sorry.
Upvotes: 1
Reputation: 6667
This works:
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}
PS1="\[$Blue\][\[$Cyan\]\d\[$Blue\]] " # Display date
PS1=$PS1"\[$Yellow\]\@:" # Display time
PS1=$PS1"\[$BGreen\]\w" # Display a green pwd
PS1=$PS1"\[$BCyan\]"'$(parse_git_branch)' # Display a cyan git-branch
PS1=$PS1"\[$Color_Off\]$ " # Turn off color and end prompt
export PS1=$PS1
it produces something like this:
[Thu Sep 18] 12:44 PM:~/repos/test[master*]$
Upvotes: 0