1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

A Bash Prompt for Ruby, Ruby on Rails and Git

By

A Bash Prompt for Ruby, Ruby on Rails and Git

When working with Ruby, Ruby on Rails and Git, you're going to be throwing a lot of Bash commands around. In all of these cases, there are commands that can be run that will give more information about these programs. For example, which version of Ruby and Rails you're using, which gemset you're on if you're using RVM, and which git branch you're currently on. In this article, I'll walk you through putting these right on your bash prompt so you'll have instant access to all of them.

This article is, sadly, not about Ruby. I say "sadly" because Ruby is so elegant, and Bash is... not. I don't mean to bash Bash, but really, it's archaic and quirky. If you follow these directions and don't know anything about Bash programming, it might be best to stick to copy and paste. This can be done in Ruby, but since the latency of the Bash prompt is important (it's very annoying if your Bash prompt takes a second to come up after to you issue every command), I've not used Ruby.

So, assuming you know nothing about Bash, all you really need to know is there is an environment variable called PS1, which defines what your prompt looks like. On a default system, it might display your username and hostname, or the directory you're in, and probably the $ character. We're going to use this to display all our information.

Another thing to consider is color. No one likes staring at a wall of text, and color coding your information in your Bash prompt makes it that much easier. We'll be using some very cryptic things called ANSI color codes to change the color of the text. Current directory in red, Ruby version in blue, Rails version in grey, git branch in orange. You might want to change these colors, or eliminate them depending on your preference and the color of your terminal window.

So, on to the nitty gritty. First, locate your Bash startup file. On OS X, this is located in ~/.bash_login, on other systems I've seen it located in ~/.bash_profile or ~/.bashrc. Refer to your OS documentation or use experimentation to find the correct file, there is no standard location and every OS seems to put it in a different place. Open this up in a text editor and take a look. The file might be empty, don't panic. Save the following line into the file and open a new terminal window to see the results.


export PS1="testing \$ "

This should have changed your Bash prompt on the command line to testing $ , that's good. You're ready to copy and paste in the whole thing. So delete your PS1 line, and copy and paste the following into the text editor.


function ruby_version() {
  rvm current
}

function git_branch() {
  branch=$(git branch 2>/dev/null | cut -d' ' -f2)
  if [ "$?" == "0" ]; then echo $branch; fi
}

function rails_version() {
  rails=$(rails --version | grep '^Rails [0-9]')
  if [ "$?" == "0" ]; then
    echo $rails
  else
    echo "No rails"
  fi
}

export PS1="\[\e[0;31m\]\W \[\e[0;34m\]\$(ruby_version) \[\e[0;23m\]\$(rails_version) \[\e[0;33m\]\$(git_branch)\[\e[0m\]\n\$ "
export PS2="\$ "

OK, this is really a mouthful. The three functions at the top keep the actual PS1 line a bit shorter (though nowhere near readable, sadly). It also makes it so you can reuse these in your bash scripts and prompts. The ruby_version function simply uses rvm version to get the Ruby version. If you're using a gemset, this will also print the gemset, a two for one deal. The git_branch function does a bit of simple logic to tell if you're actually in a git repository, and if you're not it won't print anything, otherwise it will print the git branch. And finally the rails_version function does a little regular expression match to check that Rails actually returned a version. OS X comes with a stub executable that has similar output to rails --version, so this step checks for things like that.

Put all this into your PS1 variable along with the ANSI color codes and voila, your prompt now looks like this:


test_project ruby-1.9.3-p194@rails Rails 3.2.6 master
$ 

This is like magic, and a huge time-saver. There's a lot you can do with this, but remember that everything you put in here gets run every time it displays the prompt, so things that take too long will slow your prompt down. I've had to take the Rails prompt out on one machine, as it takes too long, but is fine on other machines. So experiment, and customize your prompt to fit your needs.

  1. About.com
  2. Technology
  3. Ruby
  4. Ruby on Rails
  5. Ruby on Rails 3 Tutorials
  6. A Bash Prompt for Ruby, Ruby on Rails and Git

©2014 About.com. All rights reserved.