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