]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - .bashrc
.vimrc: Add more Command-T key-mappings
[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 -Fir"
133 MANPAGER="less -FiRs"
134 else
135 PAGER=more
136 MANPAGER="$PAGER"
137 fi
138 export PAGER MANPAGER
139
140 # Ack
141 ACK_PAGER="$PAGER"
142 ACK_PAGER_COLOR="$PAGER"
143
144 # ----------------------------------------------------------------------
145 # PROMPT
146 # ----------------------------------------------------------------------
147
148 # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
149
150 if [ "$UID" = 0 ]; then
151 # root
152 PS_C1="\[\033[1;31m\]" # red
153 PS_C2="\[\033[0;37m\]" # grey
154 PS_P="#"
155 else
156 case "$UNAME" in
157 Cygwin)
158 PS_C1="\[\033[0;32m\]" # green
159 PS_C2="\[\033[0;37m\]" # grey
160 ;;
161 Darwin)
162 PS_C1="\[\033[1;97m\]" # white
163 PS_C2="\[\033[0;37m\]" # grey
164 ;;
165 SunOS)
166 PS_C1="\[\033[1;96m\]" # cyan
167 PS_C2="\[\033[0;36m\]" # cyan
168 ;;
169 *)
170 PS_C1="\[\033[1;93m\]" # yellow
171 PS_C2="\[\033[0;33m\]" # brown
172 esac
173 PS_P="\$"
174 fi
175
176 prompt_simple() {
177 unset PROMPT_COMMAND
178 PS1="[\u@\h:\w]\$ "
179 PS2="> "
180 }
181
182 prompt_compact() {
183 unset PROMPT_COMMAND
184 PS1="${PS_C1}${PS_P}\[\033[0m\] "
185 PS2="> "
186 }
187
188 prompt_color() {
189 PS1="\[\033[0;90m\][${PS_C1}\u@\h\[\033[0m\]\[\033[0;90m\]:${PS_C2}\w\[\033[0;90m\]]${PS_P}\[\033[0m\] "
190 PS2="> "
191 }
192
193 # ----------------------------------------------------------------------
194 # MACOS X / DARWIN SPECIFIC
195 # ----------------------------------------------------------------------
196
197 if [ "$UNAME" = Darwin ]; then
198 # put ports on the paths if /opt/local exists
199 test -x /opt/local -a ! -L /opt/local && {
200 PORTS=/opt/local
201
202 # setup the PATH and MANPATH
203 PATH="$PORTS/bin:$PORTS/sbin:$PATH"
204 MANPATH="$PORTS/share/man:$MANPATH"
205
206 # nice little port alias
207 alias port="sudo nice -n +18 $PORTS/bin/port"
208 }
209 fi
210
211 # ----------------------------------------------------------------------
212 # ALIASES / FUNCTIONS
213 # ----------------------------------------------------------------------
214
215 # alias 'vi' to 'vim' if Vim is installed
216 vim="$(type -P vim)"
217 test -n "$vim" && {
218 alias vi='vim'
219 }
220 unset vim
221
222 # disk usage with human sizes and minimal depth
223 alias du1='du -h --max-depth=1'
224 alias fn='find . -name'
225 alias hi='history | tail -20'
226
227 # For Solaris, use GNU versions of grep and sed
228 if [ "$UNAME" = SunOS ]; then
229 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep"
230 test -x /usr/gnu/bin/sed && alias sed="/usr/gnu/bin/sed"
231 fi
232
233 # Alias csh-style "rebash" to bash equivalent
234 alias rehash="hash -r"
235
236 # ----------------------------------------------------------------------
237 # BASH COMPLETION
238 # ----------------------------------------------------------------------
239
240 test -z "$BASH_COMPLETION" && {
241 bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
242 test -n "$PS1" && test $bmajor -gt 1 && {
243 # search for a bash_completion file to source
244 for f in /usr/local/etc/bash_completion \
245 /usr/pkg/etc/bash_completion \
246 /opt/local/etc/bash_completion \
247 /etc/bash_completion
248 do
249 test -f $f && {
250 . $f
251 break
252 }
253 done
254 }
255 unset bash bmajor bminor
256 }
257
258 # override and disable tilde expansion
259 _expand() {
260 return 0
261 }
262
263 # ----------------------------------------------------------------------
264 # LS AND DIRCOLORS
265 # ----------------------------------------------------------------------
266
267 # we always pass these to ls(1)
268 unset LS_COMMON
269 ## OS-specific options
270 #case "$UNAME" in
271 # "Linux" ) LS_COMMON="--color=auto";;
272 # "Cygwin" ) LS_COMMON="--color=auto";;
273 # "Darwin" ) LS_COMMON="--color=auto";;
274 #esac
275
276 # if the dircolors utility is available, set that up for ls
277 dircolors="$(type -P gdircolors dircolors | head -1)"
278 test -n "$dircolors" && {
279 COLORS=/etc/DIR_COLORS
280 test -e "/etc/DIR_COLORS.$TERM" && COLORS="/etc/DIR_COLORS.$TERM"
281 test -e "$HOME/.dircolors" && COLORS="$HOME/.dircolors"
282 test ! -e "$COLORS" && COLORS=
283 eval `$dircolors --sh $COLORS`
284 }
285 unset dircolors
286
287 # enable color ls output if available
288 test -n "$COLORS" &&
289 LS_COMMON="--color=auto $LS_COMMON"
290
291 # setup the main ls alias if we've established common args
292 test -n "$LS_COMMON" &&
293 alias ls="command ls $LS_COMMON"
294
295 # these use the ls aliases above
296 alias ll="ls -l"
297 alias l.="ls -d .*"
298 alias ll.="ls -ld .*"
299 alias lla="ls -la"
300
301 # setup color grep output if available
302 if [ -n "$COLORS" ]; then
303 case "$UNAME" in
304 "SunOS")
305 # For Solaris, use the GNU version of grep for color support
306 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep --color=auto"
307 ;;
308 *)
309 alias grep="command grep --color=auto"
310 ;;
311 esac
312 fi
313
314 # --------------------------------------------------------------------
315 # MISC COMMANDS
316 # --------------------------------------------------------------------
317
318 # push SSH public key to another box
319 push_ssh_cert() {
320 local _host
321 test -f ~/.ssh/id_rsa.pub || ssh-keygen -t rsa -b 4096
322 for _host in "$@";
323 do
324 echo $_host
325 ssh $_host 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub
326 done
327 }
328
329 # --------------------------------------------------------------------
330 # PATH MANIPULATION FUNCTIONS
331 # --------------------------------------------------------------------
332
333 # Usage: pls [<var>]
334 # List path entries of PATH or environment variable <var>.
335 pls () { eval echo \$${1:-PATH} |tr : '\n'; }
336
337 # Usage: pshift [-n <num>] [<var>]
338 # Shift <num> entries off the front of PATH or environment var <var>.
339 # with the <var> option. Useful: pshift $(pwd)
340 pshift () {
341 local n=1
342 [ "$1" = "-n" ] && { n=$(( $2 + 1 )); shift 2; }
343 eval "${1:-PATH}='$(pls |tail -n +$n |tr '\n' :)'"
344 }
345
346 # Usage: ppop [-n <num>] [<var>]
347 # Pop <num> entries off the end of PATH or environment variable <var>.
348 ppop () {
349 local n=1 i=0
350 [ "$1" = "-n" ] && { n=$2; shift 2; }
351 while [ $i -lt $n ]
352 do eval "${1:-PATH}='\${${1:-PATH}%:*}'"
353 i=$(( i + 1 ))
354 done
355 }
356
357 # Usage: prm <path> [<var>]
358 # Remove <path> from PATH or environment variable <var>.
359 prm () { eval "${2:-PATH}='$(pls $2 |grep -v "^$1\$" |tr '\n' :)'"; }
360
361 # Usage: punshift <path> [<var>]
362 # Shift <path> onto the beginning of PATH or environment variable <var>.
363 punshift () { eval "${2:-PATH}='$1:$(eval echo \$${2:-PATH})'"; }
364
365 # Usage: ppush <path> [<var>]
366 ppush () { eval "${2:-PATH}='$(eval echo \$${2:-PATH})':$1"; }
367
368 # Usage: puniq [<path>]
369 # Remove duplicate entries from a PATH style value while retaining
370 # the original order. Use PATH if no <path> is given.
371 #
372 # Example:
373 # $ puniq /usr/bin:/usr/local/bin:/usr/bin
374 # /usr/bin:/usr/local/bin
375 puniq () {
376 echo "$1" | tr : '\n' | nl | sort -u -k 2,2 | sort -n |
377 cut -f 2- | tr '\n' : | sed -e 's/:$//' -e 's/^://'
378 }
379
380 # -------------------------------------------------------------------
381 # USER SHELL ENVIRONMENT
382 # -------------------------------------------------------------------
383
384 # source ~/.shenv now if it exists
385 test -r ~/.shenv &&
386 . ~/.shenv
387
388 # condense PATH entries
389 PATH=$(puniq "$PATH")
390 MANPATH=$(puniq "$MANPATH")
391
392 # Use the color prompt by default when interactive
393 test -n "$PS1" &&
394 prompt_color
395
396 # -------------------------------------------------------------------
397 # MOTD / FORTUNE
398 # -------------------------------------------------------------------
399
400 test -n "$INTERACTIVE" -a -n "$LOGIN" && {
401 # Get current uname and uptime (if exists on this host)
402 # Strip any leading whitespace from uname and uptime commands
403 t_uname="$(test -n "`type -P uname`" && uname -npsr | sed -e 's/^\s+//')"
404 t_uptime="$(test -n "`type -P uptime`" && uptime | sed -e 's/^\s+//')"
405 if [ -n "$t_uname" ] || [ -n "$t_uptime" ]; then
406 echo " --"
407 test -n "$t_uname" && echo $t_uname
408 test -n "$t_uptime" && echo $t_uptime
409 echo " --"
410 fi
411 unset t_uname t_uptime
412 }
413
414 # vim: ts=4 sts=4 shiftwidth=4 expandtab