]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - .bashrc
bin/diff-filter: Support session-level disable flag
[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 # make sure $MANPATH has some sane defaults
71 MANPATH="/usr/share/man:/usr/local/share/man:$MANPATH"
72
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"
76
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"
80
81 # macOS homebrew: include non-prefixed coreutils
82 if [ -d "/usr/local/opt/coreutils/libexec" ]; then
83 path_start="$path_start:/usr/local/opt/coreutils/libexec/gnubin"
84 manpath_start="$manpath_start:/usr/local/opt/coreutils/libexec/gnuman"
85 fi
86
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"
91 fi
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"
96 fi
97 # SmartOS: SDC
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"
101 fi
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"
106 fi
107
108 # python pip --user
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"
113 fi
114 done
115 unset python pybin
116
117 PATH="$path_start:$PATH:$path_end"
118 MANPATH="$manpath_start:$MANPATH:$manpath_end"
119 unset path_start path_end manpath_start manpath_end
120
121 # ---------------------------------------------------------------------------
122 # ENVIRONMENT CONFIGURATION
123 # ---------------------------------------------------------------------------
124
125 # detect interactive shell
126 case "$-" in
127 *i*) INTERACTIVE=yes ;;
128 *) unset INTERACTIVE ;;
129 esac
130
131 # detect login shell
132 case "$0" in
133 -*) LOGIN=yes ;;
134 *) unset LOGIN ;;
135 esac
136
137 # enable en_US locale w/ utf-8 encodings if not already
138 # configured
139 : ${LANG:="en_US.UTF-8"}
140 : ${LANGUAGE:="en"}
141 : ${LC_CTYPE:="en_US.UTF-8"}
142 : ${LC_ALL:="en_US.UTF-8"}
143 export LANG LANGUAGE LC_CTYPE LC_ALL
144
145 # ignore backups, CVS directories, python bytecode, vim swap files
146 FIGNORE="~:CVS:#:.pyc:.swp:.swa:apache-solr-*"
147
148 # history stuff
149 HISTCONTROL=ignoreboth
150 HISTFILESIZE=10000
151 HISTSIZE=10000
152
153 # ---------------------------------------------------------------------------
154 # PAGER / EDITOR
155 # ---------------------------------------------------------------------------
156
157 # see what we have to work with ...
158 HAVE_VIM=$(command -v vim)
159
160 # EDITOR
161 test -n "$HAVE_VIM" &&
162 EDITOR=vim ||
163 EDITOR=vi
164 export EDITOR
165
166 # PAGER
167 if test -n "$(command -v less)" ; then
168 PAGER="less"
169 MANPAGER="less"
170 LESS="-FiRX"
171 export LESS
172 else
173 PAGER=more
174 MANPAGER="$PAGER"
175 fi
176 export PAGER MANPAGER
177
178 # ---------------------------------------------------------------------------
179 # PROMPT
180 # ---------------------------------------------------------------------------
181
182 # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
183
184 if [ "$UID" = 0 ]; then
185 # root
186 PS_C1="\[\033[1;31m\]" # red
187 PS_C2="\[\033[0;37m\]" # grey
188 PS_P="#"
189 else
190 case "$UNAME" in
191 Cygwin)
192 PS_C1="\[\033[0;32m\]" # green
193 PS_C2="\[\033[0;37m\]" # grey
194 ;;
195 Darwin)
196 PS_C1="\[\033[1;97m\]" # white
197 PS_C2="\[\033[0;37m\]" # grey
198 ;;
199 SunOS)
200 PS_C1="\[\033[1;96m\]" # cyan
201 PS_C2="\[\033[0;36m\]" # cyan
202 ;;
203 *)
204 PS_C1="\[\033[1;93m\]" # yellow
205 PS_C2="\[\033[0;33m\]" # brown
206 esac
207 PS_P="\$"
208 fi
209
210 prompt_simple() {
211 unset PROMPT_COMMAND
212 PS1="[\u@\h:\w]\$ "
213 PS2="> "
214 }
215
216 prompt_compact() {
217 unset PROMPT_COMMAND
218 PS1="${PS_C1}${PS_P}\[\033[0m\] "
219 PS2="> "
220 }
221
222 prompt_color() {
223 # if git and the git bash_completion scripts are installed, use __git_ps1() to show current branch info.
224 # never show branch info for $HOME (dotfiles) repo.
225 # use the following to exclude a given repo: `git config --local --bool --add bash.hidePrompt true`
226 if [ -n "$(type -P git)" -a "$(type -t __git_ps1)" = "function" ]; then
227 PS_GIT='$(test -n "$(__git_ps1 %s)" &&
228 test "$(git rev-parse --show-toplevel)" != "$HOME" &&
229 test "$(git config --bool bash.hidePrompt)" != "true" &&
230 __git_ps1 "{\[\033[0;40;36m\]%s\[\033[0;90m\]}")'
231 export GIT_PS1_SHOWDIRTYSTATE=1
232 fi
233 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\] "
234 PS2="> "
235 }
236
237 # ---------------------------------------------------------------------------
238 # ALIASES
239 # ---------------------------------------------------------------------------
240
241 # 'ls' helpers
242 alias ll="ls -l"
243 alias l.="ls -d .*"
244 alias ll.="ls -ld .*"
245 alias lla="ls -la"
246
247 # use 'git diff --no-index' as a prettier 'diff' alternative (if available)
248 test -n "$(type -P git)" && alias diff="git diff --no-index"
249
250 # alias 'vi' to 'vim' if Vim is installed
251 test -n "$(type -P vim)" && alias vi='vim'
252
253 # alias csh-style "rebash" to bash equivalent
254 alias rehash="hash -r"
255
256 # svn-wrapper
257 alias svn=svn-wrapper
258
259 if [ "$UNAME" = SunOS ]; then
260 # Solaris: use GNU versions of coreutils
261 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep"
262 test -x /usr/gnu/bin/sed && alias sed="/usr/gnu/bin/sed"
263 test -x /usr/gnu/bin/awk && alias awk="/usr/gnu/bin/awk"
264 fi
265
266 # ---------------------------------------------------------------------------
267 # BASH COMPLETION
268 # ---------------------------------------------------------------------------
269
270 test -z "$BASH_COMPLETION" && {
271 bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
272 test -n "$PS1" && test $bmajor -gt 1 && {
273 # search for a bash_completion file to source
274 for f in /usr/local/etc/bash_completion \
275 /usr/pkg/etc/bash_completion \
276 /opt/local/etc/bash_completion \
277 /etc/bash_completion \
278 /etc/bash/bash_completion
279 do
280 test -f $f && {
281 . $f
282 break
283 }
284 done
285 }
286 unset bash bmajor bminor
287 }
288
289 # restore some key environment variables which may have been unset
290 # by bash_completion script
291 : ${HOME=~}
292 : ${LOGNAME=$(id -un)}
293 : ${UNAME=$(uname)}
294
295 # override and disable tilde expansion
296 _expand() {
297 return 0
298 }
299
300 # ---------------------------------------------------------------------------
301 # LS AND DIRCOLORS
302 # ---------------------------------------------------------------------------
303
304 # we always pass these to ls(1)
305 LS_COMMON="-p"
306
307 # if the dircolors utility is available, set that up for ls
308 dircolors="$(type -P gdircolors dircolors | head -1)"
309 test -n "$dircolors" && {
310 COLORS=/etc/DIR_COLORS
311 test -e "/etc/DIR_COLORS.$TERM" && COLORS="/etc/DIR_COLORS.$TERM"
312 test -e "$HOME/.dircolors" && COLORS="$HOME/.dircolors"
313 test ! -e "$COLORS" && COLORS=
314 eval `$dircolors --sh $COLORS`
315 }
316 unset dircolors
317
318 # enable color ls output if available
319 test -n "$COLORS" &&
320 LS_COMMON="--color=auto $LS_COMMON"
321
322 # setup the main ls alias if we've established common args
323 test -n "$LS_COMMON" &&
324 alias ls="command ls $LS_COMMON"
325
326 # setup color grep output if available
327 if [ -n "$COLORS" ]; then
328 case "$UNAME" in
329 "SunOS")
330 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep --color=always"
331 test -x /opt/local/bin/grep && alias grep="/opt/local/bin/grep --color=always"
332 ;;
333 *)
334 alias grep="command grep --color=always"
335 ;;
336 esac
337 # older versions of grep only support a singular highlight color
338 export GREP_COLOR='1;32'
339 # newer versions of grep have more flexible color configuration
340 export GREP_COLORS='fn=36:ln=33:ms=1;32'
341 fi
342
343 # ---------------------------------------------------------------------------
344 # MISC FUNCTIONS
345 # ---------------------------------------------------------------------------
346
347 # set 'screen' window title
348 settitle_screen() {
349 printf "\033k%s\033\\" "$@"
350 }
351 # set 'xterm' window title
352 settitle_window() {
353 printf "\033]0;%s\007" "$@"
354 }
355
356 # push SSH public key to another box
357 push_ssh_cert() {
358 local _host
359 test -f ~/.ssh/id_rsa.pub || ssh-keygen -t rsa -b 4096
360 for _host in "$@";
361 do
362 echo $_host
363 ssh $_host 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub
364 done
365 }
366
367 # ---------------------------------------------------------------------------
368 # PATH MANIPULATION FUNCTIONS
369 # ---------------------------------------------------------------------------
370
371 # Usage: pls [<var>]
372 # List path entries of PATH or environment variable <var>.
373 pls () { eval echo \$${1:-PATH} | tr ':' '\n'; }
374
375 # Usage: ppop [-n <num>] [<var>]
376 # Pop <num> entries off the end of PATH or environment variable <var>.
377 ppop () {
378 local n=1
379 [ "$1" = "-n" ] && { n=$2; shift 2; }
380 local i=0 var=${1:-PATH}
381 local list=$(eval echo \$$var)
382 while [ $i -lt $n ]; do
383 list=${list%:*}
384 i=$(( i + 1 ))
385 done
386 eval "$var='$list'"
387 }
388
389 # Usage: ppush <path> [<var>]
390 # Push an entry onto the end of PATH or enviroment variable <var>.
391 ppush () {
392 local var=${2:-PATH}
393 local list=$(eval echo \$$var)
394 local i=0 dir=""
395 IFS=':' read -a dirs <<< "$1"
396 for ((i=0; i<${#dirs[@]}; i++)); do
397 dir=$(eval echo "${dirs[$i]}")
398 [ ! -d "$dir" ] && continue
399 [ -n "$list" ] && list="$list:$dir" || list="$dir"
400 done
401 eval "$var='$list'"
402 }
403
404 # Usage: pinsert <path> [<var>]
405 # Push an entry onto the start of PATH or enviroment variable <var>.
406 pinsert () {
407 local var=${2:-PATH}
408 local list=$(eval echo \$$var)
409 local i=0 dir=""
410 IFS=':' read -a dirs <<< "${1}"
411 for ((i=${#dirs[@]}-1; i>=0; i--)); do
412 dir=$(eval echo "${dirs[$i]}")
413 [ ! -d "$dir" ] && continue
414 [ -n "$list" ] && list="$dir:$list" || list="$dir"
415 done
416 eval "$var='$list'"
417 }
418
419 # Usage: prm <path> [<var>]
420 # Remove <path> from PATH or environment variable <var>.
421 prm () { eval "${2:-PATH}='$(pls $2 | grep -v "^$1\$" | tr '\n' ':')'"; }
422
423 # Usage: puniq [<pathlist>]
424 # Remove duplicate entries from a PATH style value while retaining
425 # the original order. Use PATH if no <path> is given.
426 #
427 # Example:
428 # $ puniq /usr/bin:/usr/local/bin:/usr/bin
429 # /usr/bin:/usr/local/bin
430 puniq () { echo "$1" | tr ':' '\n' | grep -v "^$" | nl | sort -u -k 2,2 | sort -n | cut -f 2- | paste -s -d ':' -; }
431
432 # ---------------------------------------------------------------------------
433 # USER SHELL ENVIRONMENT
434 # ---------------------------------------------------------------------------
435
436 # source ~/.shenv now if it exists
437 test -r ~/.shenv &&
438 . ~/.shenv
439
440 # condense PATH entries
441 PATH=$(puniq "$PATH")
442 MANPATH=$(puniq "$MANPATH")
443
444 # use the color prompt by default when interactive
445 test -n "$PS1" &&
446 prompt_color
447
448 # fzf
449 export FZF_DEFAULT_COMMAND='rg --files --no-ignore --hidden --follow -g "!{.git,node_modules,*.swp,.venv}/*" 2> /dev/null'
450 export FZF_DEFAULT_OPTS='--bind J:down,K:up --reverse --ansi --multi'
451
452 # ---------------------------------------------------------------------------
453 # MOTD / FORTUNE
454 # ---------------------------------------------------------------------------
455
456 test -n "$INTERACTIVE" -a -n "$LOGIN" && {
457 # get current uname and uptime (if exists on this host)
458 # strip any leading whitespace from uname and uptime commands
459 t_uname="$(test -n "`type -P uname`" && uname -npsr | sed -e 's/^\s+//')"
460 t_uptime="$(test -n "`type -P uptime`" && uptime | sed -e 's/^\s+//')"
461 if [ -n "$t_uname" ] || [ -n "$t_uptime" ]; then
462 echo " --"
463 test -n "$t_uname" && echo $t_uname
464 test -n "$t_uptime" && echo $t_uptime
465 echo " --"
466 fi
467 unset t_uname t_uptime
468 }
469
470 # vim: ts=4 sts=4 shiftwidth=4 expandtab
471 # vim: foldmethod=expr foldexpr=autofolds#foldexpr(v\:lnum,'sh') foldtext=autofolds#foldtext() foldlevel=2