Making Your Bash Prompt git-aware and Other Fun Stuff

November 3, 2015

This post covers how to use a plugin to stylize a bash prompt with git-specific information. Once you see how this is done, it will be easy to add many more advanced styles and useful information to your bash prompt.

Let's start with a simple bash prompt. This is stored in a variable called PS1 and can be set in your .bash_profile file in your home directory so that it is loaded any time you open a shell.

export PS1="\u@\h \w$ "

Bash interprets several substitution variables, escaped with \. The above will give a prompt that shows your username (\u), hostname (\h) and current working directory (\w). Any other characters, such as the $ and space will be interpreted literally and printed on the command line. Once loaded it should look something like this:

Simple Bash Prompt

By the end of this post your bash prompt may look something more like this:

Advanced Bash Prompt

The first step is making your bash prompt git aware. There are several projects that help do this but I chose to use jimeh/git-aware-prompt.

The installation instructions are simple. Taken from the repo readme, clone the project to a .bash folder in your home directory:

mkdir ~/.bash
cd ~/.bash
git clone git://github.com/jimeh/git-aware-prompt.git

Edit your ~/.bash_profile or ~/.profile and add the following to the top:

export GITAWAREPROMPT=~/.bash/git-aware-prompt
source "${GITAWAREPROMPT}/main.sh"

Sourcing this script will create a PROMPT_COMMAND variable which contains some code that gets executed as a regular Bash command just before Bash displays a prompt. This code defines git_branch and git_dirty variables to customize the PS1 display. The script also creates some color variables that can be used in the PS1 display variable.

The only thing left to do is to set your PS1 variable to use the newly created git aware prompt. Once again, edit your ~/.bash_profile to contain something that takes advantage of the git aware and color variables:

export PS1="\u@\h \w \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "

But let's not stop there! On my mac, my iTerm2 allows the input and display of emojis (as of this writing I'm using version 2.1.4). Open your ~/.bash_profile for editing again, but this time in iTerm2 (vi, ecmacs, or nano editors all work well). Position the cursor at the beginning of your PS1 variable definition and choose the iTerm2 menu option [Edit] > [Emoji & Symbols]. Find the emoji you want and just click on it to be added at the position of your cursor. You should now have a prompt that looks something like this:

Emoji Prompt

The last thing we'll do to customize your Bash prompt is add two different emojis that indicate your last command's exit status. If the last command exited properly we want to display a nice emoji, and if the last command had a non-zero status we want to display a not nice emoji.

A convenient place to add the logic for this is back in the git-aware-prompt main script file, ~/.bash/git-aware-prompt/prompt.sh. Open this file up in some editor in iTerm2, and add the following function definition:

exit_status() {
    if [[ $? != 0 ]]; then
      exit_status=""
    else
      exit_status=""
    fi
}

As before, position the cursor within the double quotes "" for each exit status and pick an emoji for non-zero exit statuses and zero exit statuses (open the [Emoji & Symbols] menu option and click an emoji, as before).

Now add this function call to the PROMPT_COMMAND variable by editing a few lines below:

PROMPT_COMMAND="exit_status; find_git_branch; find_git_dirty; $PROMPT_COMMAND"

This gives a new exit_status variable to use in your PS1. You can put this immediately before the emoji you just added:

export PS1="\$exit_status 🌵 \u@\h \w \[\e[0;36m\]\$git_branch\[\e[0;31m\]\$git_dirty\[\e[0m\]$ "

Save this file, open a new terminal window and enjoy your new advanced Bash prompt!

Advanced Emoji Prompt

 

comments powered by Disqus

About This Site

This site was designed by We Are How.

This site is powered by Sculpin static site generator and the source is available here.

Yotta = 10^24, or 1 000 000 000 000 000 000 000 000, the largest metric prefix.


Contact

Get in touch to find out how we can help you refine your vision and implement a dynamite product that will help your business grow. Our agile product development process is thoughtfully designed to give clients ongoing feedback and visibility from project inception to completion.