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