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