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
16 : ${LOGNAME=$(id -un)}
18 # strip OS type and version under Cygwin (e.g. CYGWIN_NT-5.1 => Cygwin)
19 UNAME
=${UNAME/CYGWIN_*/Cygwin}
21 # complete hostnames from this file
22 HOSTFILE
=~
/.ssh
/known_hosts
27 # if current $TERM isn't valid, fall-back to TERM=xterm-color or TERM=xterm
28 case $(tput colors 2>&1) in
30 export TERM
=xterm
-color
31 case $(tput colors 2>&1) in
39 # ---------------------------------------------------------------------------
41 # ---------------------------------------------------------------------------
43 # notify of bg job completion immediately
46 # shell opts. see bash(1) for details
47 shopt -s cdspell
>/dev
/null
2>&1
48 shopt -s checkjobs
>/dev
/null
2>&1
49 shopt -s checkwinsize
>/dev
/null
2>&1
50 shopt -s extglob
>/dev
/null
2>&1
51 shopt -s histappend
>/dev
/null
2>&1
52 shopt -s hostcomplete
>/dev
/null
2>&1
53 shopt -s interactive_comments
>/dev
/null
2>&1
54 shopt -u mailwarn
>/dev
/null
2>&1
55 shopt -s no_empty_cmd_completion
>/dev
/null
2>&1
57 # don't check for new mail
66 # ---------------------------------------------------------------------------
68 # ---------------------------------------------------------------------------
70 # make sure $MANPATH has some sane defaults
71 MANPATH
="/usr/share/man:/usr/local/share/man:$MANPATH"
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"
77 # use $HOME specific bin and sbin
78 path_start
="$path_start:$HOME/bin"
79 test -d "$HOME/sbin" && path_start
="$path_start:$HOME/sbin"
81 # macOS homebrew: include non-prefixed coreutils
82 if [ -d "/opt/homebrew/opt/coreutils/libexec" ]; then
83 path_start
="$path_start:/opt/homebrew/opt/coreutils/libexec/gnubin"
84 manpath_start
="$manpath_start:/opt/homebrew/opt/coreutils/libexec/gnuman"
87 # SmartOS: local pkgin binaries
88 if [ -d "/opt/local" ]; then
89 path_start
="$path_start:/opt/local/sbin:/opt/local/bin"
90 manpath_start
="$manpath_start:/opt/local/man"
92 # SmartOS: local custom scripts
93 if [ -d "/opt/custom" ]; then
94 path_start
="$path_start:/opt/custom/sbin:/opt/custom/bin"
95 manpath_start
="$manpath_start:/opt/custom/man"
98 if [ -d "/smartdc" ]; then
99 path_start
="$path_start:/smartdc/bin:/opt/smartdc/bin:/opt/smartdc/agents/bin"
100 manpath_start
="$manpath_start:/smartdc/man"
102 # SmartOS: native binaries, for OS/LX zones
103 if [ -d "/native" ]; then
104 path_end
="$path_end:/native/usr/sbin:/native/usr/bin:/native/sbin:/native/bin"
105 manpath_end
="$manpath_end:/native/usr/share/man"
109 for python
in "python" "python2" "python3"; do
110 if [ -n "$(type -P $python)" ]; then
111 pybin
="$($python -m site --user-base)/bin"
112 test -d "$pybin" && path_end
="$path_end:$pybin"
117 PATH
="$path_start:$PATH:$path_end"
118 MANPATH
="$manpath_start:$MANPATH:$manpath_end"
119 unset path_start path_end manpath_start manpath_end
121 # ---------------------------------------------------------------------------
122 # ENVIRONMENT CONFIGURATION
123 # ---------------------------------------------------------------------------
125 # detect interactive shell
127 *i
*) INTERACTIVE
=yes ;;
128 *) unset INTERACTIVE
;;
137 # enable en_US locale w/ utf-8 encodings if not already
139 : ${LANG:="en_US.UTF-8"}
141 : ${LC_CTYPE:="en_US.UTF-8"}
142 : ${LC_ALL:="en_US.UTF-8"}
143 export LANG LANGUAGE LC_CTYPE LC_ALL
145 # ignore backups, CVS directories, python bytecode, vim swap files
146 FIGNORE
="~:CVS:#:.pyc:.swp:.swa:apache-solr-*"
149 HISTCONTROL
=ignoreboth
153 # ---------------------------------------------------------------------------
155 # ---------------------------------------------------------------------------
157 # see what we have to work with ...
158 HAVE_VIM
=$(command -v vim)
161 test -n "$HAVE_VIM" &&
167 if test -n "$(command -v less)" ; then
176 export PAGER MANPAGER
178 # ---------------------------------------------------------------------------
180 # ---------------------------------------------------------------------------
182 # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
184 if [ "$UID" = 0 ]; then
186 PS_C1
="\[\033[1;31m\]" # red
187 PS_C2
="\[\033[0;37m\]" # grey
192 PS_C1
="\[\033[0;32m\]" # green
193 PS_C2
="\[\033[0;37m\]" # grey
196 PS_C1
="\[\033[1;97m\]" # white
197 PS_C2
="\[\033[0;37m\]" # grey
200 PS_C1
="\[\033[1;96m\]" # cyan
201 PS_C2
="\[\033[0;36m\]" # cyan
204 PS_C1
="\[\033[1;93m\]" # yellow
205 PS_C2
="\[\033[0;33m\]" # brown
218 PS1
="${PS_C1}${PS_P}\[\033[0m\] "
223 # if git and the git bash_completion scripts are installed, use __git_ps1()
224 # to show current branch info.
225 # - never show branch info for $HOME (dotfiles) repo.
226 # - optionally hide branch info for git repo's with `bash.hidePrompt`
227 # config flag set. use the following to exclude a given repo:
228 # `git config --local --bool --add bash.hidePrompt true`
229 if [ -n "$(type -P git)" -a "$(type -t __git_ps1)" = "function" ]; then
230 home_canonical
="$(test -n "$(type -P realpath)" && realpath
$HOME || echo $HOME)"
231 PS_GIT='$(test -n "$(__git_ps1 %s)" &&
232 test "$(git rev-parse --show-toplevel)" != "$home_canonical" &&
233 test "$(git config --bool bash.hidePrompt)" != "true" &&
234 __git_ps1
"{\[\033[0;40;36m\]%s\[\033[0;90m\]}")'
235 export GIT_PS1_SHOWDIRTYSTATE=1
237 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\] "
241 # ---------------------------------------------------------------------------
243 # ---------------------------------------------------------------------------
248 alias ll.="ls -ld .*"
251 # use 'git
diff --no-index' as a prettier 'diff' alternative (if available)
252 test -n "$(type -P git)" && alias diff="git diff --no-index"
254 # alias 'vi
' to 'vim
' if Vim is installed
255 test -n "$(type -P vim)" && alias vi='vim
'
257 # alias csh-style "rebash" to bash equivalent
258 alias rehash="hash -r"
261 alias svn=svn-wrapper
264 if [ -n "$(type -P docker)" ]; then
265 alias docker-ps="docker ps -a --format \"table {{.ID}}\t{{.Names}}\t{{.State}}\t{{.Status}}\t{{.Networks}}\""
266 alias docker-tail="docker logs -tf --tail 50"
267 alias docker-top="docker run --name ctop -it --rm -v /var/run/docker.sock:/var/run/docker.sock quay.io/vektorlab/ctop"
268 alias ctop="docker-top"
271 if [ "$UNAME" = SunOS ]; then
272 # Solaris: use GNU versions of coreutils
273 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep"
274 test -x /usr/gnu/bin/sed && alias sed="/usr/gnu/bin/sed"
275 test -x /usr/gnu/bin/awk && alias awk="/usr/gnu/bin/awk"
278 # ---------------------------------------------------------------------------
280 # ---------------------------------------------------------------------------
282 test -z "$BASH_COMPLETION" && {
283 bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
284 test -n "$PS1" && test $bmajor -gt 1 && {
285 # search for a bash_completion file to source
286 for f in /usr/local/etc/bash_completion \
287 /usr/pkg/etc/bash_completion \
288 /opt/local/etc/bash_completion \
289 /etc/bash_completion \
290 /etc/bash/bash_completion
298 unset bash bmajor bminor
301 # restore some key environment variables which may have been unset
302 # by bash_completion script
304 : ${LOGNAME=$(id -un)}
307 # override and disable tilde expansion
312 # ---------------------------------------------------------------------------
314 # ---------------------------------------------------------------------------
316 # we always pass these to ls(1)
319 # if the dircolors utility is available, set that up for ls
320 dircolors="$(type -P gdircolors dircolors | head -1)"
321 test -n "$dircolors" && {
322 COLORS=/etc/DIR_COLORS
323 test -e "/etc/DIR_COLORS.$TERM" && COLORS="/etc/DIR_COLORS.$TERM"
324 test -e "$HOME/.dircolors" && COLORS="$HOME/.dircolors"
325 test ! -e "$COLORS" && COLORS=
326 eval `$dircolors --sh $COLORS`
330 # enable color ls output if available
332 LS_COMMON="--color=auto $LS_COMMON"
334 # setup the main ls alias if we've established common args
335 test -n "$LS_COMMON" &&
336 alias ls="command ls $LS_COMMON"
338 # setup color grep output if available
339 if [ -n "$COLORS" ]; then
342 export GREP_COLOR
='1;32' # BSD grep
345 export GREP_COLORS
='fn=36:ln=33:ms=1;32' # GNU grep
350 test -x /usr
/gnu
/bin
/grep && alias grep="/usr/gnu/bin/grep --color=always"
351 test -x /opt
/local
/bin
/grep && alias grep="/opt/local/bin/grep --color=always"
354 alias grep="command grep --color=always"
359 # ---------------------------------------------------------------------------
361 # ---------------------------------------------------------------------------
363 # set 'screen' window title
365 printf "\033k%s\033\\" "$@"
367 # set 'xterm' window title
369 printf "\033]0;%s\007" "$@"
372 # ---------------------------------------------------------------------------
373 # PATH MANIPULATION FUNCTIONS
374 # ---------------------------------------------------------------------------
377 # List path entries of PATH or environment variable <var>.
378 pls
() { eval echo \$
${1:-PATH} | tr ':' '\n'; }
380 # Usage: ppop [-n <num>] [<var>]
381 # Pop <num> entries off the end of PATH or environment variable <var>.
384 [ "$1" = "-n" ] && { n
=$2; shift 2; }
385 local i
=0 var
=${1:-PATH}
386 local list
=$(eval echo \$$var)
387 while [ $i -lt $n ]; do
394 # Usage: ppush <path> [<var>]
395 # Push an entry onto the end of PATH or enviroment variable <var>.
398 local list
=$(eval echo \$$var)
400 IFS
=':' read -a dirs <<< "$1"
401 for ((i
=0; i
<${#dirs[@]}; i
++)); do
402 dir
=$(eval echo "${dirs[$i]}")
403 [ ! -d "$dir" ] && continue
404 [ -n "$list" ] && list
="$list:$dir" || list
="$dir"
409 # Usage: pinsert <path> [<var>]
410 # Push an entry onto the start of PATH or enviroment variable <var>.
413 local list
=$(eval echo \$$var)
415 IFS
=':' read -a dirs <<< "${1}"
416 for ((i
=${#dirs[@]}-1; i
>=0; i
--)); do
417 dir
=$(eval echo "${dirs[$i]}")
418 [ ! -d "$dir" ] && continue
419 [ -n "$list" ] && list
="$dir:$list" || list
="$dir"
424 # Usage: prm <path> [<var>]
425 # Remove <path> from PATH or environment variable <var>.
426 prm
() { eval "${2:-PATH}='$(pls $2 | grep -v "^$1\$" | tr '\n' ':')'"; }
428 # Usage: puniq [<pathlist>]
429 # Remove duplicate entries from a PATH style value while retaining
430 # the original order. Use PATH if no <path> is given.
433 # $ puniq /usr/bin:/usr/local/bin:/usr/bin
434 # /usr/bin:/usr/local/bin
435 puniq
() { echo "$1" | tr ':' '\n' | grep -v "^$" | nl | sort -u -k 2,2 | sort -n | cut
-f 2- | paste -s -d ':' -; }
437 # ---------------------------------------------------------------------------
438 # USER SHELL ENVIRONMENT
439 # ---------------------------------------------------------------------------
441 # source ~/.shenv now if it exists
445 # condense PATH entries
446 PATH
=$(puniq "$PATH")
447 MANPATH
=$(puniq "$MANPATH")
449 # use the color prompt by default when interactive
454 export RIPGREP_CONFIG_PATH
="$HOME/.ripgreprc"
457 export FZF_DEFAULT_COMMAND
="rg --files --hidden --follow --color=never 2> /dev/null"
458 export FZF_DEFAULT_OPTS
='--bind J:down,K:up --reverse --ansi --multi'
460 # ---------------------------------------------------------------------------
462 # ---------------------------------------------------------------------------
464 test -n "$INTERACTIVE" -a -n "$LOGIN" && {
465 # get current uname and uptime (if exists on this host)
466 # strip any leading whitespace from uname and uptime commands
467 t_uname
="$(test -n "`type -P uname`" && uname -npsr | sed -e 's/^\s+//')"
468 t_uptime
="$(test -n "`type -P uptime`" && uptime | sed -e 's/^\s+//')"
469 if [ -n "$t_uname" ] || [ -n "$t_uptime" ]; then
471 test -n "$t_uname" && echo $t_uname
472 test -n "$t_uptime" && echo $t_uptime
475 unset t_uname t_uptime
478 # vim: ts=4 sts=4 shiftwidth=4 expandtab