How to hack Bash config to colorize your boring bash prompt
Published on by nick
As most developers, I live in the terminal. And despite the fact that the authors of the .bashrc
default file apparently discourage coloring your bash prompt, I wholeheartedly love it and recommend others do it too. Here's the snippet from the version of .bashrc
shipped with Ubuntu:
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
So I set out to figure out the best way to do this. It wasn't too bad. If you do some Google searches, you can find a decent amount of information on how to color your virtual environment. This is good... And you can find quite a bit on how to colorize your Git branches. And even a little bit on Mercurial branches, too. But not much on how to do both. This is where this article is unique.
This article is broken into two pieces. The first, how to set up colorizing the virtual environment indication; the second, how to dynamically color both Git and Mercurial branches, when in these directories.
The first thing I did was set up some bash variables for colors. If you have ever just even changed your bash prompt even a little bit, you have seen that the PS1 envvar
is ugly as hell. To make it much more readable, let's add this to a file called .bash_prompt
in your home
directory on Linux:
# Codes to color our prompt RED="\[\033[0;31m\]" YELLOW="\[\033[1;33m\]" GREEN="\[\033[0;32m\]" BLUE="\[\033[1;34m\]" PURPLE="\[\033[1;35m\]" LIGHT_RED="\[\033[1;31m\]" LIGHT_GREEN="\[\033[1;32m\]" WHITE="\[\033[1;37m\]" LIGHT_GRAY="\[\033[0;37m\]" COLOR_NONE="\[\e[0m\]"
The next thing to do is set up the function for determining the active Python virtual environment. This is pretty straight forward, and Stack Overflow is a great place to start. Thanks @dagarre.
# Determine active Python virtualenv details. function set_virtualenv () { if test -z "$VIRTUAL_ENV" ; then PYTHON_VIRTUALENV="" else PYTHON_VIRTUALENV="${RED}(`basename \"$VIRTUAL_ENV\"`)${COLOR_NONE} " fi }
Before coming back to how to implement this, let's check out the functions to figure out the Git and Mercurial branches:
hg_branch() { hg branch 2> /dev/null | awk '{print "hg["$1"] "}' } git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/git[\1] /' }
These are modified from several references. I had to tweak what I found quite a bit to make this work. I think I might be in the minority of folks who regularly uses both Git and Mercurial? I hope not! My day job is a Java shop that uses Mercurial, but in the open source community and for personal or side jobs, I like Git. So who knows...
The last piece to the puzzle is the function that brings it all together.
# Set the full bash prompt function set_bash_prompt () { # Set the PYTHON_VIRTUALENV variable. set_virtualenv PS1="${PYTHON_VIRTUALENV}${debian_chroot:+($debian_chroot)}${WHITE}\u${YELLOW}@${PURPLE}\h\[\033[00m\]:${BLUE}\w\[\033[00m\] ${GREEN}$(git_branch)$(hg_branch)${COLOR_NONE}$ " } # Execute this function before displaying prompt PROMPT_COMMAND=set_bash_prompt
Here is what this ends up looking like:
Drop me a line...
[email protected]
Follow me on Twitter...
@nicorellius
Share on
Comments
Comments powered by Disqus