Skip to content

Instantly share code, notes, and snippets.

@nicksieger
Created August 24, 2010 20:25
Show Gist options
  • Save nicksieger/548242 to your computer and use it in GitHub Desktop.
Save nicksieger/548242 to your computer and use it in GitHub Desktop.

Nick's bash prompt code

Example

[13:14:19][~/Projects/ruby/redmine (master) {jruby-head@redmine}]
$ false
[exited with 1]

Features

  • Current time, host, shortened $PWD on one line

  • Show hash (#) for prompt and root@host when root

  • Show user@host when $SSH_TTY is set (remote ssh sessions)

  • Show git branch

  • Show RVM info (Ruby, version, current gemset)

  • Show shortened $PWD in terminal window title as well as prompt

  • Print any non-zero exit status after the command has finished

  • Growl support: growl when a long-running command is done

    $ growldone "Finished" && long_running_command
    

Install

  • Ensure you have git-completion.bash installed and sourced.

    source ~/.bashrc.d/git-completion.bash
    
  • Put prompt.bash somewhere in your home directory and source it.

    source ~/.bashrc.d/prompt.bash
    
#!/bin/bash
#
# Nick Sieger's Bash Prompt
short_pwd ()
{
local pwd_length=${PROMPT_LEN-40}
local cur_pwd=$(echo $(pwd) | sed -e "s,^$HOME,~,")
if [ $(echo -n $cur_pwd | wc -c | tr -d " ") -gt $pwd_length ]; then
echo "...$(echo $cur_pwd | sed -e "s/.*\(.\{$pwd_length\}\)/\1/")"
else
echo $cur_pwd
fi
}
export TITLE_BASE='${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}'
export TITLE="Bash $BASH_VERSION"
title()
{
local t="$*"
if [ "$t" ]; then
TITLE="$t"
fi
t="$TITLE -- [$(eval echo $TITLE_BASE)]"
case $TERM in
xterm*)
echo -ne "\033]0;$t\007"
;;
screen)
echo -ne "\033_$t\033\\"
;;
*)
if [ "$TERM_PROGRAM" = iTerm.app ]; then
osascript <<EOF
tell application "iTerm"
tell the current terminal
tell the current session
set name to "$t"
end tell
end tell
end tell
EOF
fi
esac
}
# this prints the value of any non-zero exit status after each
# command.
_prompt_command ()
{
laststatus=$?
# Disable PROMPT_COMMAND if PATH becomes empty, to avoid errors
if [ "$PATH" = "" ]; then
PROMPT_COMMAND=
else
if [ $laststatus != 0 ]; then
echo [exited with $laststatus]
fi
git=
if type git >/dev/null; then
git=$(__git_ps1)
fi
rvm=
if [ -f ~/.rvm/bin/rvm-prompt ]; then
rvm=$(~/.rvm/bin/rvm-prompt i v p g)
if [ "$rvm" -a "$rvm" != system ]; then
rvm=" {$rvm}"
else
rvm=
fi
fi
# Set the shortPWD environment var so we can use it in our prompt
# `pwd_length' controls the maximum length of $PWD
shortPWD=$(short_pwd)$git$rvm
fi
title
}
prompt()
{
local cyan='\[\033[1;36m\]'
local white='\[\033[0;1m\]'
local nocolor='\[\033[0m\]'
if ! perl -e "'$TERM' =~ /(cygwin|ansi|linux|xterm|rxvt)/ or exit 1"; then
cyan=""
white=""
nocolor=""
fi
if [ "$SSH_TTY" -o "$USER" = root ]; then
# Root or remote system, display host and full path
PS1="$cyan[$white\t$cyan][$white\u@\h:\${shortPWD}$cyan]$nocolor\n$cyan$nocolor"'\$ '
else
# Skip user@host when on local system; it will be in the title bar
PS1="$cyan[$white\t$cyan][$white\${shortPWD}$cyan]$nocolor\n$cyan$nocolor"'\$ '
fi
}
_growl_prompt_command()
{
eval $PREV_PROMPT_COMMAND
growlnotify -n Shell -m "$PROMPT_MEMO exited with $laststatus" Shell
PROMPT_COMMAND=$PREV_PROMPT_COMMAND
PREV_PROMPT_COMMAND=
PROMPT_MEMO=
}
growldone()
{
PREV_PROMPT_COMMAND=$PROMPT_COMMAND
PROMPT_COMMAND=_growl_prompt_command
if [ $# -gt 0 ]; then
PROMPT_MEMO=$@
else
PROMPT_MEMO=Command
fi
}
# Set up $PS1 and $PROMPT_COMMAND
prompt
export PROMPT_COMMAND=_prompt_command
@jabley
Copy link

jabley commented Aug 25, 2010

Adding this at the end

export PROMPT_COMMAND=_prompt_command

seems to fix it for me?

[12:34:05][~ {rbx-1.0.1-20100603}]
$ pushd ~/work/xcode/bbc-iphone-news/
~/work/xcode/bbc-iphone-news ~
[12:34:20][~/work/xcode/bbc-iphone-news (develop) {rbx-1.0.1-20100603}]
$ false
[exited with 1]
[12:34:25][~/work/xcode/bbc-iphone-news (develop) {rbx-1.0.1-20100603}]
$ 

@nicksieger
Copy link
Author

Ah yes. I must be setting PROMPT_COMMAND somewhere else in my .bashrc. I'll update the code here for posterity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment