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