Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
261 lines (224 sloc) 7.34 KB
# /etc/skel/.bashrc
#
# This file is sourced by all *interactive* bash shells on startup,
# including some apparently interactive shells such as scp and rcp
# that can't tolerate any output. So make sure this doesn't display
# anything or bad things will happen !
#######################################################################
#
# _ _ _ _ _ _ _ _ _ _ _ _
# / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \
# ( C | L | U | S | T | E | R | S | H | E | L | L )
# \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/
#
#
# By davide DOT rambaldi AT CAMPUS.DOMAIN
#
# Summary:
# 1. check interactivity
# 2. activate bashc completition
# 3. colors for directory listing
# 4. FUNCTION: start_agent --> cache passphrases of private keys
# 5. Change Window TITLE (tested on OSX iTerm.app and Terminal.app)
# 6. USER VARS
#
#######################################################################
#######################################################################
# 1. CHECK IF INTERACTIVE
#######################################################################
# Test for an interactive shell. There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
if [[ $- != *i* ]] ; then
# Shell is non-interactive. Be done now!
return
fi
# Shell is interactive. It is okay to produce output at this point,
# though this example doesn't produce any. Do setup for
# command-line interactivity.
# Put your fun stuff here.
#######################################################################
# 2. BASH COMPLETITION STUFFS
#######################################################################
# ACTIVATE bash completion
[ -f /etc/bash_completion ] && source /etc/bash_completion
[ -f /etc/profile.d/bash-completion ] && source /etc/profile.d/bash-completion
# ACTIVATE GIT BASH COMPLETION
[ -f /etc/bash_completion.d/git ] && source /etc/bash_completion.d/git
#######################################################################
# 3. LS COLORS
#######################################################################
# colors for ls, etc. Prefer ~/.dir_colors #64489
if [[ -f ~/.dir_colors ]]; then
eval `dircolors -b ~/.dir_colors`
else
eval `dircolors -b /etc/DIR_COLORS`
fi
##################################
# 4. SSH AGENT
##################################
# ssh-agent
ssh_agent_conf="$HOME/.ssh-agent.sh"
if [ -f $ssh_agent_conf ]
then
source $ssh_agent_conf
fi
# RUN SHH AGENT TO CACHE PASSPHRASES
SSH_ENV="$HOME/.ssh/environment"
# start the ssh-agent
function start_agent {
echo "Initializing new SSH agent..."
# spawn ssh-agent
ssh-agent | sed 's/^echo/#echo/' > "$SSH_ENV"
echo succeeded
chmod 600 "$SSH_ENV"
. "$SSH_ENV" > /dev/null
ssh-add
}
# test for identities
function test_identities {
# test whether standard identities have been added to the agent already
ssh-add -l | grep "The agent has no identities" > /dev/null
if [ $? -eq 0 ]; then
ssh-add
# $SSH_AUTH_SOCK broken so we start a new proper agent
if [ $? -eq 2 ];then
start_agent
fi
fi
}
# check for running ssh-agent with proper $SSH_AGENT_PID
if [ -n "$SSH_AGENT_PID" ]; then
ps -ef | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
if [ $? -eq 0 ]; then
test_identities
fi
# if $SSH_AGENT_PID is not properly set, we might be able to load one from
# $SSH_ENV
else
if [ -f "$SSH_ENV" ]; then
. "$SSH_ENV" > /dev/null
fi
ps -ef | grep "$SSH_AGENT_PID" | grep -v grep | grep ssh-agent > /dev/null
if [ $? -eq 0 ]; then
test_identities
# else
# start_agent
fi
fi
# NOTE: disabled automatic start_agent on login
#######################################################################
# 5. PROMPT
#######################################################################
##################################################
# Fancy PWD display function
##################################################
# The home directory (HOME) is replaced with a ~
# The last pwdmaxlen characters of the PWD are displayed
# Leading partial directory names are striped off
# /home/me/stuff -> ~/stuff if USER=me
# /usr/share/big_dir_name -> ../share/big_dir_name if pwdmaxlen=20
##################################################
bash_prompt_command() {
# How many characters of the $PWD should be kept
local pwdmaxlen=15
# Indicate that there has been dir truncation
local trunc_symbol=".."
local dir=${PWD##*/}
pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
NEW_PWD=${PWD/#$HOME/\~}
local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
if [ ${pwdoffset} -gt "0" ]
then
NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
fi
}
# write a function to compute the current svn branch
parse_svn_branch() {
SVNBRANCH=`svn info 2>/dev/null | grep Revision: | cut -f 2 -d " "`
if [ -n "$SVNBRANCH" ]; then
BRANCH=" svn:$SVNBRANCH";
else
BRANCH="";
fi
if [[ `svn status 2>/dev/null` != "" ]]
then
BRANCH=$BRANCH*
fi
echo $BRANCH
}
# write a function to compute the current git branch
parse_git_branch() {
GITBRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'`
if [ -n "$GITBRANCH" ]; then
if [[ `git status -s 2>/dev/null` != "" ]]
then
GITBRANCH=$GITBRANCH*
fi
echo $GITBRANCH
fi
}
# write current load average (last minute)
parse_load_average() {
LOAD=`uptime | sed -e s/.*:' '// | sed -e s/,.*//`
echo $LOAD
}
bash_prompt() {
case $TERM in
xterm*|rxvt*)
local TITLEBAR='\[\033]0;\u:${NEW_PWD}\007\]'
;;
*)
local TITLEBAR=""
;;
esac
local NONE="\[\033[0m\]" # unsets color to term's fg color
# regular colors
local K="\[\033[0;30m\]" # black
local R="\[\033[0;31m\]" # red
local G="\[\033[0;32m\]" # green
local Y="\[\033[0;33m\]" # yellow
local B="\[\033[0;34m\]" # blue
local M="\[\033[0;35m\]" # magenta
local C="\[\033[0;36m\]" # cyan
local W="\[\033[0;37m\]" # white
# emphasized (bolded) colors
local EMK="\[\033[1;30m\]"
local EMR="\[\033[1;31m\]"
local EMG="\[\033[1;32m\]"
local EMY="\[\033[1;33m\]"
local EMB="\[\033[1;34m\]"
local EMM="\[\033[1;35m\]"
local EMC="\[\033[1;36m\]"
local EMW="\[\033[1;37m\]"
# background colors
local BGK="\[\033[40m\]"
local BGR="\[\033[41m\]"
local BGG="\[\033[42m\]"
local BGY="\[\033[43m\]"
local BGB="\[\033[44m\]"
local BGM="\[\033[45m\]"
local BGC="\[\033[46m\]"
local BGW="\[\033[47m\]"
local UC=$G # user's color
[ $UID -eq "0" ] && UC=$R # root's color
PS1="$TITLEBAR[${UC}\h${K} (\$(parse_load_average))${UC}:${EMB}\${NEW_PWD}${EMY} \$(parse_git_branch)\$(parse_svn_branch)${K}]\\$ ${NONE}"
# with no colors: PS1="[\h \${NEW_PWD}]\\$ "
# extra backslash in front of \$ to make bash colorize the prompt
# set the PS2 variable
PS2=" > "
}
PROMPT_COMMAND=bash_prompt_command
bash_prompt
unset bash_prompt
##################################
# 6. USER VARS & ALIASES
##################################
CODE=$HOME/Code
PATH=$PATH:/home/drambald/Code/:/home/drambald/bin/
SVNEDITOR=/usr/bin/vim
alias l="ls -lG"
alias ll="ls -al"
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8