Cool, interesting, useful, unique and innovative Shell Prompts

by Oliver on Wednesday, September 19th, 2012.

At $employer today, we had our bi-weekly tech talk session and one of the lightning talks given was on tmux. Tmux is an excellent piece of software (although I gave up on it and started using iTerm2) but that’s not what I wanted to talk about.

One of the other participants in the session noticed the presenter’s shell prompt had a little smiley/frowney face which changed both expression and colour depending on the exit code of the last command – how cool is that? How many times have we all typed echo $? just to find out if our last command was really successful? It really makes sense to have this information displayed at all times.

So in that spirit I’m sharing my PS1 prompt variable with you. It’s not the most advanced, doesn’t use all of the bells and whistles and I’m still not entirely sure the information it presents is essential but it’s a work in progress. I’d love for you to share your own in the comments in the hope of spreading know-how and ideas!

export PS1="\`if [ \$? = 0 ]; then echo \e[33\;40m\\\^\\\_\\\^\e[0m; else echo \e[36\;40m\\\-\e[0m\\\_\e[36\;40m\\\-\e[0m; fi\` \[\033[38m\]\u \[\033[0;36m\]\j \[\033[1;32m\]\!\[\033[01;34m\] \w \[\033[31m\]\`ruby -e \"print (%x{git branch 2> /dev/null}.split(%q{\n}).grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`\[\033[37m\]$\[\033[00m\] "

Roughly in order, this equates to:

  1. Smiley/frowney face based on exit code of last command.
  2. Username
  3. Number of backgrounded jobs
  4. Shell history number
  5. CWD leaving home directory an unexpanded ~
  6. Git repository branch, using Ruby 1.8/1.9-compatible code

On my machine it looks like this:

What does yours look like?

Tags: ,

Wednesday, September 19th, 2012 Tech
  • Iterm2 apparently can work with tmux (haven’t tried this).

    I use PS1=’\h:\w\[33[32m\]$(__git_ps1) \[33[0m\]$ ‘

    What does the ruby stuff provide extra to what I get via the git bash completion using $(__git_ps1)?

    • Good question – absolutely nothing, but I had no idea that Git even shipped a set of bash completions, so this is entirely enlightening to me!

      For the benefit of others, it seems they live in /usr/share/git-core/git-completion.bash, so source that file in your .bashrc/.zshrc and follow the contained instructions for changing your prompt.

      They provide the following examples:

      #         Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
      #         ZSH:  PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '

      Thanks for that Jilles!

      • This does make me wonder though, if I haven’t been sourcing that completion file, what has been performing branch name completion for me up until now ?!?

  • Pingback: ThatBytes » Fun with Bash prompts()