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