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