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