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