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 test -d "$HOME/bin" && PATH
="$HOME/bin:$PATH"
79 test -d "$HOME/sbin" && PATH
="$HOME/sbin:$PATH"
81 # macOS homebrew: include non-prefixed coreutils
82 test -x /usr
/local
/opt
/coreutils
/libexec
-a ! -L /usr
/local
/opt
/coreutils
/libexec
&& {
83 # setup the PATH and MANPATH
84 PATH
="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
85 MANPATH
="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"
89 test -d "/opt/local" && PATH
="/opt/local/sbin:/opt/local/bin:$PATH"
90 test -d "/opt/local/man" && MANPATH
="/opt/local/man:$MANPATH"
92 test -d "/opt/custom" && PATH
="/opt/custom/sbin:/opt/custom/bin:$PATH"
93 test -d "/opt/custom/man" && MANPATH
="/opt/custom/man:$MANPATH"
95 test -d "/smartdc" && PATH
="/smartdc/bin:/opt/smartdc/bin:/opt/smartdc/agents/bin:$PATH"
96 test -d "/smartdc/man" && MANPATH
="/smartdc/man:$MANPATH"
97 # SmartOS native binaries, for OS/LX zones
98 test -d "/native" && PATH
="$PATH:/native/usr/sbin:/native/usr/bin:/native/sbin:/native/bin"
99 test -d "/native/usr/share/man" && MANPATH
="$MANPATH:/native/usr/share/man"
102 test -d "$HOME/.rvm/bin" && PATH
="$PATH:$HOME/.rvm/bin"
105 test -d "/usr/local/share/golang" && export GOPATH
=/usr
/local
/share
/golang
106 test -n "$GOPATH" && PATH
="$PATH:$GOPATH/bin"
108 # ---------------------------------------------------------------------------
109 # ENVIRONMENT CONFIGURATION
110 # ---------------------------------------------------------------------------
112 # detect interactive shell
114 *i
*) INTERACTIVE
=yes ;;
115 *) unset INTERACTIVE
;;
124 # enable en_US locale w/ utf-8 encodings if not already
126 : ${LANG:="en_US.UTF-8"}
128 : ${LC_CTYPE:="en_US.UTF-8"}
129 : ${LC_ALL:="en_US.UTF-8"}
130 export LANG LANGUAGE LC_CTYPE LC_ALL
132 # ignore backups, CVS directories, python bytecode, vim swap files
133 FIGNORE
="~:CVS:#:.pyc:.swp:.swa:apache-solr-*"
136 HISTCONTROL
=ignoreboth
140 # ---------------------------------------------------------------------------
142 # ---------------------------------------------------------------------------
144 # see what we have to work with ...
145 HAVE_VIM
=$(command -v vim)
148 test -n "$HAVE_VIM" &&
154 if test -n "$(command -v less)" ; then
163 export PAGER MANPAGER
165 # ---------------------------------------------------------------------------
167 # ---------------------------------------------------------------------------
169 # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
171 if [ "$UID" = 0 ]; then
173 PS_C1
="\[\033[1;31m\]" # red
174 PS_C2
="\[\033[0;37m\]" # grey
179 PS_C1
="\[\033[0;32m\]" # green
180 PS_C2
="\[\033[0;37m\]" # grey
183 PS_C1
="\[\033[1;97m\]" # white
184 PS_C2
="\[\033[0;37m\]" # grey
187 PS_C1
="\[\033[1;96m\]" # cyan
188 PS_C2
="\[\033[0;36m\]" # cyan
191 PS_C1
="\[\033[1;93m\]" # yellow
192 PS_C2
="\[\033[0;33m\]" # brown
205 PS1
="${PS_C1}${PS_P}\[\033[0m\] "
210 # if git and the git bash_completion scripts are installed, use __git_ps1() to show current branch info.
211 # never show branch info for $HOME (dotfiles) repo.
212 # use the following to exclude a given repo: `git config --local --bool --add bash.hidePrompt true`
213 if [ -n "$(type -P git)" -a "$(type -t __git_ps1)" = "function" ]; then
214 PS_GIT
='$(test -n "$(__git_ps1 %s)" &&
215 test "$(git rev-parse --show-toplevel)" != "$HOME" &&
216 test "$(git config --bool bash.hidePrompt)" != "true" &&
217 __git_ps1 "{\[\033[0;40;36m\]%s\[\033[0;90m\]}")'
218 export GIT_PS1_SHOWDIRTYSTATE
=1
220 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\] "
224 # ---------------------------------------------------------------------------
226 # ---------------------------------------------------------------------------
231 alias ll.
="ls -ld .*"
234 # use 'git diff --no-index' as a prettier 'diff' alternative (if available)
235 test -n "$(type -P git)" && alias diff="git diff --no-index"
237 # alias 'vi' to 'vim' if Vim is installed
238 test -n "$(type -P vim)" && alias vi
='vim'
240 # alias csh-style "rebash" to bash equivalent
241 alias rehash
="hash -r"
244 alias svn
=svn
-wrapper
246 if [ "$UNAME" = SunOS
]; then
247 # Solaris: use GNU versions of coreutils
248 test -x /usr
/gnu
/bin
/grep && alias grep="/usr/gnu/bin/grep"
249 test -x /usr
/gnu
/bin
/sed && alias sed="/usr/gnu/bin/sed"
250 test -x /usr
/gnu
/bin
/awk && alias awk="/usr/gnu/bin/awk"
253 # ---------------------------------------------------------------------------
255 # ---------------------------------------------------------------------------
257 test -z "$BASH_COMPLETION" && {
258 bash
=${BASH_VERSION%.*}; bmajor
=${bash%.*}; bminor
=${bash#*.}
259 test -n "$PS1" && test $bmajor -gt 1 && {
260 # search for a bash_completion file to source
261 for f
in /usr
/local
/etc
/bash_completion \
262 /usr
/pkg
/etc
/bash_completion \
263 /opt
/local
/etc
/bash_completion \
264 /etc
/bash_completion \
265 /etc
/bash
/bash_completion
273 unset bash bmajor bminor
276 # restore some key environment variables which may have been unset
277 # by bash_completion script
279 : ${LOGNAME=$(id -un)}
282 # override and disable tilde expansion
287 # ---------------------------------------------------------------------------
289 # ---------------------------------------------------------------------------
291 # we always pass these to ls(1)
294 # if the dircolors utility is available, set that up for ls
295 dircolors
="$(type -P gdircolors dircolors | head -1)"
296 test -n "$dircolors" && {
297 COLORS
=/etc
/DIR_COLORS
298 test -e "/etc/DIR_COLORS.$TERM" && COLORS
="/etc/DIR_COLORS.$TERM"
299 test -e "$HOME/.dircolors" && COLORS
="$HOME/.dircolors"
300 test ! -e "$COLORS" && COLORS
=
301 eval `$dircolors --sh $COLORS`
305 # enable color ls output if available
307 LS_COMMON
="--color=auto $LS_COMMON"
309 # setup the main ls alias if we've established common args
310 test -n "$LS_COMMON" &&
311 alias ls="command ls $LS_COMMON"
313 # setup color grep output if available
314 if [ -n "$COLORS" ]; then
317 test -x /usr
/gnu
/bin
/grep && alias grep="/usr/gnu/bin/grep --color=always"
318 test -x /opt
/local
/bin
/grep && alias grep="/opt/local/bin/grep --color=always"
321 alias grep="command grep --color=always"
324 # older versions of grep only support a singular highlight color
325 export GREP_COLOR
='1;32'
326 # newer versions of grep have more flexible color configuration
327 export GREP_COLORS
='fn=36:ln=33:ms=1;32'
330 # ---------------------------------------------------------------------------
332 # ---------------------------------------------------------------------------
334 # set 'screen' window title
336 printf "\033k%s\033\\" "$@"
338 # set 'xterm' window title
340 printf "\033]0;%s\007" "$@"
343 # push SSH public key to another box
346 test -f ~
/.ssh
/id_rsa.pub
|| ssh-keygen -t rsa
-b 4096
350 ssh $_host 'cat >> ~/.ssh/authorized_keys' < ~
/.ssh
/id_rsa.pub
354 # ---------------------------------------------------------------------------
355 # PATH MANIPULATION FUNCTIONS
356 # ---------------------------------------------------------------------------
359 # List path entries of PATH or environment variable <var>.
360 pls
() { eval echo \$
${1:-PATH} | tr ':' '\n'; }
362 # Usage: ppop [-n <num>] [<var>]
363 # Pop <num> entries off the end of PATH or environment variable <var>.
366 [ "$1" = "-n" ] && { n
=$2; shift 2; }
367 local i
=0 var
=${1:-PATH}
368 local list
=$(eval echo \$$var)
369 while [ $i -lt $n ]; do
376 # Usage: ppush <path> [<var>]
377 # Push an entry onto the end of PATH or enviroment variable <var>.
380 local list
=$(eval echo \$$var)
382 IFS
=':' read -a dirs <<< "$1"
383 for ((i
=0; i
<${#dirs[@]}; i
++)); do
384 dir
=$(eval echo "${dirs[$i]}")
385 [ ! -d "$dir" ] && continue
386 [ -n "$list" ] && list
="$list:$dir" || list
="$dir"
391 # Usage: pinsert <path> [<var>]
392 # Push an entry onto the start of PATH or enviroment variable <var>.
395 local list
=$(eval echo \$$var)
397 IFS
=':' read -a dirs <<< "${1}"
398 for ((i
=${#dirs[@]}-1; i
>=0; i
--)); do
399 dir
=$(eval echo "${dirs[$i]}")
400 [ ! -d "$dir" ] && continue
401 [ -n "$list" ] && list
="$dir:$list" || list
="$dir"
406 # Usage: prm <path> [<var>]
407 # Remove <path> from PATH or environment variable <var>.
408 prm
() { eval "${2:-PATH}='$(pls $2 | grep -v "^$1\$" | tr '\n' ':')'"; }
410 # Usage: puniq [<pathlist>]
411 # Remove duplicate entries from a PATH style value while retaining
412 # the original order. Use PATH if no <path> is given.
415 # $ puniq /usr/bin:/usr/local/bin:/usr/bin
416 # /usr/bin:/usr/local/bin
417 puniq
() { echo "$1" | tr ':' '\n' | grep -v "^$" | nl | sort -u -k 2,2 | sort -n | cut
-f 2- | paste -s -d ':'; }
419 # ---------------------------------------------------------------------------
420 # USER SHELL ENVIRONMENT
421 # ---------------------------------------------------------------------------
423 # source ~/.shenv now if it exists
427 # condense PATH entries
428 PATH
=$(puniq "$PATH")
429 MANPATH
=$(puniq "$MANPATH")
431 # use the color prompt by default when interactive
436 export FZF_DEFAULT_COMMAND
='rg --files --no-ignore --hidden --follow -g "!{.git,node_modules,*.swp,.venv}/*" 2> /dev/null'
437 export FZF_DEFAULT_OPTS
='--bind J:down,K:up --reverse --ansi --multi'
439 # ---------------------------------------------------------------------------
441 # ---------------------------------------------------------------------------
443 test -n "$INTERACTIVE" -a -n "$LOGIN" && {
444 # get current uname and uptime (if exists on this host)
445 # strip any leading whitespace from uname and uptime commands
446 t_uname
="$(test -n "`type -P uname`" && uname -npsr | sed -e 's/^\s+//')"
447 t_uptime
="$(test -n "`type -P uptime`" && uptime | sed -e 's/^\s+//')"
448 if [ -n "$t_uname" ] || [ -n "$t_uptime" ]; then
450 test -n "$t_uname" && echo $t_uname
451 test -n "$t_uptime" && echo $t_uptime
454 unset t_uname t_uptime
457 # vim: ts=4 sts=4 shiftwidth=4 expandtab
458 # vim: foldmethod=expr foldexpr=autofolds#foldexpr(v\:lnum,'sh') foldtext=autofolds#foldtext() foldlevel=2