Color Coded Command Line

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
miche's picture

Last night while I was demoing, someone mentioned the colors I had on the command line and how it also denoted which GIT branch I was in. You can thanks James Sansbury (q0rban) for the code.

My instructions are biased towards MAC users. Sorry.

You need Git already installed for this to work.

In your terminal... //denotes my comments

//Find where your bash file is
ls ~/.bash_profile

//This returns "/Users/miche/.bash_profile" for me
//change directory (cd) to that path
cd /Users/miche

//Make sure you are there by "print working directory"
pwd

//Open the file in a commandline editor, like Nano, so we can edit it and add more stuff
nano .bash_profile

//If you are using Nano, arrow down to below the stuff that is in it and paste in:
//(If you aren't using nano, you are on your own for editing that file)
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1='\[\033[1;32m\]\h\[\033[0;33m\] \w\[\033[1;32m\]$(parse_git_branch)\[\033[00m\]: '

//For Nano, Save the file and answer yes to 'do you want to overwrite'
CTRL-O

//For Nano, Exit
CTRL-X

//Now CD into a folder that is under git source control and smile

Comments

There is some syntax errors

Perignon's picture

There is some syntax errors that happened when you posted this. Can you gist it?

Sorry, Markdown filter was

miche's picture

Sorry,
Markdown filter was enabled for the body so any brackets got escaped. I edited the node so it should be okay now.


Michelle Lauer
michellelauer.com

That works! Testing it now.

Perignon's picture

That works! Testing it now.

Awesome Thank you. I'd tried

rggoode's picture

Awesome Thank you.

I'd tried it myself and ran into the same problem, but didn't know how to makes sense of it to describe properly.

Works now. Super cool.

Roger

_________________

Art has gone to the dogs
GoodeGallery.com

Short path

DamienMcKenna's picture

If you'd prefer to have it show the bare current directory name instead of the full path in the output, change the "export" line to have \W instead of \w.

I just uploaded how to do

Perignon's picture

I just uploaded how to do this on Linux. This can also be done on Mac too. I found the git-completion.bash on my Mac as well.

https://gist.github.com/1803405

Color chart

j2parker's picture

If you insert the color chart into .bash_profile or .bashrc you can then assign color by name instead of by code.

# Color Chart

black='\e[0;30m'
darkgray='\e[1;30m'
red='\e[0;31m'
lightred='\e[1;31m'
green='\e[0;32m'
lightgreen='\e[1;32m'
yellow='\e[0;33m'
blue='\e[0;34m'
lightblue='\e[1;34m'
purple='\e[0;35m'
cyan='\e[0;36m'
lightcyan='\e[1;36m'
white='\e[0;37m'
default="\e[0m"

Miche's PS1 then becomes:

PS1="$lightgreen\h$yellow \w$lightgreen$(parse_git_branch)$default: "

The PS1 I use is:

PS1="\`if $? = "0" ; then echo '$green'; else echo '$red'; fi\`[\!] $yellow\u$lightcyan...$white\H $cyan(\@)$white: $lightcyan\w $white\$:\n$default"

The if statement will print the command number (the same number that appears in bash history) which will be colored green if it completed successfully or red if it failed.
**Credit cannot be assigned as I found that if statement somewhere on the web.

New Hampshire

Group categories

Regional Audience

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week