]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - .bashrc
.bashrc: Better color support, add Cygwin support
[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 [ "$UNAME" = "Darwin" ]; then
20 # Fink init, for OSX
21 test -r /sw/bin/init.sh &&
22 source /sw/bin/init.sh
23 fi
24
25 # ----------------------------------------------------------------------
26 # SHELL OPTIONS
27 # ----------------------------------------------------------------------
28
29 # bring in system bashrc
30 test -r /etc/bashrc &&
31 . /etc/bashrc
32
33 # notify of bg job completion immediately
34 set -o notify
35
36 # shell opts. see bash(1) for details
37 shopt -s cdspell >/dev/null 2>&1
38 shopt -s extglob >/dev/null 2>&1
39 shopt -s histappend >/dev/null 2>&1
40 shopt -s hostcomplete >/dev/null 2>&1
41 shopt -s interactive_comments >/dev/null 2>&1
42 shopt -u mailwarn >/dev/null 2>&1
43 shopt -s no_empty_cmd_completion >/dev/null 2>&1
44
45 # don't check for new mail
46 unset MAILCHECK
47
48 # disable core dumps
49 ulimit -S -c 0
50
51 # default umask
52 umask 0022
53
54 # ----------------------------------------------------------------------
55 # PATH
56 # ----------------------------------------------------------------------
57
58 # we want the various sbins on the path along with /usr/local/bin
59 PATH="$PATH:/usr/local/sbin:/usr/sbin:/sbin"
60 PATH="/usr/local/bin:$PATH"
61
62 # put ~/bin on PATH if you have it
63 test -d "$HOME/bin" &&
64 PATH="$HOME/bin:$PATH"
65
66 # ----------------------------------------------------------------------
67 # ENVIRONMENT CONFIGURATION
68 # ----------------------------------------------------------------------
69
70 # detect interactive shell
71 case "$-" in
72 *i*) INTERACTIVE=yes ;;
73 *) unset INTERACTIVE ;;
74 esac
75
76 # detect login shell
77 case "$0" in
78 -*) LOGIN=yes ;;
79 *) unset LOGIN ;;
80 esac
81
82 # enable en_US locale w/ utf-8 encodings if not already
83 # configured
84 : ${LANG:="en_US.UTF-8"}
85 : ${LANGUAGE:="en"}
86 : ${LC_CTYPE:="en_US.UTF-8"}
87 : ${LC_ALL:="en_US.UTF-8"}
88 export LANG LANGUAGE LC_CTYPE LC_ALL
89
90 # ignore backups, CVS directories, python bytecode, vim swap files
91 FIGNORE="~:CVS:#:.pyc:.swp:.swa:apache-solr-*"
92 HISTCONTROL=ignoreboth
93
94 # ----------------------------------------------------------------------
95 # PAGER / EDITOR
96 # ----------------------------------------------------------------------
97
98 # See what we have to work with ...
99 HAVE_VIM=$(command -v vim)
100
101 # EDITOR
102 test -n "$HAVE_VIM" &&
103 EDITOR=vim ||
104 EDITOR=vi
105 export EDITOR
106
107 # PAGER
108 if test -n "$(command -v less)" ; then
109 PAGER="less -FirSwX"
110 MANPAGER="less -FiRswX"
111 else
112 PAGER=more
113 MANPAGER="$PAGER"
114 fi
115 export PAGER MANPAGER
116
117 # ----------------------------------------------------------------------
118 # PROMPT
119 # ----------------------------------------------------------------------
120
121 # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
122 SGR_RED="\[\033[1;31m\]"
123 SGR_GREEN="\[\033[0;32m\]"
124 SGR_BROWN="\[\033[0;33m\]"
125 SGR_YELLOW="\[\033[1;33m\]"
126 SGR_GREY="\[\033[0;37m\]"
127 SGR_WHITE="\[\033[1;37m\]"
128 SGR_BLUE="\[\033[1;34m\]"
129 SGR_RESET="\[\033[0m\]"
130
131 if [ "$UID" = 0 ]; then
132 # root
133 PS_COLOR1="${SGR_RED}"
134 PS_COLOR2=""
135 PS_P="#"
136 else
137 if [ "$UNAME" = "Cygwin" ]; then
138 PS_COLOR1="${SGR_GREEN}"
139 PS_COLOR2=""
140 else
141 PS_COLOR1="${SGR_BROWN}"
142 PS_COLOR2="${SGR_YELLOW}"
143 fi
144 PS_P="\$"
145 fi
146
147 prompt_simple() {
148 unset PROMPT_COMMAND
149 PS1="[\u@\h:\w]\$ "
150 PS2="> "
151 }
152
153 prompt_compact() {
154 unset PROMPT_COMMAND
155 PS1="${PS_COLOR1}${PS_P}${SGR_RESET} "
156 PS2="> "
157 }
158
159 prompt_color() {
160 PS1="[${PS_COLOR1}\u@\h${SGR_RESET}:${PS_COLOR2}\w${SGR_RESET}]${PS_P} "
161 PS2="> "
162 }
163
164 # ----------------------------------------------------------------------
165 # MACOS X / DARWIN SPECIFIC
166 # ----------------------------------------------------------------------
167
168 if [ "$UNAME" = Darwin ]; then
169 # put ports on the paths if /opt/local exists
170 test -x /opt/local -a ! -L /opt/local && {
171 PORTS=/opt/local
172
173 # setup the PATH and MANPATH
174 PATH="$PORTS/bin:$PORTS/sbin:$PATH"
175 MANPATH="$PORTS/share/man:$MANPATH"
176
177 # nice little port alias
178 alias port="sudo nice -n +18 $PORTS/bin/port"
179 }
180 fi
181
182 # ----------------------------------------------------------------------
183 # ALIASES / FUNCTIONS
184 # ----------------------------------------------------------------------
185
186 # alias 'vi' to 'vim' if Vim is installed
187 vim="$(type -P vim)"
188 test -n "$vim" && {
189 alias vi='vim'
190 }
191 unset vim
192
193 # disk usage with human sizes and minimal depth
194 alias du1='du -h --max-depth=1'
195 alias fn='find . -name'
196 alias hi='history | tail -20'
197
198 # ----------------------------------------------------------------------
199 # BASH COMPLETION
200 # ----------------------------------------------------------------------
201
202 test -z "$BASH_COMPLETION" && {
203 bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
204 test -n "$PS1" && test $bmajor -gt 1 && {
205 # search for a bash_completion file to source
206 for f in /usr/local/etc/bash_completion \
207 /usr/pkg/etc/bash_completion \
208 /opt/local/etc/bash_completion \
209 /etc/bash_completion
210 do
211 test -f $f && {
212 . $f
213 break
214 }
215 done
216 }
217 unset bash bmajor bminor
218 }
219
220 # override and disable tilde expansion
221 _expand()
222 {
223 return 0
224 }
225
226 # ----------------------------------------------------------------------
227 # LS AND DIRCOLORS
228 # ----------------------------------------------------------------------
229
230 # we always pass these to ls(1)
231 unset LS_COMMON
232 ## OS-specific options
233 #case "$UNAME" in
234 # "Linux" ) LS_COMMON="--color=auto";;
235 # "Cygwin" ) LS_COMMON="--color=auto";;
236 # "Darwin" ) LS_COMMON="--color=auto";;
237 #esac
238
239 # if the dircolors utility is available, set that up for ls
240 dircolors="$(type -P gdircolors dircolors | head -1)"
241 test -n "$dircolors" && {
242 COLORS=/etc/DIR_COLORS
243 test -e "/etc/DIR_COLORS.$TERM" && COLORS="/etc/DIR_COLORS.$TERM"
244 test -e "$HOME/.dircolors" && COLORS="$HOME/.dircolors"
245 test ! -e "$COLORS" && COLORS=
246 eval `$dircolors --sh $COLORS`
247 }
248 unset dircolors
249
250 # enable color ls output if available
251 test -n "$COLORS" &&
252 LS_COMMON="--color=auto $LS_COMMON"
253
254 # setup the main ls alias if we've established common args
255 test -n "$LS_COMMON" &&
256 alias ls="command ls $LS_COMMON"
257
258 # these use the ls aliases above
259 alias ll="ls -l"
260 alias l.="ls -d .*"
261
262 # setup color grep output if available
263 test -n "$COLORS" &&
264 alias grep="command grep --color=auto"
265
266 # --------------------------------------------------------------------
267 # MISC COMMANDS
268 # --------------------------------------------------------------------
269
270 # push SSH public key to another box
271 push_ssh_cert() {
272 local _host
273 test -f ~/.ssh/id_dsa.pub || ssh-keygen -t dsa
274 for _host in "$@";
275 do
276 echo $_host
277 ssh $_host 'cat >> ~/.ssh/authorized_keys2' < ~/.ssh/id_dsa.pub
278 done
279 }
280
281 # --------------------------------------------------------------------
282 # PATH MANIPULATION FUNCTIONS
283 # --------------------------------------------------------------------
284
285 # Usage: pls [<var>]
286 # List path entries of PATH or environment variable <var>.
287 pls () { eval echo \$${1:-PATH} |tr : '\n'; }
288
289 # Usage: pshift [-n <num>] [<var>]
290 # Shift <num> entries off the front of PATH or environment var <var>.
291 # with the <var> option. Useful: pshift $(pwd)
292 pshift () {
293 local n=1
294 [ "$1" = "-n" ] && { n=$(( $2 + 1 )); shift 2; }
295 eval "${1:-PATH}='$(pls |tail -n +$n |tr '\n' :)'"
296 }
297
298 # Usage: ppop [-n <num>] [<var>]
299 # Pop <num> entries off the end of PATH or environment variable <var>.
300 ppop () {
301 local n=1 i=0
302 [ "$1" = "-n" ] && { n=$2; shift 2; }
303 while [ $i -lt $n ]
304 do eval "${1:-PATH}='\${${1:-PATH}%:*}'"
305 i=$(( i + 1 ))
306 done
307 }
308
309 # Usage: prm <path> [<var>]
310 # Remove <path> from PATH or environment variable <var>.
311 prm () { eval "${2:-PATH}='$(pls $2 |grep -v "^$1\$" |tr '\n' :)'"; }
312
313 # Usage: punshift <path> [<var>]
314 # Shift <path> onto the beginning of PATH or environment variable <var>.
315 punshift () { eval "${2:-PATH}='$1:$(eval echo \$${2:-PATH})'"; }
316
317 # Usage: puniq [<path>]
318 # Remove duplicate entries from a PATH style value while retaining
319 # the original order. Use PATH if no <path> is given.
320 #
321 # Example:
322 # $ puniq /usr/bin:/usr/local/bin:/usr/bin
323 # /usr/bin:/usr/local/bin
324 puniq () {
325 echo "$1" | tr : '\n' | nl | sort -u -k 2,2 | sort -n |
326 cut -f 2- | tr '\n' : | sed -e 's/:$//' -e 's/^://'
327 }
328
329 # -------------------------------------------------------------------
330 # USER SHELL ENVIRONMENT
331 # -------------------------------------------------------------------
332
333 # source ~/.shenv now if it exists
334 test -r ~/.shenv &&
335 . ~/.shenv
336
337 # condense PATH entries
338 PATH=$(puniq $PATH)
339 MANPATH=$(puniq $MANPATH)
340
341 # Use the color prompt by default when interactive
342 test -n "$PS1" &&
343 prompt_color
344
345 # vim: ts=4 sts=4 shiftwidth=4 expandtab