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