1 # .bashrc
  2 # 06-sep-2011
  3 #
  4 
  5 # Shell is non-interactive?
  6 [[ $- != *i* ]] && return
  7 
  8 bashrc_v="sep/06/2011"
  9 red="\033[0;31m"
 10 RED="\033[1;31m"
 11 blue="\033[0;34m"
 12 BLUE="\033[1;34m"
 13 cyan="\033[0;36m"
 14 CYAN="\033[1;36m"
 15 NC="\033[0m"
 16 
 17 # Add options for improved verbosity of commands
 18 alias path='echo -e ${PATH//:/\\n}'
 19 alias df='df -kTh'
 20 alias du='du -kh'
 21 if [[ $("uname") == "Darwin" ]]; then
 22   alias ls="ls -F -h -G"
 23 else
 24   alias ls="ls -F -h --color=auto"
 25 fi
 26 
 27 alias cp='cp -v'
 28 alias mv='mv -v'
 29 alias rm='rm -v'
 30 
 31 # enable color grep if color is supported
 32 ( echo chk | grep --color=auto chk &> /dev/null ) && export GREP_OPTIONS="--color=auto"
 33 
 34 # uncomment for single line prompt
 35 #export PS1="\\u@\\H:\\w\\076"
 36 export PS1=$'\\[\\033m\\033[32m\\]\\u@\\h \\[\\033[33m\\w\\033[0m\\]\n$ '
 37 # Make root prompt to be red
 38 [ "$HOME" == "/root" ] && export PS1=$'\\[\\033m\\033[31m\\]\\u@\\h \\[\\033[33m\\w\\033[0m\\]\n# '
 39 export PS2='> '
 40 
 41 # Source CD history code inplace (used to be separate file)
 42 #
 43 # source acd_func.sh
 44 # acd_func 1.0.6, 16-feb-2005
 45 # petar marinov, http://bashrc.sourceforge.net, this is public domain
 46 
 47 cd_func ()
 48 {
 49   local x2 the_new_dir adir index
 50   local -i cnt
 51 
 52   if [[ $1 ==  "--" ]]; then
 53     dirs -v
 54     return 0
 55   fi
 56 
 57   the_new_dir=$1
 58   [[ -z $1 ]] && the_new_dir=$HOME
 59 
 60   if [[ ${the_new_dir:0:1} == '-' ]]; then
 61     #
 62     # Extract dir N from dirs
 63     index=${the_new_dir:1}
 64     [[ -z $index ]] && index=1
 65     adir=$(dirs +$index)
 66     [[ -z $adir ]] && return 1
 67     the_new_dir=$adir
 68   fi
 69 
 70   #
 71   # '~' has to be substituted by ${HOME}
 72   [[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}"
 73 
 74   #
 75   # Now change to the new dir and add to the top of the stack
 76   pushd "${the_new_dir}" > /dev/null
 77   [[ $? -ne 0 ]] && return 1
 78   the_new_dir=$(pwd)
 79 
 80   #
 81   # Trim down everything beyond 11th entry
 82   popd -n +11 2>/dev/null 1>/dev/null
 83 
 84   #
 85   # Remove any other occurence of this dir, skipping the top of the stack
 86   for ((cnt=1; cnt <= 10; cnt++)); do
 87     x2=$(dirs +${cnt} 2>/dev/null)
 88     [[ $? -ne 0 ]] && return 0
 89     [[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
 90     if [[ "${x2}" == "${the_new_dir}" ]]; then
 91       popd -n +$cnt 2>/dev/null 1>/dev/null
 92       cnt=cnt-1
 93     fi
 94   done
 95 
 96   return 0
 97 }
 98 
 99 # Conveniences for command `cd'
100 alias cd=cd_func
101 export CDPATH=.:..:../..:../../..
102 alias ..="cd .."
103 alias ...="cd ../.."
104 alias ....="cd ../../.."
105 alias .....="cd ../../../.."
106 
107 if [[ $BASH_VERSION > "2.05a" ]]; then
108   # alt+w shows the menu
109   bind -x "\"\M-w\":cd_func -- ;"
110 fi
111 # end of acd_func.sh
112 
113 
114 # Arrow-up/-down search history based on a mask, one entry at a time
115 bind '"\e[A"':history-search-backward
116 bind '"\e[B"':history-search-forward
117 bind Space:magic-space
118 
119 # alt+r -- search history based on a mask (augments Arrow up):
120 # alternative shortcut is ctrl+x,ctrl+r
121 # method 1
122 #cmd_mhist="\"\C-k\C-ahistory | grep '^ *[0-9]* *\C-e.'\C-m\""
123 # method 2
124 cmd_mhist="\"\C-k\C-uhistory | grep \\\"^ *[0-9]* *\C-y\\\" \C-m\""
125 bind '"\M-r"':"$cmd_mhist"
126 bind '"\C-x\C-r"':"$cmd_mhist"
127 
128 # alt+k -- paste current command line into history and begin new line
129 # alternative shortcut is ctrl+x,ctrl+k
130 cmd_hist="\"\C-ahistory -s '\C-e'\C-m\""
131 bind '"\M-k"':"$cmd_hist"
132 bind '"\C-x\C-k"':"$cmd_hist"
133 
134 # ctrl+xPgUp: show last 25 entries of the history
135 # alternative shortcut is ctrl+x,ctrl+l
136 # (erase the line first)
137 bind '"\C-x\e[5~"':"\"\C-k\C-uhistory | tail -25\C-m\""
138 bind '"\C-x\C-l"':"\"\C-k\C-uhistory | tail -25\C-m\""
139 
140 # Now map xterm's alternative keybindings to existing functionality
141 # Some are simple translations to correspontend M- combinations
142 # ctrl+left/right arrows:
143 bind '"\e\x5b\x31\x3b\x35\x44"':backward-word
144 bind '"\e\x5b\x31\x3b\x35\x43"':forward-word
145 # alt+b/f: the usual word navigation but in xterm terms
146 bind '"\xe2"':backward-word
147 bind '"\xe6"':forward-word
148 # atl+backspace:
149 bind '"\xff"':backward-kill-word
150 # alt+'.':
151 bind '"\xae"':yank-last-arg
152 # alt+k:
153 bind '"\xeb"':"$cmd_hist"
154 # alt+r:
155 bind '"\xf2"':"$cmd_mhist"
156 
157 unset cmd_hist cmd_mhist
158 
159 # Don't use ^D to exit
160 set -o ignoreeof
161 
162 # Don't put duplicate lines in the history.
163 export HISTCONTROL=ignoredups
164 
165 # Don't put 'history' commands themselves in the history
166 export HISTIGNORE='history*'
167 
168 # Kepp no more than 1000 lines
169 export HISTFILESIZE=1000
170 export HISTSIZE=1000
171 
172 # Append, don't overwrite history
173 shopt -s histappend
174 
175 # Check window size after each command to avoid annoying effects of resizing
176 shopt -s checkwinsize
177 
178 # Write history before accepting a new command
179 # Then read back the merged list
180 export PROMPT_COMMAND='_xtitle; history -a; history -c; history -r'
181 
182 # Show directories bold, everything else is normal
183 if [[ $("uname") == "Darwin" ]]; then
184   export LSCOLORS="Gxxxxxxxxxxxxxxbadxxxx"
185 else
186   # 90 (darkgray) explicitely needed for Mac terminal, 00 is good otherwise
187   export LS_COLORS="no=00:fi=02:di=01:ln=00:pi=00:so=00:bd=00:cd=00:or=00:ex=00"
188 
189 fi
190 
191 
192 # disable XON/XOFF flow control (^s/^q)
193 stty -ixon
194 
195 # This is more objective than the builtin $SHLVL
196 if [[ -z $BASH_NEST ]]; then
197   export BASH_NEST=0
198 else
199   (( BASH_NEST++ ))
200 fi
201 
202 # print
203 echo -e "${cyan}GNU Bash: ${red}v${BASH_VERSION%.*}${cyan}"\
204   " | bashrc: ${red}${bashrc_v}${cyan} |  nest level: ${red}${BASH_NEST}${NC}"
205 
206 # Activate bash_completions if appropriate
207 bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
208 if [ "$PS1" ] && [ $bmajor -eq 2 ] && [ $bminor '>' 04 ] \
209    && [ -f /etc/bash_completion ]; then # interactive shell
210        # Source completion code
211        . /etc/bash_completion
212 fi
213 unset bash bmajor bminor
214 
215 # _xtitle: sets title
216 # no arguments (and no $MANUAL_TITLE) sets the title to current dir
217 function _xtitle ()
218 {
219   a="$1"
220   if [[ -n $MANUAL_TITLE ]]; then
221     a="$MANUAL_TITLE"
222   else
223     # auto is current directory, replace $HOME with ~
224     [[ -z $a ]] && a="${PWD/$HOME/~}"
225   fi
226   case "$TERM" in
227     *term | rxvt | cygwin | xterm-color)
228       echo -n -e "\033]0;$a\007" ;;
229     screen)
230       # add [screen] to the window title & change term name in screen's list
231       echo -n -e "\033]0;$a[screen]\007"
232       echo -n -e "\033k$a\033\134"
233       ;;
234     *)
235       ;;
236   esac
237 }
238 
239 
240 # by default no manual title block
241 unset MANUAL_TITLE
242 
243 # xtitle: for manual set of title, this blocks _xtitle from
244 # doing auto set
245 # call with no parameters to reset to auto
246 function xtitle()
247 {
248   a="$1"
249   if [[ -z "$a" ]]; then
250     unset MANUAL_TITLE
251   else
252     MANUAL_TITLE="$a"
253     _xtitle
254   fi
255 }
256 
257 # changes window title to the command that is executed
258 # could be used as a wrap for execution of any command
259 function title_cmd()
260 {
261   s=$MANUAL_TITLE
262   unset MANUAL_TITLE
263   xtitle "$*"
264   $*
265   MANUAL_TITLE=$s
266 }
267 
268 # for programs that involve user interaction (like doc browsing)
269 # or take longer time (like compilations) it is good to
270 # supply a more informative title
271 # ADD here commands for which you wish to supply better title
272 alias pinfo="title_cmd pinfo"
273 alias info="title_cmd info"
274 alias man="title_cmd man"
275 alias make="title_cmd make"
276 alias less="title_cmd less"
277 alias more="title_cmd more"
278 # find's cmd line is too complicated to prefix
279 # alias find="title_cmd find"
280 alias ping="title_cmd ping"
281 alias vim="title_cmd vim"
282 alias vi="title_cmd vi"
283 alias emacs="title_cmd emacs"
284 alias ftp="title_cmd ftp"
285 alias ncftp="title_cmd ncftp"
286 alias wget="title_cmd wget"
287 
288 # path_once: Adds a new path to $PATH, moves it to the end if it already
289 # exists
290 function path_once()
291 {
292   [ -z "$1" ] && return
293   pa="$PATH:"        # add end marker (one ':')
294   pa="${pa//$1:/}"   # remove any occurrance of $1:
295   pa="${pa//::/:}"   # remove any empty entry, replace :: with :
296   PATH="$pa$1"       # add $1 at the end (we already have the delimiter)
297 }
298 
299 # show a fortune
300 function fo()
301 {
302 wget -q -O -  "http://anduin.eldar.org/cgi-bin/fortune.pl?max_lines=2&text_format=yes"
303 }
304 
305 # continue with other settings
306 if [ -f ~/.bashrc2 ]; then . ~/.bashrc2; fi
307 
308 # the content of this file is public domain
309 # for updates: http://bashrc.sourceforge.net