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