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