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