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