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