2 # ~/.bashrc: executed by bash(1) for non-login shells.
3 # A basically sane bash environment.
5 # - https://github.com/rtomayko/dotfiles/blob/rtomayko/.bashrc
7 # ---------------------------------------------------------------------------
9 # ---------------------------------------------------------------------------
11 # short-circuit for non-interactive sessions
12 [ -z "$PS1" ] && return
15 PATH
="/usr/local/bin:/bin:/usr/bin:/usr/sbin:/usr/local/sbin:/sbin:$PATH"
17 : ${LOGNAME=$(id -un)}
19 # strip OS type and version under Cygwin (e.g. CYGWIN_NT-5.1 => Cygwin)
20 UNAME
=${UNAME/CYGWIN_*/Cygwin}
22 # complete hostnames from this file
23 HOSTFILE
=~
/.ssh
/known_hosts
28 # if current $TERM isn't valid, fall-back to TERM=xterm-color or TERM=xterm
29 case $(tput colors 2>&1) in
31 export TERM
=xterm
-color
32 case $(tput colors 2>&1) in
40 # ---------------------------------------------------------------------------
42 # ---------------------------------------------------------------------------
44 # notify of bg job completion immediately
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
58 # don't check for new mail
67 # ---------------------------------------------------------------------------
69 # ---------------------------------------------------------------------------
71 # make sure $MANPATH has some sane defaults
72 MANPATH
="/usr/share/man:/usr/local/share/man:$MANPATH"
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"
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"
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"
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"
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"
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"
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"
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"
118 PATH
="$path_start:$PATH:$path_end"
119 MANPATH
="$manpath_start:$MANPATH:$manpath_end"
120 unset path_start path_end manpath_start manpath_end
122 # ---------------------------------------------------------------------------
123 # ENVIRONMENT CONFIGURATION
124 # ---------------------------------------------------------------------------
126 # detect interactive shell
128 *i
*) INTERACTIVE
=yes ;;
129 *) unset INTERACTIVE
;;
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"}
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"}
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"}
154 : ${LC_CTYPE:="en_US.ISO8859-1"}
155 : ${LC_ALL:="en_US.ISO8859-1"}
159 : ${LC_CTYPE:="en_US"}
162 export LANG LANGUAGE LC_CTYPE LC_ALL
165 # ignore backups, CVS directories, python bytecode, vim swap files
166 FIGNORE
="~:CVS:#:.pyc:.swp:.swa:apache-solr-*"
169 HISTCONTROL
=ignoreboth
173 # ---------------------------------------------------------------------------
175 # ---------------------------------------------------------------------------
177 # see what we have to work with ...
178 HAVE_VIM
=$(command -v vim)
181 test -n "$HAVE_VIM" &&
187 if test -n "$(command -v less)" ; then
196 export PAGER MANPAGER
198 # ---------------------------------------------------------------------------
200 # ---------------------------------------------------------------------------
202 # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
204 if [ "$UID" = 0 ]; then
206 PS_C1
="\[\033[1;31m\]" # red
207 PS_C2
="\[\033[0;37m\]" # grey
212 PS_C1
="\[\033[0;32m\]" # green
213 PS_C2
="\[\033[0;37m\]" # grey
216 PS_C1
="\[\033[1;97m\]" # white
217 PS_C2
="\[\033[0;37m\]" # grey
220 PS_C1
="\[\033[1;96m\]" # cyan
221 PS_C2
="\[\033[0;36m\]" # cyan
224 PS_C1
="\[\033[1;93m\]" # yellow
225 PS_C2
="\[\033[0;33m\]" # brown
238 PS1
="${PS_C1}${PS_P}\[\033[0m\] "
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
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\] "
257 # ----------------------------------------------------------------------
259 # ----------------------------------------------------------------------
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
/xpg
4/bin
/sed && alias sed="/usr/xpg4/bin/sed"
267 test -x /usr
/xpg
4/bin
/awk && alias awk="/usr/xpg4/bin/awk"
270 # ---------------------------------------------------------------------------
272 # ---------------------------------------------------------------------------
277 alias ll.
="ls -ld .*"
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"
283 # alias 'vi' to 'vim' if Vim is installed
284 test -n "$(type -P vim)" && alias vi
='vim'
286 # alias csh-style "rebash" to bash equivalent
287 alias rehash
="hash -r"
290 alias svn
=svn
-wrapper
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"
299 # ---------------------------------------------------------------------------
301 # ---------------------------------------------------------------------------
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
319 unset bash bmajor bminor
322 # restore some key environment variables which may have been unset
323 # by bash_completion script
325 : ${LOGNAME=$(id -un)}
328 # override and disable tilde expansion
333 # ---------------------------------------------------------------------------
335 # ---------------------------------------------------------------------------
337 # we always pass these to ls(1)
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`
353 # enable color ls output if available
355 LS_COMMON
="--color=auto $LS_COMMON"
357 # setup the main ls alias if we've established common args
358 test -n "$LS_COMMON" &&
359 alias ls="command ls $LS_COMMON"
361 # setup color grep output if available
362 if [ -n "$COLORS" ]; then
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"
369 alias grep="command grep --color=always"
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'
378 # ---------------------------------------------------------------------------
380 # ---------------------------------------------------------------------------
382 # set 'screen' window title
384 printf "\033k%s\033\\" "$@"
386 # set 'xterm' window title
388 printf "\033]0;%s\007" "$@"
391 # push SSH public key to another box
394 test -f ~
/.ssh
/id_rsa.pub
|| ssh-keygen -t rsa
-b 4096
398 ssh $_host 'cat >> ~/.ssh/authorized_keys' < ~
/.ssh
/id_rsa.pub
402 # ---------------------------------------------------------------------------
403 # PATH MANIPULATION FUNCTIONS
404 # ---------------------------------------------------------------------------
407 # List path entries of PATH or environment variable <var>.
408 pls
() { eval echo \$
${1:-PATH} | tr ':' '\n'; }
410 # Usage: ppop [-n <num>] [<var>]
411 # Pop <num> entries off the end of PATH or environment variable <var>.
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
424 # Usage: ppush <path> [<var>]
425 # Push an entry onto the end of PATH or enviroment variable <var>.
428 local list
=$(eval echo \$$var)
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"
439 # Usage: pinsert <path> [<var>]
440 # Push an entry onto the start of PATH or enviroment variable <var>.
443 local list
=$(eval echo \$$var)
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"
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' ':')'"; }
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.
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 ':' -; }
467 # ---------------------------------------------------------------------------
468 # USER SHELL ENVIRONMENT
469 # ---------------------------------------------------------------------------
471 # source ~/.shenv now if it exists
475 # condense PATH entries
476 PATH
=$(puniq "$PATH")
477 MANPATH
=$(puniq "$MANPATH")
479 # use the color prompt by default when interactive
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'
487 # ---------------------------------------------------------------------------
489 # ---------------------------------------------------------------------------
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
498 test -n "$t_uname" && echo $t_uname
499 test -n "$t_uptime" && echo $t_uptime
502 unset t_uname t_uptime
505 # vim: ts=4 sts=4 shiftwidth=4 expandtab
506 # vim: foldmethod=expr foldexpr=autofolds#foldexpr(v\:lnum,'sh') foldtext=autofolds#foldtext() foldlevel=2