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