Bash

Update: You can now find all of my .dotfiles on GitHub.


 

Find below my “.bash_profile" file in OS X Lion and the referenced “.bash_ps1" (slightly modified from lifehacker).

Detailed walkthrough

  • add some additional directories to PATH
  • reference .bash_ps1 (see the file below)
  • set grep and ls to display some fancy colors
  • two aliases to often used commands
    • order top by CPU usage
    • display more human readable directory listing output
  • Set Textmate’s “mate” command as the default editor
  • Ignore duplicates in history. E. g. if I issue “ls” multiple times, it only occurs once when I press the cursor up key, to cycle through my last commands.
  • Export DISPLAY environment variable if it’s not already set. This helps with X-forwarding from remote systems.
  • SSH host completion lets me type the first few characters of any host I already connected to successfully and completes it just like normal shell commands or files after pressing TAB.
  • The “myip” function lets me look up my external IP.
  • “git_stats” spits out detailed infos about any git repository you issue this command in.

.bash_profile

export PATH=$PATH:~/bin:/usr/local/bin:/usr/local/sbin:/usr/local/git/bin

# set fancy prompt
if [ -f "$HOME/.bash_ps1" ]; then
    . "$HOME/.bash_ps1"
fi

# some settings to be more colorful
export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
export CLICOLOR=true
export LSCOLORS=ExGxFxdxCxDxDxBxBxExEx

# handy aliases
alias top='top -o cpu'
alias ll='ls -lh'

# use Textmate as default editor
export EDITOR="mate"

# no duplicates in bash history
export HISTCONTROL=ignoredups

# export DISPLAY if it's not set yet
[[ -z $DISPLAY ]] && export DISPLAY=":0.0"

# ssh host tab completion
complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" ssh

################################################
# bash functions
################################################
function myip {
  res=$(curl -s checkip.dyndns.org | grep -Eo '[0-9\.]+')
  echo "$res"
}

function git_stats {
# awesome work from https://github.com/esc/git-stats
# including some modifications
if [ -n "$(git symbolic-ref HEAD 2> /dev/null)" ]; then
    echo "Number of commits per author:"
    git --no-pager shortlog -sn --all
    AUTHORS=$( git shortlog -sn --all | cut -f2 | cut -f1 -d' ')
    LOGOPTS=""
    if [ "$1" == '-w' ]; then
        LOGOPTS="$LOGOPTS -w"
        shift
    fi
    if [ "$1" == '-M' ]; then
        LOGOPTS="$LOGOPTS -M"
        shift
    fi
    if [ "$1" == '-C' ]; then
        LOGOPTS="$LOGOPTS -C --find-copies-harder"
        shift
    fi
    for a in $AUTHORS
    do
        echo '-------------------'
        echo "Statistics for: $a"
        echo -n "Number of files changed: "
        git log $LOGOPTS --all --numstat --format="%n" --author=$a | cut -f3 | sort -iu | wc -l
        echo -n "Number of lines added: "
        git log $LOGOPTS --all --numstat --format="%n" --author=$a | cut -f1 | awk '{s+=$1} END {print s}'
        echo -n "Number of lines deleted: "
        git log $LOGOPTS --all --numstat --format="%n" --author=$a | cut -f2 | awk '{s+=$1} END {print s}'
        echo -n "Number of merges: "
        git log $LOGOPTS --all --merges --author=$a | grep -c '^commit'
    done
else
    echo "you're currently not in a git repository"
fi
}

.bash_ps

############################################
# Modified from emilis bash prompt script
# from https://github.com/emilis/emilis-config/blob/master/.bash_ps1
###########################################
fill="--- "
reset_style='\[\033[00m\]'
status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
prompt_style=$reset_style
command_style=$reset_style'\[\033[1;29m\]' # bold black
PS1="$status_style"'$fill \t\n'"$prompt_style"'${debian_chroot:+($debian_chroot)}\[\033[0;32m\]::>'"$command_style "
# this is invoked every time before a command is executed
trap 'echo -ne "\033[00m"' DEBUG

function prompt_command {
    let fillsize=${COLUMNS}-9
    fill=""
    while [ "$fillsize" -gt "0" ]
        do
            fill="-${fill}" # fill with underscores to work on
            let fillsize=${fillsize}-1
        done
}
export PROMPT_COMMAND=prompt_command