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