Saturday, August 25, 2012

Bash prompt

After attending the Cool Git Tricks talk by Carina Zona at Women Who Code Lightning Talk this Tuesday, I decided to add git status to my bash prompt.

Here is how it looks like in action:

chiuki@moomin 09:50 ~/github/android-square-view master $

  • username@hostname: Basic info.
  • Time: I show time in my prompt so I can start a long-running job and go to lunch. The prompt will be displayed the moment the job returns, so I can subtract and see how long it took.
  • Current directory: Gives me some context.
  • git branch: I show it in blue when I am in master, purple otherwise.
  • git status: I show a red star if the current directory is dirty.
  • $: The color indicates if the last command was successful. Green for success, red for failure.

So if I'm on the release branch, has pending changes, and the last command failed, my prompt would look like this:

chiuki@moomin 10:05 ~/github/android-square-view release* $

What do you show on your shell prompt?


Inline coding questions will not be answsered. Instead, ask on StackOverflow and put the link in the comment.

  1. Pretty nice. I liked the status dolar, I'll replicate that in Zsh.

  2. haha, you know about the Moomins? I used to have a machine called snufkin, so not just trolls ^_^

  3. Nice! I hadn't even known about PROMPT_COMMAND.

    I use:

    1. Show username only if I'm someone other than me (like "root")
    2. Current directory
    3. Show hostname only if I'm sshed somewhere (omit if it's my local machine)
    4. Display a green :) for success or red :( for failure
    5. Current git branch
    6. Screen name if I'm inside "screen"

    However my code is a complete mess, because I try to set PS1 directly, with shell commands inside, all on one line, with things like PS1="\[\e[32m\]\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\[\e[0m\] $PS1"

    I'm going to rewrite my code to use prompt command. Thanks!!