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