# ~/.bashrc: executed by bash(1) for non-login shells. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) # for examples # If not running interactively, don't do anything [ -z "$PS1" ] && return # don't put duplicate lines in the history. See bash(1) for more options # ... or force ignoredups and ignorespace HISTCONTROL=ignoredups:ignorespace # append to the history file, don't overwrite it shopt -s histappend # for setting history length see HISTSIZE and HISTFILESIZE in bash(1) HISTSIZE=1000 HISTFILESIZE=2000 # check the window size after each command and, if necessary, # update the values of LINES and COLUMNS. shopt -s checkwinsize # make less more friendly for non-text input files, see lesspipe(1) [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" # set variable identifying the chroot you work in (used in the prompt below) if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then debian_chroot=$(cat /etc/debian_chroot) fi # set a fancy prompt (non-color, unless we know we "want" color) case "$TERM" in xterm-color) color_prompt=yes;; esac # uncomment for a colored prompt, if the terminal has the capability; turned # off by default to not distract the user: the focus in a terminal window # should be on the output of commands, not on the prompt #force_color_prompt=yes if [ -n "$force_color_prompt" ]; then if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then # We have color support; assume it's compliant with Ecma-48 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such # a case would tend to support setf rather than setaf.) color_prompt=yes else color_prompt= fi fi if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' fi unset color_prompt force_color_prompt # If this is an xterm set the title to user@host:dir case "$TERM" in xterm*|rxvt*) PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1" ;; *) ;; esac # enable color support of ls and also add handy aliases if [ -x /usr/bin/dircolors ]; then test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" alias ls='ls --color=auto' #alias dir='dir --color=auto' #alias vdir='vdir --color=auto' alias grep='grep --color=auto' alias fgrep='fgrep --color=auto' alias egrep='egrep --color=auto' fi #------------------------------------------------------------- # Automatic setting of $DISPLAY (if not set already) # This works for linux - your mileage may vary.... # The problem is that different types of terminals give # different answers to 'who am i'...... # I have not found a 'universal' method yet #------------------------------------------------------------- DISPLAY=":0.0" HILIT=${red} export DISPLAY #--------------- # Some settings #--------------- export TIMEFORMAT=$'\nreal %3R\tuser %3U\tsys %3S\tpcpu %P\n' export HISTIGNORE="&:bg:fg:ll:h" export HOSTFILE=$HOME/.hosts # Put a list of remote hosts in ~/.hosts #----------------------- # Greeting, motd etc... #----------------------- # Define some colors first: red='\e[0;31m' RED='\e[1;31m' blue='\e[0;34m' BLUE='\e[1;34m' cyan='\e[0;36m' CYAN='\e[1;36m' NC='\e[0m' # No Color # --> Nice. Has the same effect as using "ansi.sys" in DOS. # Looks best on a black background..... echo -e "${CYAN}This is BASH ${RED}${BASH_VERSION%.*}${CYAN} - DISPLAY on ${RED}$DISPLAY${NC}\n" date if [ -x /usr/games/fortune ]; then /usr/games/fortune -s # makes our day a bit more fun.... :-) fi function _exit() # function to run upon exit of shell { echo -e "${RED}Hasta la vista, baby${NC}" } trap _exit EXIT # Alias definitions. # You may want to put all your additions into a separate file like # ~/.bash_aliases, instead of adding them here directly. # See /usr/share/doc/bash-doc/examples in the bash-doc package. if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi # enable programmable completion features (you don't need to enable # this, if it's already enabled in /etc/bash.bashrc and /etc/profile # sources /etc/bash.bashrc). if [ -f /etc/bash_completion ] && ! shopt -oq posix; then . /etc/bash_completion fi #stub if [[ "${DISPLAY#$HOST}" != ":0.0" && "${DISPLAY}" != ":0" ]]; then HILIT=${red} # remote machine: prompt will be partly red else HILIT=${cyan} # local machine: prompt will be partly cyan fi # --> Replace instances of \W with \w in prompt functions below #+ --> to get display of full path name. function fastprompt() { unset PROMPT_COMMAND case $TERM in *term | rxvt ) PS1="${HILIT}[\h]$NC \W > \[\033]0;\${TERM} [\u@\h] \w\007\]" ;; linux ) PS1="${HILIT}[\h]$NC \W > " ;; *) PS1="[\h] \W > " ;; esac } function powerprompt() { _powerprompt() { LOAD=$(uptime|sed -e "s/.*: \([^,]*\).*/\1/" -e "s/ //g") } PROMPT_COMMAND=_powerprompt case $TERM in *term | rxvt ) PS1="${HILIT}[\A \$LOAD]$NC\n[\h \#] \W > \[\033]0;\${TERM} [\u@\h] \w\007\]" ;; * ) PS1="${HILIT}[\A - \$LOAD]$NC\n[\h \#] \w > " ;; # * ) # PS1="[\A - \$LOAD]\n[\h \#] \w > " ;; esac PS1="${HILIT}[\A - \$LOAD]$NC\n[\h \#] \w > " } powerprompt # this is the default prompt - might be slow # If too slow, use fastprompt instead.... #----------------------------------- # File & strings related functions: #----------------------------------- # Find a file with a pattern in name: function ff() { find . -type f -iname '*'$*'*' -ls ; } # Find a file with pattern $1 in name and Execute $2 on it: function fe() { find . -type f -iname '*'$1'*' -exec "${2:-file}" {} \; ; } # find pattern in a set of filesand highlight them: function fstr() { OPTIND=1 local case="" local usage="fstr: find string in files. Usage: fstr [-i] \"pattern\" [\"filename pattern\"] " while getopts :it opt do case "$opt" in i) case="-i " ;; *) echo "$usage"; return;; esac done shift $(( $OPTIND - 1 )) if [ "$#" -lt 1 ]; then echo "$usage" return; fi local SMSO=$(tput smso) local RMSO=$(tput rmso) find . -type f -name "${2:-*}" -print0 | xargs -0 grep -sn ${case} "$1" 2>&- | \ sed "s/$1/${SMSO}\0${RMSO}/gI" | more } function cuttail() # cut last n lines in file, 10 by default { nlines=${2:-10} sed -n -e :a -e "1,${nlines}!{P;N;D;};N;ba" $1 } function lowercase() # move filenames to lowercase { for file ; do filename=${file##*/} case "$filename" in */*) dirname==${file%/*} ;; *) dirname=.;; esac nf=$(echo $filename | tr A-Z a-z) newname="${dirname}/${nf}" if [ "$nf" != "$filename" ]; then mv "$file" "$newname" echo "lowercase: $file --> $newname" else echo "lowercase: $file not changed." fi done } function swap() # swap 2 filenames around { local TMPFILE=tmp.$$ mv "$1" $TMPFILE mv "$2" "$1" mv $TMPFILE "$2" } function extract() # Handy Extract Program. { if [ -f $1 ] ; then case $1 in *.tar.bz2) tar xvjf $1 ;; *.tar.gz) tar xvzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar x $1 ;; *.gz) gunzip $1 ;; *.tar) tar xvf $1 ;; *.tbz2) tar xvjf $1 ;; *.tgz) tar xvzf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *.7z) 7z x $1 ;; *) echo "'$1' cannot be extracted via >extract<" ;; esac else echo "'$1' is not a valid file" fi } #----------------------------------- # Process/system related functions: #----------------------------------- function my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; } function pp() { my_ps f | awk '!/awk/ && $0~var' var=${1:-".*"} ; } # This function is roughly the same as 'killall' on linux # but has no equivalent (that I know of) on Solaris function killps() # kill by process name { local pid pname sig="-TERM" # default signal if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then echo "Usage: killps [-SIGNAL] pattern" return; fi if [ $# = 2 ]; then sig=$1 ; fi for pid in $(my_ps| awk '!/awk/ && $0~pat { print $1 }' pat=${!#} ) ; do pname=$(my_ps | awk '$1~var { print $5 }' var=$pid ) if ask "Kill process $pid <$pname> with signal $sig?" then kill $sig $pid fi done } function my_ip() # get IP adresses { MY_IP=$(/sbin/ifconfig ppp0 | awk '/inet/ { print $2 } ' | sed -e s/addr://) MY_ISP=$(/sbin/ifconfig ppp0 | awk '/P-t-P/ { print $3 } ' | sed -e s/P-t-P://) } function ii() # get current host related info { echo -e "\nYou are logged on ${RED}$HOST" echo -e "\nAdditionnal information:$NC " ; uname -a echo -e "\n${RED}Users logged on:$NC " ; w -h echo -e "\n${RED}Current date :$NC " ; date echo -e "\n${RED}Machine stats :$NC " ; uptime echo -e "\n${RED}Memory stats :$NC " ; free my_ip 2>&- ; echo -e "\n${RED}Local IP Address :$NC" ; echo ${MY_IP:-"Not connected"} echo -e "\n${RED}ISP Address :$NC" ; echo ${MY_ISP:-"Not connected"} echo } # Misc utilities: function repeat() # repeat n times command { local i max max=$1; shift; for ((i=1; i <= max ; i++)); do # --> C-like syntax eval "$@"; done } function ask() { echo -n "$@" '[y/n] ' ; read ans case "$ans" in y*|Y*) return 0 ;; *) return 1 ;; esac } # some more ls aliases alias ll='ls -alF' alias la='ls -A' alias l='ls -CF' #------------------- # Personnal Aliases #------------------- alias gedit='sudo gedit&' alias nb='ipython notebook --pylab inline' alias pq='ipython qtconsole --pylab' alias chrome='chromium-browser&' alias poweroff='sudo shutdown -h now' alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # -> Prevents accidentally clobbering files. alias mkdir='mkdir -p' alias h='history' alias j='jobs -l' alias r='rlogin' alias which='type -all' alias ..='cd ..' alias path='echo -e ${PATH//:/\\n}' alias print='/usr/bin/lp -o nobanner -d $LPDEST' # Assumes LPDEST is defined alias pjet='enscript -h -G -fCourier9 -d $LPDEST' # Pretty-print using enscript alias background='xv -root -quit -max -rmode 5' # Put a picture in the background alias du='du -kh' alias df='df -kTh' # The 'ls' family (this assumes you use the GNU ls) alias la='ls -Al' # show hidden files alias ls='ls -hF --color' # add colors for filetype recognition alias lx='ls -lXB' # sort by extension alias lk='ls -lSr' # sort by size alias lc='ls -lcr' # sort by change time alias lu='ls -lur' # sort by access time alias lr='ls -lR' # recursive ls alias lt='ls -ltr' # sort by date alias lm='ls -al |more' # pipe through 'more' alias tree='tree -Csu' # nice alternative to 'ls' # tailoring 'less' alias more='less' export PAGER=less export LESSCHARSET='latin1' export LESSOPEN='|/usr/bin/lesspipe.sh %s 2>&-' # Use this if lesspipe.sh exists # spelling typos - highly personnal :-) alias xs='cd' alias vf='cd' alias moer='more' alias moew='more' alias kk='ll' alias lll='ll' export PATH=$PATH:/usr/local/:. export PYTHONPATH=$PYTHONPATH:/home/byron/code/