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