]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - .bashrc
fc357b531b1e08c0f1c1f312ae5f2dc4c9e09934
[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 test -d "$HOME/bin" && PATH="$HOME/bin:$PATH"
79 test -d "$HOME/sbin" && PATH="$HOME/sbin:$PATH"
80
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"
86 }
87
88 # SmartOS pkgin
89 test -d "/opt/local" && PATH="/opt/local/sbin:/opt/local/bin:$PATH"
90 test -d "/opt/local/man" && MANPATH="/opt/local/man:$MANPATH"
91 # SmartOS local files
92 test -d "/opt/custom" && PATH="/opt/custom/sbin:/opt/custom/bin:$PATH"
93 test -d "/opt/custom/man" && MANPATH="/opt/custom/man:$MANPATH"
94 # SmartOS SDC
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"
100
101 # RVM
102 test -d "$HOME/.rvm/bin" && PATH="$PATH:$HOME/.rvm/bin"
103
104 # setup $GOPATH
105 test -d "/usr/local/share/golang" && export GOPATH=/usr/local/share/golang
106 test -n "$GOPATH" && PATH="$PATH:$GOPATH/bin"
107
108 # ---------------------------------------------------------------------------
109 # ENVIRONMENT CONFIGURATION
110 # ---------------------------------------------------------------------------
111
112 # detect interactive shell
113 case "$-" in
114 *i*) INTERACTIVE=yes ;;
115 *) unset INTERACTIVE ;;
116 esac
117
118 # detect login shell
119 case "$0" in
120 -*) LOGIN=yes ;;
121 *) unset LOGIN ;;
122 esac
123
124 # enable en_US locale w/ utf-8 encodings if not already
125 # configured
126 : ${LANG:="en_US.UTF-8"}
127 : ${LANGUAGE:="en"}
128 : ${LC_CTYPE:="en_US.UTF-8"}
129 : ${LC_ALL:="en_US.UTF-8"}
130 export LANG LANGUAGE LC_CTYPE LC_ALL
131
132 # ignore backups, CVS directories, python bytecode, vim swap files
133 FIGNORE="~:CVS:#:.pyc:.swp:.swa:apache-solr-*"
134
135 # history stuff
136 HISTCONTROL=ignoreboth
137 HISTFILESIZE=10000
138 HISTSIZE=10000
139
140 # ---------------------------------------------------------------------------
141 # PAGER / EDITOR
142 # ---------------------------------------------------------------------------
143
144 # see what we have to work with ...
145 HAVE_VIM=$(command -v vim)
146
147 # EDITOR
148 test -n "$HAVE_VIM" &&
149 EDITOR=vim ||
150 EDITOR=vi
151 export EDITOR
152
153 # PAGER
154 if test -n "$(command -v less)" ; then
155 PAGER="less"
156 MANPAGER="less"
157 LESS="-FiRX"
158 export LESS
159 else
160 PAGER=more
161 MANPAGER="$PAGER"
162 fi
163 export PAGER MANPAGER
164
165 # ---------------------------------------------------------------------------
166 # PROMPT
167 # ---------------------------------------------------------------------------
168
169 # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
170
171 if [ "$UID" = 0 ]; then
172 # root
173 PS_C1="\[\033[1;31m\]" # red
174 PS_C2="\[\033[0;37m\]" # grey
175 PS_P="#"
176 else
177 case "$UNAME" in
178 Cygwin)
179 PS_C1="\[\033[0;32m\]" # green
180 PS_C2="\[\033[0;37m\]" # grey
181 ;;
182 Darwin)
183 PS_C1="\[\033[1;97m\]" # white
184 PS_C2="\[\033[0;37m\]" # grey
185 ;;
186 SunOS)
187 PS_C1="\[\033[1;96m\]" # cyan
188 PS_C2="\[\033[0;36m\]" # cyan
189 ;;
190 *)
191 PS_C1="\[\033[1;93m\]" # yellow
192 PS_C2="\[\033[0;33m\]" # brown
193 esac
194 PS_P="\$"
195 fi
196
197 prompt_simple() {
198 unset PROMPT_COMMAND
199 PS1="[\u@\h:\w]\$ "
200 PS2="> "
201 }
202
203 prompt_compact() {
204 unset PROMPT_COMMAND
205 PS1="${PS_C1}${PS_P}\[\033[0m\] "
206 PS2="> "
207 }
208
209 prompt_color() {
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
219 fi
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\] "
221 PS2="> "
222 }
223
224 # ---------------------------------------------------------------------------
225 # ALIASES
226 # ---------------------------------------------------------------------------
227
228 # 'ls' helpers
229 alias ll="ls -l"
230 alias l.="ls -d .*"
231 alias ll.="ls -ld .*"
232 alias lla="ls -la"
233
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"
236
237 # alias 'vi' to 'vim' if Vim is installed
238 test -n "$(type -P vim)" && alias vi='vim'
239
240 # alias csh-style "rebash" to bash equivalent
241 alias rehash="hash -r"
242
243 # svn-wrapper
244 alias svn=svn-wrapper
245
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"
251 fi
252
253 # ---------------------------------------------------------------------------
254 # BASH COMPLETION
255 # ---------------------------------------------------------------------------
256
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
266 do
267 test -f $f && {
268 . $f
269 break
270 }
271 done
272 }
273 unset bash bmajor bminor
274 }
275
276 # restore some key environment variables which may have been unset
277 # by bash_completion script
278 : ${HOME=~}
279 : ${LOGNAME=$(id -un)}
280 : ${UNAME=$(uname)}
281
282 # override and disable tilde expansion
283 _expand() {
284 return 0
285 }
286
287 # ---------------------------------------------------------------------------
288 # LS AND DIRCOLORS
289 # ---------------------------------------------------------------------------
290
291 # we always pass these to ls(1)
292 LS_COMMON="-p"
293
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`
302 }
303 unset dircolors
304
305 # enable color ls output if available
306 test -n "$COLORS" &&
307 LS_COMMON="--color=auto $LS_COMMON"
308
309 # setup the main ls alias if we've established common args
310 test -n "$LS_COMMON" &&
311 alias ls="command ls $LS_COMMON"
312
313 # setup color grep output if available
314 if [ -n "$COLORS" ]; then
315 case "$UNAME" in
316 "SunOS")
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"
319 ;;
320 *)
321 alias grep="command grep --color=always"
322 ;;
323 esac
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'
328 fi
329
330 # ---------------------------------------------------------------------------
331 # MISC FUNCTIONS
332 # ---------------------------------------------------------------------------
333
334 # set 'screen' window title
335 settitle_screen() {
336 printf "\033k%s\033\\" "$@"
337 }
338 # set 'xterm' window title
339 settitle_window() {
340 printf "\033]0;%s\007" "$@"
341 }
342
343 # push SSH public key to another box
344 push_ssh_cert() {
345 local _host
346 test -f ~/.ssh/id_rsa.pub || ssh-keygen -t rsa -b 4096
347 for _host in "$@";
348 do
349 echo $_host
350 ssh $_host 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub
351 done
352 }
353
354 # ---------------------------------------------------------------------------
355 # PATH MANIPULATION FUNCTIONS
356 # ---------------------------------------------------------------------------
357
358 # Usage: pls [<var>]
359 # List path entries of PATH or environment variable <var>.
360 pls () { eval echo \$${1:-PATH} | tr ':' '\n'; }
361
362 # Usage: ppop [-n <num>] [<var>]
363 # Pop <num> entries off the end of PATH or environment variable <var>.
364 ppop () {
365 local n=1
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
370 list=${list%:*}
371 i=$(( i + 1 ))
372 done
373 eval "$var='$list'"
374 }
375
376 # Usage: ppush <path> [<var>]
377 # Push an entry onto the end of PATH or enviroment variable <var>.
378 ppush () {
379 local var=${2:-PATH}
380 local list=$(eval echo \$$var)
381 local i=0 dir=""
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"
387 done
388 eval "$var='$list'"
389 }
390
391 # Usage: pinsert <path> [<var>]
392 # Push an entry onto the start of PATH or enviroment variable <var>.
393 pinsert () {
394 local var=${2:-PATH}
395 local list=$(eval echo \$$var)
396 local i=0 dir=""
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"
402 done
403 eval "$var='$list'"
404 }
405
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' ':')'"; }
409
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.
413 #
414 # Example:
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 ':'; }
418
419 # ---------------------------------------------------------------------------
420 # USER SHELL ENVIRONMENT
421 # ---------------------------------------------------------------------------
422
423 # source ~/.shenv now if it exists
424 test -r ~/.shenv &&
425 . ~/.shenv
426
427 # condense PATH entries
428 PATH=$(puniq "$PATH")
429 MANPATH=$(puniq "$MANPATH")
430
431 # use the color prompt by default when interactive
432 test -n "$PS1" &&
433 prompt_color
434
435 # fzf
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'
438
439 # ---------------------------------------------------------------------------
440 # MOTD / FORTUNE
441 # ---------------------------------------------------------------------------
442
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
449 echo " --"
450 test -n "$t_uname" && echo $t_uname
451 test -n "$t_uptime" && echo $t_uptime
452 echo " --"
453 fi
454 unset t_uname t_uptime
455 }
456
457 # vim: ts=4 sts=4 shiftwidth=4 expandtab
458 # vim: foldmethod=expr foldexpr=autofolds#foldexpr(v\:lnum,'sh') foldtext=autofolds#foldtext() foldlevel=2