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