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