]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - .bashrc
.bashrc: Don't include system bashrc, enable more shell options
[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 # ----------------------------------------------------------------------
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 # enable en_US locale w/ utf-8 encodings if not already
102 # configured
103 : ${LANG:="en_US.UTF-8"}
104 : ${LANGUAGE:="en"}
105 : ${LC_CTYPE:="en_US.UTF-8"}
106 : ${LC_ALL:="en_US.UTF-8"}
107 export LANG LANGUAGE LC_CTYPE LC_ALL
108
109 # ignore backups, CVS directories, python bytecode, vim swap files
110 FIGNORE="~:CVS:#:.pyc:.swp:.swa:apache-solr-*"
111
112 # history stuff
113 HISTCONTROL=ignoreboth
114 HISTFILESIZE=10000
115 HISTSIZE=10000
116
117 # ----------------------------------------------------------------------
118 # PAGER / EDITOR
119 # ----------------------------------------------------------------------
120
121 # see what we have to work with ...
122 HAVE_VIM=$(command -v vim)
123
124 # EDITOR
125 test -n "$HAVE_VIM" &&
126 EDITOR=vim ||
127 EDITOR=vi
128 export EDITOR
129
130 # PAGER
131 if test -n "$(command -v less)" ; then
132 PAGER="less"
133 MANPAGER="less"
134 LESS="-FiRX"
135 export LESS
136 else
137 PAGER=more
138 MANPAGER="$PAGER"
139 fi
140 export PAGER MANPAGER
141
142 # ----------------------------------------------------------------------
143 # PROMPT
144 # ----------------------------------------------------------------------
145
146 # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
147
148 if [ "$UID" = 0 ]; then
149 # root
150 PS_C1="\[\033[1;31m\]" # red
151 PS_C2="\[\033[0;37m\]" # grey
152 PS_P="#"
153 else
154 case "$UNAME" in
155 Cygwin)
156 PS_C1="\[\033[0;32m\]" # green
157 PS_C2="\[\033[0;37m\]" # grey
158 ;;
159 Darwin)
160 PS_C1="\[\033[1;97m\]" # white
161 PS_C2="\[\033[0;37m\]" # grey
162 ;;
163 SunOS)
164 PS_C1="\[\033[1;96m\]" # cyan
165 PS_C2="\[\033[0;36m\]" # cyan
166 ;;
167 *)
168 PS_C1="\[\033[1;93m\]" # yellow
169 PS_C2="\[\033[0;33m\]" # brown
170 esac
171 PS_P="\$"
172 fi
173
174 prompt_simple() {
175 unset PROMPT_COMMAND
176 PS1="[\u@\h:\w]\$ "
177 PS2="> "
178 }
179
180 prompt_compact() {
181 unset PROMPT_COMMAND
182 PS1="${PS_C1}${PS_P}\[\033[0m\] "
183 PS2="> "
184 }
185
186 prompt_color() {
187 # if git and the git bash_completion scripts are installed, use __git_ps1() to show current branch info.
188 # never show branch info for $HOME (dotfiles) repo.
189 # use the following to exclude a given repo: `git config --local --bool --add bash.hidePrompt true`
190 if [ -n "$(type -P git)" -a "$(type -t __git_ps1)" = "function" ]; then
191 PS_GIT='$(test -n "$(__git_ps1 %s)" &&
192 test "$(git rev-parse --show-toplevel)" != "$HOME" &&
193 test "$(git config --bool bash.hidePrompt)" != "true" &&
194 __git_ps1 "{\[\033[0;40;36m\]%s\[\033[0;90m\]}")'
195 export GIT_PS1_SHOWDIRTYSTATE=1
196 fi
197 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\] "
198 PS2="> "
199 }
200
201 # ----------------------------------------------------------------------
202 # MACOS X / DARWIN SPECIFIC
203 # ----------------------------------------------------------------------
204
205 if [ "$UNAME" = Darwin ]; then
206 # put ports on the paths if /opt/local exists
207 test -x /opt/local -a ! -L /opt/local && {
208 PORTS=/opt/local
209
210 # setup the PATH and MANPATH
211 PATH="$PORTS/bin:$PORTS/sbin:$PATH"
212 MANPATH="$PORTS/share/man:$MANPATH"
213
214 # nice little port alias
215 alias port="sudo nice -n +18 $PORTS/bin/port"
216 }
217 # put coreutils on the paths if /usr/local/opt/coreutils/libexec exists
218 test -x /usr/local/opt/coreutils/libexec -a ! -L /usr/local/opt/coreutils/libexec && {
219 # setup the PATH and MANPATH
220 PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
221 MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"
222 }
223 fi
224
225 # ----------------------------------------------------------------------
226 # ALIASES / FUNCTIONS
227 # ----------------------------------------------------------------------
228
229 # 'ls' helpers
230 alias ll="ls -l"
231 alias l.="ls -d .*"
232 alias ll.="ls -ld .*"
233 alias lla="ls -la"
234
235 # use 'git diff --no-index' as a prettier 'diff' alternative (if available)
236 test -n "$(type -P git)" && alias diff="git diff --no-index"
237
238 # alias 'vi' to 'vim' if Vim is installed
239 vim="$(type -P vim)"
240 test -n "$vim" && {
241 alias vi='vim'
242 }
243 unset vim
244
245 # disk usage with human sizes and minimal depth
246 alias du1='du -h --max-depth=1'
247 alias fn='find . -name'
248 alias hi='history | tail -20'
249
250 # for Solaris, use GNU versions of core utils
251 if [ "$UNAME" = SunOS ]; then
252 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep"
253 test -x /usr/gnu/bin/sed && alias sed="/usr/gnu/bin/sed"
254 test -x /usr/gnu/bin/awk && alias awk="/usr/gnu/bin/awk"
255 fi
256
257 # alias csh-style "rebash" to bash equivalent
258 alias rehash="hash -r"
259
260 # set 'screen' window title
261 settitle() {
262 printf "\033k%s\033\\" "$@"
263 }
264
265 # svn-wrapper
266 alias svn=svn-wrapper
267
268 # ----------------------------------------------------------------------
269 # BASH COMPLETION
270 # ----------------------------------------------------------------------
271
272 test -z "$BASH_COMPLETION" && {
273 bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
274 test -n "$PS1" && test $bmajor -gt 1 && {
275 # search for a bash_completion file to source
276 for f in /usr/local/etc/bash_completion \
277 /usr/pkg/etc/bash_completion \
278 /opt/local/etc/bash_completion \
279 /etc/bash_completion
280 do
281 test -f $f && {
282 . $f
283 break
284 }
285 done
286 }
287 unset bash bmajor bminor
288 }
289
290 # override and disable tilde expansion
291 _expand() {
292 return 0
293 }
294
295 # ----------------------------------------------------------------------
296 # LS AND DIRCOLORS
297 # ----------------------------------------------------------------------
298
299 # we always pass these to ls(1)
300 LS_COMMON="-p"
301
302 # if the dircolors utility is available, set that up for ls
303 dircolors="$(type -P gdircolors dircolors | head -1)"
304 test -n "$dircolors" && {
305 COLORS=/etc/DIR_COLORS
306 test -e "/etc/DIR_COLORS.$TERM" && COLORS="/etc/DIR_COLORS.$TERM"
307 test -e "$HOME/.dircolors" && COLORS="$HOME/.dircolors"
308 test ! -e "$COLORS" && COLORS=
309 eval `$dircolors --sh $COLORS`
310 }
311 unset dircolors
312
313 # enable color ls output if available
314 test -n "$COLORS" &&
315 LS_COMMON="--color=auto $LS_COMMON"
316
317 # setup the main ls alias if we've established common args
318 test -n "$LS_COMMON" &&
319 alias ls="command ls $LS_COMMON"
320
321 # setup color grep output if available
322 if [ -n "$COLORS" ]; then
323 case "$UNAME" in
324 "SunOS")
325 # for Solaris, use the GNU version of grep for color support
326 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep --color=always"
327 ;;
328 *)
329 alias grep="command grep --color=always"
330 ;;
331 esac
332 # older versions of grep only support a singular highlight color
333 export GREP_COLOR='1;37;42'
334 # newer versions of grep have more flexible color configuration
335 export GREP_COLORS='fn=36:ln=1;33:ms=1;37;42'
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' : | sed -e 's/:$//' -e 's/^://'
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