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