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