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