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