Git prompts
Rather than roll your own here I'd recommend using what's provided by the Git project out of the box.
On a CentOS 7.x box if you yum install git
everything is already included with Git to do both commandline completion + the git prompt.
$ rpm -ql git|grep completion
/etc/bash_completion.d
/etc/bash_completion.d/git
/usr/share/doc/git-1.8.3.1/contrib/completion
/usr/share/doc/git-1.8.3.1/contrib/completion/git-completion.bash
/usr/share/doc/git-1.8.3.1/contrib/completion/git-completion.tcsh
/usr/share/doc/git-1.8.3.1/contrib/completion/git-completion.zsh
/usr/share/doc/git-1.8.3.1/contrib/completion/git-prompt.sh
/usr/share/git-core/contrib/completion
/usr/share/git-core/contrib/completion/git-completion.tcsh
/usr/share/git-core/contrib/completion/git-prompt.sh
Here's the steps to do this manually, they'd need to be added your ~/.bash_profile
to make them permanent.
$ . /usr/share/git-core/contrib/completion/git-prompt.sh
$ mkdir somedir && cd somedir
$ git init
$ PS1="$GREEN\t$RED-$BLUE\u$YELLOW\w\[\033[m\]$MAGENTA\$(__git_ps1)$WHITE \$ "
Doing the above will set your prompt so that it goes from this:
[root@centos7 somedir]#
-to-
13:18:42-root/home/vagrant/somedir (master) $
NOTE: it's now displaying the branch that I'm currently on in this repo, in my prompt.
Where's the colors?
To get this colored you need to define the colors so that the variable PS1
for the prompt's color variables have corresponding escape codes for their respective colors:
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
MAGENTA="\[\033[0;35m\]"
WHITE="\[\033[0;37m\]"
Now reset the PS1
again to pick these up:
13:24:26-root/home/vagrant/somedir (master) $ PS1="$GREEN\t$RED-$BLUE\u$YELLOW\w\[\033[m\]$MAGENTA\$(__git_ps1)$WHITE \$ "
13:24:35-root/home/vagrant/somedir (master) $
And it'll look like this now:
And what about changing the color of ls
?
If is probably something I'd discourage your from because you need to then run a git status
against every directory in each ls
command to determine if it's a Git repo or not.
Imagine this:
$ ls somedir
dir1
dir2
dir3
dir4
dir5
dir6
dir7
would result in 7 git status
commands to determine which directory is a Git repo and which is not, so that you can decide whether to color it as purple or not.