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()
244 # to show current branch info.
245 # - never show branch info for $HOME (dotfiles) repo.
246 # - optionally hide branch info for git repo's with `bash.hidePrompt`
247 # config flag set. use the following to exclude a given repo:
248 # `git config --local --bool --add bash.hidePrompt true`
249 if [ -n "$(type -P git)" -a "$(type -t __git_ps1)" = "function" ]; then
250 home_canonical
="$(test -n "$(type -P realpath)" && realpath
$HOME || echo $HOME)"
251 PS_GIT='$(test -n "$(__git_ps1 %s)" &&
252 test "$(git rev-parse --show-toplevel)" != "$home_canonical" &&
253 test "$(git config --bool bash.hidePrompt)" != "true" &&
254 __git_ps1
"{\[\033[0;40;36m\]%s\[\033[0;90m\]}")'
255 export GIT_PS1_SHOWDIRTYSTATE=1
257 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\] "
261 # ----------------------------------------------------------------------
263 # ----------------------------------------------------------------------
265 if [ "$UNAME" = SunOS ]; then
266 # use GNU versions of core utils
267 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep"
268 test -x /usr/gnu/bin/sed && alias sed="/usr/gnu/bin/sed"
269 test -x /usr/gnu/bin/awk && alias awk="/usr/gnu/bin/awk"
270 test -x /usr/xpg4/bin/sed && alias sed="/usr/xpg4/bin/sed"
271 test -x /usr/xpg4/bin/awk && alias awk="/usr/xpg4/bin/awk"
274 # ---------------------------------------------------------------------------
276 # ---------------------------------------------------------------------------
281 alias ll.="ls -ld .*"
284 # use 'git
diff --no-index' as a prettier 'diff' alternative (if available)
285 test -n "$(type -P git)" && alias diff="git diff --no-index"
287 # alias 'vi
' to 'vim
' if Vim is installed
288 test -n "$(type -P vim)" && alias vi='vim
'
290 # alias csh-style "rebash" to bash equivalent
291 alias rehash="hash -r"
294 alias svn=svn-wrapper
297 if [ -n "$(type -P docker)" ]; then
298 alias docker-ps="docker ps -a --format \"table {{.ID}}\t{{.Names}}\t{{.State}}\t{{.Status}}\t{{.Networks}}\""
299 alias docker-tail="docker logs -tf --tail 50"
300 alias docker-top="docker run --name ctop -it --rm -v /var/run/docker.sock:/var/run/docker.sock quay.io/vektorlab/ctop"
301 alias ctop="docker-top"
304 if [ "$UNAME" = SunOS ]; then
305 # Solaris: use GNU versions of coreutils
306 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep"
307 test -x /usr/gnu/bin/sed && alias sed="/usr/gnu/bin/sed"
308 test -x /usr/gnu/bin/awk && alias awk="/usr/gnu/bin/awk"
311 # ---------------------------------------------------------------------------
313 # ---------------------------------------------------------------------------
315 test -z "$BASH_COMPLETION" && {
316 bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
317 test -n "$PS1" && test $bmajor -gt 1 && {
318 # search for a bash_completion file to source
319 for f in /usr/local/etc/bash_completion \
320 /usr/pkg/etc/bash_completion \
321 /opt/local/etc/bash_completion \
322 /etc/bash_completion \
323 /etc/bash/bash_completion
331 unset bash bmajor bminor
334 # restore some key environment variables which may have been unset
335 # by bash_completion script
337 : ${LOGNAME=$(id -un)}
340 # override and disable tilde expansion
345 # ---------------------------------------------------------------------------
347 # ---------------------------------------------------------------------------
349 # we always pass these to ls(1)
352 # if the dircolors utility is available, set that up for ls
353 dircolors="$(type -P gdircolors dircolors | head -1)"
354 test -n "$dircolors" && {
355 COLORS=/etc/DIR_COLORS
356 test -e "/etc/DIR_COLORS.$TERM" && COLORS="/etc/DIR_COLORS.$TERM"
357 test -e "$HOME/.dircolors" && COLORS="$HOME/.dircolors"
358 test ! -e "$COLORS" && COLORS=
359 # AIX (vx-track) has dircolors(1) but ls(1) doesn't support
--color arg
360 test "$UNAME" = "AIX" && COLORS
=
361 eval `$dircolors --sh $COLORS`
365 # enable color ls output if available
367 LS_COMMON
="--color=auto $LS_COMMON"
369 # setup the main ls alias if we've established common args
370 test -n "$LS_COMMON" &&
371 alias ls="command ls $LS_COMMON"
373 # setup color grep output if available
374 if [ -n "$COLORS" ]; then
377 test -x /usr
/gnu
/bin
/grep && alias grep="/usr/gnu/bin/grep --color=always"
378 test -x /opt
/local
/bin
/grep && alias grep="/opt/local/bin/grep --color=always"
381 alias grep="command grep --color=always"
384 # older versions of grep only support a singular highlight color
385 export GREP_COLOR
='1;32'
386 # newer versions of grep have more flexible color configuration
387 export GREP_COLORS
='fn=36:ln=33:ms=1;32'
390 # ---------------------------------------------------------------------------
392 # ---------------------------------------------------------------------------
394 # set 'screen' window title
396 printf "\033k%s\033\\" "$@"
398 # set 'xterm' window title
400 printf "\033]0;%s\007" "$@"
403 # push SSH public key to another box
406 test -f ~
/.ssh
/id_rsa.pub
|| ssh-keygen -t rsa
-b 4096
410 ssh $_host 'cat >> ~/.ssh/authorized_keys' < ~
/.ssh
/id_rsa.pub
414 # ---------------------------------------------------------------------------
415 # PATH MANIPULATION FUNCTIONS
416 # ---------------------------------------------------------------------------
419 # List path entries of PATH or environment variable <var>.
420 pls
() { eval echo \$
${1:-PATH} | tr ':' '\n'; }
422 # Usage: ppop [-n <num>] [<var>]
423 # Pop <num> entries off the end of PATH or environment variable <var>.
426 [ "$1" = "-n" ] && { n
=$2; shift 2; }
427 local i
=0 var
=${1:-PATH}
428 local list
=$(eval echo \$$var)
429 while [ $i -lt $n ]; do
436 # Usage: ppush <path> [<var>]
437 # Push an entry onto the end of PATH or enviroment variable <var>.
440 local list
=$(eval echo \$$var)
442 IFS
=':' read -a dirs <<< "$1"
443 for ((i
=0; i
<${#dirs[@]}; i
++)); do
444 dir
=$(eval echo "${dirs[$i]}")
445 [ ! -d "$dir" ] && continue
446 [ -n "$list" ] && list
="$list:$dir" || list
="$dir"
451 # Usage: pinsert <path> [<var>]
452 # Push an entry onto the start of PATH or enviroment variable <var>.
455 local list
=$(eval echo \$$var)
457 IFS
=':' read -a dirs <<< "${1}"
458 for ((i
=${#dirs[@]}-1; i
>=0; i
--)); do
459 dir
=$(eval echo "${dirs[$i]}")
460 [ ! -d "$dir" ] && continue
461 [ -n "$list" ] && list
="$dir:$list" || list
="$dir"
466 # Usage: prm <path> [<var>]
467 # Remove <path> from PATH or environment variable <var>.
468 prm
() { eval "${2:-PATH}='$(pls $2 | grep -v "^$1\$" | tr '\n' ':')'"; }
470 # Usage: puniq [<pathlist>]
471 # Remove duplicate entries from a PATH style value while retaining
472 # the original order. Use PATH if no <path> is given.
475 # $ puniq /usr/bin:/usr/local/bin:/usr/bin
476 # /usr/bin:/usr/local/bin
477 puniq
() { echo "$1" | tr ':' '\n' | grep -v "^$" | nl | sort -u -k 2,2 | sort -n | cut
-f 2- | paste -s -d ':' -; }
479 # ---------------------------------------------------------------------------
480 # USER SHELL ENVIRONMENT
481 # ---------------------------------------------------------------------------
483 # source ~/.shenv now if it exists
487 # condense PATH entries
488 PATH
=$(puniq "$PATH")
489 MANPATH
=$(puniq "$MANPATH")
491 # use the color prompt by default when interactive
496 export FZF_DEFAULT_COMMAND
='rg --files --no-ignore --hidden --follow -g "!{.git,node_modules,*.swp,.venv}/*" 2> /dev/null'
497 export FZF_DEFAULT_OPTS
='--bind J:down,K:up --reverse --ansi --multi'
499 # ---------------------------------------------------------------------------
501 # ---------------------------------------------------------------------------
503 test -n "$INTERACTIVE" -a -n "$LOGIN" && {
504 # get current uname and uptime (if exists on this host)
505 # strip any leading whitespace from uname and uptime commands
506 t_uname
="$(test -n "`type -P uname`" && uname -npsr | sed -e 's/^\s+//')"
507 t_uptime
="$(test -n "`type -P uptime`" && uptime | sed -e 's/^\s+//')"
508 if [ -n "$t_uname" ] || [ -n "$t_uptime" ]; then
510 test -n "$t_uname" && echo $t_uname
511 test -n "$t_uptime" && echo $t_uptime
514 unset t_uname t_uptime
517 # vim: ts=4 sts=4 shiftwidth=4 expandtab