Saturday, September 12, 2015

Highlighting bash prompt and adding GIT branch

I like colors in command line. I like any colors! :)
I would like to share my own setting for bash prompt. There is comments that's why you will understand how it works.

But before you start I'd suggest to try this fast way to make it.
You just need to run this online script:

$ curl -L | bash
$ . ~/.bashrc

That's how it will look like:

Or, you may make it by your own. :)

At the beginning we need to get script to be able to paste GIT branch name of current GIT directory.

curl -o ~/

Now we are ready to colorize out prompt and add GIT branch.

# -- Set up prompt --
# load GIT-Prompt scripts
. ~/

# show '>>' in RED to unsuccessful commands
PS1=${PS1}'\[`[ $? = 0 ] && F=2 || F=1; tput setaf $F`\]>>'
# show username and hostname, show in RED for ROOT user
PS1=${PS1}'\[`[ \u = 'root' ] && X=1 || X=2; tput setaf $X; echo $F`\]\u\[`tput setaf 2`\]@\h\[`tput sgr0`\]:'
# show current path
PS1=${PS1}'\[`tput setaf 3`\]$PWD\[`tput sgr0`\]'
# add GIT branch
PS1=${PS1}'\[`tput setaf 6`\]'"\$(__git_ps1)"'\[`tput sgr0`\]'
# tail, make command line on the next line and symbol $
PS1=${PS1}'\n\$ '

export PS1 # Share PS1 for sudo mode

For now, we need to load also for root user because it will try to load GIT branchAdd new line 
    . /home/vagrant/ 
into /root/.bashrc.

$ sudo nano /root/.bashrc

>>vagrant@redbox:/web/ (develop)
$ badcommand
-bash: badcommand: command not found
>>vagrant@redbox:/web/ (develop)

In sudo -s mode:
>>root@redbox:/web/ (develop)
# badcommand
-bash: badcommand: command not found
>>root@redbox:/web/ (develop)

I have vagrant user. You may have different name that's why you will have different path like /home/vagrant.


My own on-fly script:

Possibly you will be interested in to consider more dynamic GIT prompt scripts by link:

How to: Change / Setup bash custom prompt (PS1)
Example on stackoverflow
Put Your Git Branch in Your Bash Prompt
Post a Comment