Setting up your terminal to be awesome

Some of the terminal defaults aren't all that great or helpful. There's a few things we can do to make working in the terminal a lot more bearable.

Change the prompt

The default prompt includes a pretty hidden display of your current folder, as well as a bunch of relatively useless information (My computer name? Why??). Changing the default is pretty easy to do.

  • From the terminal, open ~/.bash_profile in an editor of your choice (Vi or Nano are pretty straight forward and let you do it right from the terminal itself).
vi ~/.bash_profile . # If this file didn't already exist, this will create it  
  • Inside the file that appears, hit i to go into insert mode. (If using Nano instead, no need to do this)
  • Paste in the following line anywhere in the file:
export PS1="\w $ "  
  • If you used Vi:
    • Hit escape (to exit insert mode)
    • Hold Shift and hit "Z" twice (shortcut to save and quit)
  • If you used Nano
    • Hit control + o (saves)
    • Hit enter (sets the filename)
    • Hit control + x (quits nano)

Lastly, we need our computer to re-read that .bash_profile file, so type:

source ~/.bash_profile  

There are a lot of other options when it comes to customizing your bash prompt if you want. This is an excellent site for customizing it to display exactly what you want.

Add Git tab-completion and visible branch name in the prompt

It can be hard to remember which branch you're working on with Git. It's extremely helpful to always see, front-and-center, which branch you're on. It's also really handy to be able to use tab completion on all your git commands. Adding this is pretty easy to do:

  • From anywhere in terminal, run:
curl -o ~/.git-completion.bash  
  • Open ~/.bash_profile in an editor
  • Add the following to .bash_profile:
# Git tab completion
if [ -f ~/.git-completion.bash ]; then  
  . ~/.git-completion.bash

# Git branch in prompt
parse_git_branch() {  
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'

export PS1="\w\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "  
  • Run source ~/.bash_profile once again

Note - if you already did the first part in changing your terminal prompt, you'll want to replace the old line (my example was export PS1="\w $ ") with the new line at the end of the segment above - export PS1="\w\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ ". If you leave both in, the final result will be whichever comes last in .bash_profile.