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