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