]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - .bashrc
Merge branch 'master' into epic
[dotfiles.git] / .bashrc
1 #!/bin/bash
2 # ~/.bashrc: executed by bash(1) for non-login shells.
3 # A basically sane bash environment.
4 # Tony Duckles <http://nynim.org/about/> (based on http://github.com/rtomayko/dotfiles)
5
6 # short-circuit for non-interactive sessions
7 [ -z "$PS1" ] && return
8
9 # the basics
10 PATH="/usr/local/bin:/bin:/usr/bin:/usr/sbin:/usr/local/sbin:/sbin:$PATH"
11 : ${HOME=~}
12 : ${LOGNAME=$(id -un)}
13 : ${UNAME=$(uname)}
14 # strip OS type and version under Cygwin (e.g. CYGWIN_NT-5.1 => Cygwin)
15 UNAME=${UNAME/CYGWIN_*/Cygwin}
16
17 # complete hostnames from this file
18 HOSTFILE=~/.ssh/known_hosts
19
20 # readline config
21 INPUTRC=~/.inputrc
22
23 # if current $TERM isn't valid, fall-back to TERM=xterm-color or TERM=xterm
24 case $(tput colors 2>&1) in
25 tput* )
26 export TERM=xterm-color
27 case $(tput colors 2>&1) in
28 tput* )
29 export TERM=xterm
30 ;;
31 esac
32 ;;
33 esac
34
35 if [ "$UNAME" = "Darwin" ]; then
36 # Fink init, for OSX
37 test -r /sw/bin/init.sh &&
38 source /sw/bin/init.sh
39 fi
40
41 # ----------------------------------------------------------------------
42 # SHELL OPTIONS
43 # ----------------------------------------------------------------------
44
45 # bring in system bashrc
46 test -r /etc/bashrc &&
47 . /etc/bashrc
48
49 # notify of bg job completion immediately
50 set -o notify
51
52 # shell opts. see bash(1) for details
53 shopt -s cdspell >/dev/null 2>&1
54 shopt -s extglob >/dev/null 2>&1
55 shopt -s histappend >/dev/null 2>&1
56 shopt -s hostcomplete >/dev/null 2>&1
57 shopt -s interactive_comments >/dev/null 2>&1
58 shopt -u mailwarn >/dev/null 2>&1
59 shopt -s no_empty_cmd_completion >/dev/null 2>&1
60
61 # don't check for new mail
62 unset MAILCHECK
63
64 # disable core dumps
65 ulimit -S -c 0
66
67 # default umask
68 umask 0022
69
70 # ----------------------------------------------------------------------
71 # PATH
72 # ----------------------------------------------------------------------
73
74 # we want the various sbin's on the path along with /usr/local/bin
75 PATH="$PATH:/usr/local/sbin:/usr/sbin:/sbin"
76 PATH="/usr/local/bin:$PATH"
77
78 # put /opt/csw/* on PATH if exists (OpenCSW Solaris packages)
79 test -d "/opt/csw" &&
80 PATH="/opt/csw/sbin:/opt/csw/bin:$PATH"
81
82 # put ~/bin on PATH if exists
83 test -d "$HOME/bin" &&
84 PATH="$HOME/bin:$PATH"
85
86 # put ~/sbin on PATH if exists
87 test -d "$HOME/sbin" &&
88 PATH="$HOME/sbin:$PATH"
89
90 # ----------------------------------------------------------------------
91 # ENVIRONMENT CONFIGURATION
92 # ----------------------------------------------------------------------
93
94 # detect interactive shell
95 case "$-" in
96 *i*) INTERACTIVE=yes ;;
97 *) unset INTERACTIVE ;;
98 esac
99
100 # detect login shell
101 case "$0" in
102 -*) LOGIN=yes ;;
103 *) unset LOGIN ;;
104 esac
105
106 # setup locale. Try to enable en_US locale w/ utf-8 encodings
107 # (if not already configured), but do graceful fall-back.
108 lclist=$(locale -a | grep en_US)
109 if test -n "$(echo $lclist | grep UTF-8)"; then
110 : ${LANG:="en_US.UTF-8"}
111 : ${LANGUAGE:="en"}
112 : ${LC_CTYPE:="en_US.UTF-8"}
113 : ${LC_ALL:="en_US.UTF-8"}
114 elif test -n "$(echo $lclist | grep utf8)"; then
115 : ${LANG:="en_US.utf8"}
116 : ${LANGUAGE:="en"}
117 : ${LC_CTYPE:="en_US.utf8"}
118 : ${LC_ALL:="en_US.utf8"}
119 elif test -n "$(echo $lclist | grep ISO8859-1)"; then
120 : ${LANG:="en_US.ISO8859-1"}
121 : ${LANGUAGE:="en"}
122 : ${LC_CTYPE:="en_US.ISO8859-1"}
123 : ${LC_ALL:="en_US.ISO8859-1"}
124 else
125 : ${LANG:="en_US"}
126 : ${LANGUAGE:="en"}
127 : ${LC_CTYPE:="en_US"}
128 : ${LC_ALL:="en_US"}
129 fi
130 export LANG LANGUAGE LC_CTYPE LC_ALL
131 unset lclist
132
133 # ignore backups, CVS directories, python bytecode, vim swap files
134 FIGNORE="~:CVS:#:.pyc:.swp:.swa:apache-solr-*"
135
136 # history stuff
137 HISTCONTROL=ignoreboth
138 HISTFILESIZE=10000
139 HISTSIZE=10000
140
141 # ----------------------------------------------------------------------
142 # PAGER / EDITOR
143 # ----------------------------------------------------------------------
144
145 # See what we have to work with ...
146 HAVE_VIM=$(command -v vim)
147
148 # EDITOR
149 test -n "$HAVE_VIM" &&
150 EDITOR=vim ||
151 EDITOR=vi
152 export EDITOR
153
154 # PAGER
155 if test -n "$(command -v less)" ; then
156 PAGER="less"
157 MANPAGER="less"
158 LESS="-FiRX"
159 export LESS
160 else
161 PAGER=more
162 MANPAGER="$PAGER"
163 fi
164 export PAGER MANPAGER
165
166 # Ack
167 ACK_PAGER="$PAGER"
168 ACK_PAGER_COLOR="$PAGER"
169
170 # ----------------------------------------------------------------------
171 # PROMPT
172 # ----------------------------------------------------------------------
173
174 # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
175
176 if [ "$UID" = 0 ]; then
177 # root
178 PS_C1="\[\033[1;31m\]" # red
179 PS_C2="\[\033[0;37m\]" # grey
180 PS_P="#"
181 else
182 case "$UNAME" in
183 Cygwin)
184 PS_C1="\[\033[0;32m\]" # green
185 PS_C2="\[\033[0;37m\]" # grey
186 ;;
187 Darwin)
188 PS_C1="\[\033[1;97m\]" # white
189 PS_C2="\[\033[0;37m\]" # grey
190 ;;
191 SunOS | AIX)
192 PS_C1="\[\033[1;96m\]" # cyan
193 PS_C2="\[\033[0;36m\]" # cyan
194 ;;
195 *)
196 PS_C1="\[\033[1;93m\]" # yellow
197 PS_C2="\[\033[0;33m\]" # brown
198 esac
199 PS_P="\$"
200 fi
201
202 prompt_simple() {
203 unset PROMPT_COMMAND
204 PS1="[\u@\h:\w]\$ "
205 PS2="> "
206 }
207
208 prompt_compact() {
209 unset PROMPT_COMMAND
210 PS1="${PS_C1}${PS_P}\[\033[0m\] "
211 PS2="> "
212 }
213
214 prompt_color() {
215 PS1="\[\033[0;90m\][${PS_C1}\u@\h\[\033[0m\]\[\033[0;90m\]:${PS_C2}\w\[\033[0;90m\]]${PS_P}\[\033[0m\] "
216 PS2="> "
217 }
218
219 # ----------------------------------------------------------------------
220 # MACOS X / DARWIN SPECIFIC
221 # ----------------------------------------------------------------------
222
223 if [ "$UNAME" = Darwin ]; then
224 # put ports on the paths if /opt/local exists
225 test -x /opt/local -a ! -L /opt/local && {
226 PORTS=/opt/local
227
228 # setup the PATH and MANPATH
229 PATH="$PORTS/bin:$PORTS/sbin:$PATH"
230 MANPATH="$PORTS/share/man:$MANPATH"
231
232 # nice little port alias
233 alias port="sudo nice -n +18 $PORTS/bin/port"
234 }
235 fi
236
237 # ----------------------------------------------------------------------
238 # ALIASES / FUNCTIONS
239 # ----------------------------------------------------------------------
240
241 # alias 'vi' to 'vim' if Vim is installed
242 vim="$(type -P vim)"
243 test -n "$vim" && {
244 alias vi='vim'
245 }
246 unset vim
247
248 # disk usage with human sizes and minimal depth
249 alias du1='du -h --max-depth=1'
250 alias fn='find . -name'
251 alias hi='history | tail -20'
252
253 # For Solaris, use GNU versions of grep and sed
254 if [ "$UNAME" = SunOS ]; then
255 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep"
256 test -x /usr/gnu/bin/sed && alias sed="/usr/gnu/bin/sed"
257 fi
258
259 # Alias csh-style "rebash" to bash equivalent
260 alias rehash="hash -r"
261
262 # ----------------------------------------------------------------------
263 # BASH COMPLETION
264 # ----------------------------------------------------------------------
265
266 test -z "$BASH_COMPLETION" && {
267 bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
268 test -n "$PS1" && test $bmajor -gt 1 && {
269 # search for a bash_completion file to source
270 for f in /usr/local/etc/bash_completion \
271 /usr/pkg/etc/bash_completion \
272 /opt/local/etc/bash_completion \
273 /etc/bash_completion
274 do
275 test -f $f && {
276 . $f
277 break
278 }
279 done
280 }
281 unset bash bmajor bminor
282 }
283
284 # override and disable tilde expansion
285 _expand() {
286 return 0
287 }
288
289 # ----------------------------------------------------------------------
290 # LS AND DIRCOLORS
291 # ----------------------------------------------------------------------
292
293 # we always pass these to ls(1)
294 unset LS_COMMON
295 ## OS-specific options
296 #case "$UNAME" in
297 # "Linux" ) LS_COMMON="--color=auto";;
298 # "Cygwin" ) LS_COMMON="--color=auto";;
299 # "Darwin" ) LS_COMMON="--color=auto";;
300 #esac
301
302 # if the dircolors utility is available, set that up for ls
303 dircolors="$(type -P gdircolors dircolors | head -1)"
304 test -n "$dircolors" && {
305 COLORS=/etc/DIR_COLORS
306 test -e "/etc/DIR_COLORS.$TERM" && COLORS="/etc/DIR_COLORS.$TERM"
307 test -e "$HOME/.dircolors" && COLORS="$HOME/.dircolors"
308 test ! -e "$COLORS" && COLORS=
309 # AIX (vx-track) has dircolors(1) but ls(1) doesn't support --color arg
310 test "$UNAME" = "AIX" && COLORS=
311 eval `$dircolors --sh $COLORS`
312 }
313 unset dircolors
314
315 # enable color ls output if available
316 test -n "$COLORS" &&
317 LS_COMMON="--color=auto $LS_COMMON"
318
319 # setup the main ls alias if we've established common args
320 test -n "$LS_COMMON" &&
321 alias ls="command ls $LS_COMMON"
322
323 # these use the ls aliases above
324 alias ll="ls -l"
325 alias l.="ls -d .*"
326 alias ll.="ls -ld .*"
327 alias lla="ls -la"
328
329 # setup color grep output if available
330 if [ -n "$COLORS" ]; then
331 case "$UNAME" in
332 "SunOS")
333 # For Solaris, use the GNU version of grep for color support
334 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep --color=always"
335 ;;
336 *)
337 alias grep="command grep --color=always"
338 ;;
339 esac
340 # older versions of grep only support a singular highlight color
341 export GREP_COLOR='1;37;42'
342 # newer versions of grep have more flexible color configuration
343 export GREP_COLORS='fn=36:ln=1;33:ms=1;37;42'
344 fi
345
346 # --------------------------------------------------------------------
347 # MISC COMMANDS
348 # --------------------------------------------------------------------
349
350 # push SSH public key to another box
351 push_ssh_cert() {
352 local _host
353 test -f ~/.ssh/id_rsa.pub || ssh-keygen -t rsa -b 4096
354 for _host in "$@";
355 do
356 echo $_host
357 ssh $_host 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub
358 done
359 }
360
361 # --------------------------------------------------------------------
362 # PATH MANIPULATION FUNCTIONS
363 # --------------------------------------------------------------------
364
365 # Usage: pls [<var>]
366 # List path entries of PATH or environment variable <var>.
367 pls () { eval echo \$${1:-PATH} |tr : '\n'; }
368
369 # Usage: pshift [-n <num>] [<var>]
370 # Shift <num> entries off the front of PATH or environment var <var>.
371 # with the <var> option. Useful: pshift $(pwd)
372 pshift () {
373 local n=1
374 [ "$1" = "-n" ] && { n=$(( $2 + 1 )); shift 2; }
375 eval "${1:-PATH}='$(pls |tail -n +$n |tr '\n' :)'"
376 }
377
378 # Usage: ppop [-n <num>] [<var>]
379 # Pop <num> entries off the end of PATH or environment variable <var>.
380 ppop () {
381 local n=1 i=0
382 [ "$1" = "-n" ] && { n=$2; shift 2; }
383 while [ $i -lt $n ]
384 do eval "${1:-PATH}='\${${1:-PATH}%:*}'"
385 i=$(( i + 1 ))
386 done
387 }
388
389 # Usage: prm <path> [<var>]
390 # Remove <path> from PATH or environment variable <var>.
391 prm () { eval "${2:-PATH}='$(pls $2 |grep -v "^$1\$" |tr '\n' :)'"; }
392
393 # Usage: punshift <path> [<var>]
394 # Shift <path> onto the beginning of PATH or environment variable <var>.
395 punshift () { eval "${2:-PATH}='$1:$(eval echo \$${2:-PATH})'"; }
396
397 # Usage: ppush <path> [<var>]
398 ppush () { eval "${2:-PATH}='$(eval echo \$${2:-PATH})':$1"; }
399
400 # Usage: puniq [<path>]
401 # Remove duplicate entries from a PATH style value while retaining
402 # the original order. Use PATH if no <path> is given.
403 #
404 # Example:
405 # $ puniq /usr/bin:/usr/local/bin:/usr/bin
406 # /usr/bin:/usr/local/bin
407 puniq () {
408 echo "$1" | tr : '\n' | nl | sort -u -k 2,2 | sort -n |
409 cut -f 2- | tr '\n' :
410 }
411
412 # -------------------------------------------------------------------
413 # USER SHELL ENVIRONMENT
414 # -------------------------------------------------------------------
415
416 # source ~/.shenv now if it exists
417 test -r ~/.shenv &&
418 . ~/.shenv
419
420 # condense PATH entries
421 PATH=$(puniq "$PATH")
422 MANPATH=$(puniq "$MANPATH")
423
424 # Use the color prompt by default when interactive
425 test -n "$PS1" &&
426 prompt_color
427
428 # -------------------------------------------------------------------
429 # MOTD / FORTUNE
430 # -------------------------------------------------------------------
431
432 test -n "$INTERACTIVE" -a -n "$LOGIN" && {
433 # Get current uname and uptime (if exists on this host)
434 # Strip any leading whitespace from uname and uptime commands
435 t_uname="$(test -n "`type -P uname`" && uname -npsr | sed -e 's/^\s+//')"
436 t_uptime="$(test -n "`type -P uptime`" && uptime | sed -e 's/^\s+//')"
437 if [ -n "$t_uname" ] || [ -n "$t_uptime" ]; then
438 echo " --"
439 test -n "$t_uname" && echo $t_uname
440 test -n "$t_uptime" && echo $t_uptime
441 echo " --"
442 fi
443 unset t_uname t_uptime
444 }
445
446 # vim: ts=4 sts=4 shiftwidth=4 expandtab