-
Notifications
You must be signed in to change notification settings - Fork 6
/
.bashrc
424 lines (365 loc) · 14 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# This file is sourced by all bash shells on startup, whether interactive
# or not. This file *should generate no output* or it will break the
# scp and rcp commands.
[[ -f /etc/bash_completion ]] && source /etc/bash_completion
export INPUTRC="$HOME/.inputrc"
export EDITOR="emacsclient --alternate-editor=emacs"
export GLOBIGNORE='.:..'
export HISTTIMEFORMAT='%c '
export LC_COLLATE="POSIX"
export ACKRC="$HOME/.ackrc"
export ARCHFLAGS="-arch x86_64"
# export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/mysql/lib
# remove the ':' from wordbreaks so we don't have to escape it on teh command line
COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
alias timerake='cat ~/.timerake; echo; time bundle exec rake'
alias ls='ls --color=auto -F -b -T 0'
alias ll='ls -lh --color=auto -F -b -T 0'
alias lobster="lobster.telaranrhiod.com"
#alias su='su -'
alias la='ls -alh'
alias es='eix'
alias eS='eix -S'
#alias sS='screen -S'
#alias sx='screen -x'
alias sls='screen -ls'
alias sw='screen -wipe'
#alias cfup='((svn info &> /dev/null && svn up) || (echo; echo -n "svn repository not detected, use tbz2? [Y,n]: "; read y; [ "$y" == "" -o "$y" == "y" -o "$y" == "Y" ] && (wget -O - http://cf.telaranrhiod.com/files/common/common_files.tbz2 | tar -xjov --no-same-permissions ./))); exec bash'
alias bgup='(wget -O - http://cf.telaranrhiod.com/files/common/backgrounds.tbz2 | tar -xjov --no-same-permissions -C ~/.fluxbox/backgrounds/)'
alias pgrep='pgrep -iL'
which md5 &> /dev/null || alias md5='md5sum'
alias cuwork="cucumber ./features -t @shouldwork"
alias cuwip="cucumber ./features -t @wip"
alias cufail="cucumber ./features -t @shouldfail"
alias cuke="cucumber ./features"
alias sc='script/rails console'
alias sct='RAILS_ENV="test" sc'
alias ss='script/rails server'
alias ackp='ack --pager="less -r"'
alias acki='ack -i'
alias a='acki'
alias hn='hcl note'
alias be='bundle exec'
export CF_TARBALL_BACKUP="true"
export CF_BACKUP_COUNT=5
cfup() {
if [[ "$PWD" != "$HOME" ]]; then
#you aren't in your home directory, prompt for continuing
echo
echo "You do not appear to be in your home directory"
echo -n "Do you wish to continue? [Y,n]: "
read
if ! [[ "$REPLY" == "" || "$REPLY" == "y" || "$REPLY" == "Y" ]]; then
return 1
fi
fi
if svn info &> /dev/null; then
#is repos
if [[ "`svn info | grep '/svn/home/common_files'`" == "" ]]; then
#repos isn't common files
echo
echo "The repository in the current directory does not appear to be the common files."
echo -n "Do you wish to use the tarball? [Y,n]: "
read
if ! [[ "$REPLY" == "" || "$REPLY" == "y" || "$REPLY" == "Y" ]]; then
return 3
fi
else
svn up
if [[ "$?" != "0" || "`svn stat | grep '^C'`" != "" ]]; then
echo
echo "Error updating repository, check above for more info."
return 4
fi
return 0
fi
else
#curr dir isn't repos
echo
echo -n "svn repository not detected, use tbz2? [Y,n]: "
read y
if ! [[ "$y" == "" || "$y" == "y" || "$y" == "Y" ]]; then
return 5
fi
fi
if [[ "$CF_TARBALL_BACKUP" == "true" ]]; then
TEMPDIR=`mktemp -d -t cf_backup_tmp.XXXXXXXXXXXXXX` &&
wget -O - http://cf.telaranrhiod.com/files/common/common_files.tbz2 | tar -xjov --no-same-permissions -C $TEMPDIR ./ &&
date="`date '+%Y-%m-%d--%H-%M-%S'`" &&
version=`echo $CF_RUNNING_VERSION | awk '{print $1}'` &&
backup_path="$HOME/.common_files/backups/${date}--r${version}/" &&
mkdir -p $backup_path &&
echo "moving new files into place and backing up old files to $backup_path" &&
rsync -av -b --backup-dir=$backup_path $TEMPDIR/ ./ &&
rm -rv $TEMPDIR &&
# cp --parents `find | grep '.*.cf.bkp$' | grep -v '\.common_files/backups/'` $backup_path &&
# rm -rf `find | grep '.*.cf.bkp$' | grep -v '\.common_files/backups/'` &&
if [[ "$CF_BACKUP_COUNT" != "" && "$CF_BACKUP_COUNT" -ge "0" ]] &> /dev/null; then
old_backups="`ls $HOME/.common_files/backups/ | sort | head -n -${CF_BACKUP_COUNT}`"
if [[ "$old_backups" != "" ]]; then
echo "Removing old backups ($old_backups) due to a CF_BACKUP_COUNT of $CF_BACKUP_COUNT"
(cd $HOME/.common_files/backups/ && rm -rf $old_backups)
fi
fi
else
wget -O - http://cf.telaranrhiod.com/files/common/common_files.tbz2 | tar -xjov --no-same-permissions ./
fi
if [[ "$?" != "0" ]]; then
echo
echo "Error downloading or extracting tar, check above for more info."
return 2
fi
echo
echo "Performing 'exec bash' to pick up updates."
exec bash
}
sS() {
if [[ "$2" != "" ]]; then
ssh -t $2 screen -S $1
else
screen -S $1
fi
}
sx() {
if [[ "$2" != "" ]]; then
ssh -t $2 screen -x $1
else
screen -x $1
fi
}
psg() {
ps aux | grep "$*" | grep -v "grep .*$*"
}
known_hosts_temp_file=~/.ssh/temp_known_hosts_file
set_temp_known_host() {
rm ${known_hosts_temp_file} &> /dev/null
# we use xxx because we want this to fail to login, just set the known host
ssh -o 'StrictHostKeyChecking=no' -o 'PreferredAuthentications="xxx"' -o "UserKnownHostsFile=${known_hosts_temp_file}" $* &> /dev/null
}
pssh() {
set_temp_known_host $*
cat ${known_hosts_temp_file} >> ~/.ssh/known_hosts
ssh $*
}
tssh() {
set_temp_known_host $*
ssh -o "UserKnownHostsFile=${known_hosts_temp_file}" $*
}
vncvia() {
if [[ "$*" == "" ]]; then
echo "usage: vncvia [user@]remotehost [other vnc options...]"
else
echo "vncviewer -via $* localhost"
vncviewer -via $* localhost
fi
}
repeat() {
local i n
n=$1
shift
for ((i=1; i<=n; i++))
do "$@"
done
}
alias cd='pushd -n $PWD &> /dev/null; cd'
# cf_cd() {
# pushd -n "$PWD" &> /dev/null
# /usr/bin/cd "$@" || popd -n &> /dev/null
# }
# alias cd='cf_cd' # put this below cf_cd so when cf_cd is read 'cd' isn't expanded making it recursive
#min seconds between notifications of new common files.
export CF_TIME_BETWEEN_NOTIFICATIONS=86400
last_notified_date_path="$HOME/.common_files/.out_of_date_last_notified_date"
notification_message_path="$HOME/.common_files/.out_of_date_notification_message"
cf_date_check_notify() {
#if it isn't the same day as the last time we told you and
#we found that you were out of date last time you logged in we're going to tell you about it now.
last_date=0
[ -f $last_notified_date_path ] && last_date=`cat $last_notified_date_path`
let "new_date = $last_date + $CF_TIME_BETWEEN_NOTIFICATIONS"
if [[ "$new_date" -lt "`date '+%s'`" ]]; then
[ -f $notification_message_path ] && cat $notification_message_path && echo "`date '+%s'`" > $last_notified_date_path
fi
}
cf_get_latest_local_version() {
#get your current revision number
if which git &> /dev/null; then
my_rev=`(git --git-dir $HOME log -1 --pretty=format:"%H %ad") 2> /dev/null`
fi
if [[ "$my_rev" == "" ]]; then
#couldn't get version from svn so we'll try .common_files/latest_revision.txt
# This means you're probably using the tarball.
my_rev=`cat "$HOME/.common_files/.latest_revision" 2> /dev/null`
fi
if [[ "$my_rev" == "" ]]; then
return 1
fi
CF_LOCAL_LATEST_VERSION=$my_rev
return 0
}
cf_get_latest_local_version
CF_RUNNING_VERSION="$CF_LOCAL_LATEST_VERSION"
CF_LOCAL_LATEST_VERSION="$CF_RUNNING_VERSION";
cf_check_for_updates() {
#first paren executes rest in subshell so we don't see the output from the job finishing
#second peren and its & lump this block together and execute it in the background to it happens asynchronously and we don't hold up shell startup
((
#checking to see if you're up to date
#make sure you have curl and svn
if which git curl &> /dev/null; then
#get the latest revision number, this should just be an integer.
latest=`curl --max-time 5 -sL http://cf.telaranrhiod.com/files/common/latest_revision.txt`
#make sure curl returned successfully
if [[ "$?" == "0" ]]; then
# my_rev="`cf_get_latest_local_version`"
cf_get_latest_local_version
my_rev=$CF_LOCAL_LATEST_VERSION
#check if you're up to date
latest_hash=`echo $latest | awk '{print $1}'`
my_hash=`echo $my_rev | awk '{print $1}'`
if [[ "$latest_hash" != "$my_hash" ]]; then
#if not, create the .out_of_date file with the appropriate message so next time you start a terminal we can alert you.
echo "Not on latest revision of common_files. Latest: $latest, yours: $my_rev" > $notification_message_path
else
#if you're up to date we don't need this file
[ -f $notification_message_path ] && rm $notification_message_path
fi
fi
fi
) &)
}
#min seconds between checking for new common files.
export CF_TIME_BETWEEN_UPDATES=86400
cf_date_check_for_updates() {
last_checked_for_updates_date_path="$HOME/.common_files/tmp/.last_checked_date"
last_date=0
[ -f $last_checked_for_updates_date_path ] && last_date=`cat $last_checked_for_updates_date_path`
let "new_date = $last_date + $CF_TIME_BETWEEN_UPDATES"
if [[ "$new_date" -lt "`date '+%s'`" ]]; then
cf_check_for_updates
echo "`date '+%s'`" > $last_checked_for_updates_date_path
fi
}
cf_prompt_command() {
# old_hist_time_format=$HISTTIMEFORMAT
# HISTTIMEFORMAT='%s '
# hist_cmd=`history 1`
# HISTTIMEFORMAT=$old_hist_time_format
# history 10
# begin_hist=`begin_hist_grep $hist_cmd`
# history -p '!!:1'
# set | grep sleep
# foo=">${hist_cmd/$begin_hist $BASH_COMMAND/}--$begin_hist--$BASH_COMMAND<"
# echo $foo
# [[ "${hist_cmd/$begin_hist $BASH_COMMAND/}" == "" ]] && echo "hooray"
# history 10
history -a
# history 10
((cf_date_check_for_updates) &)
cf_date_check_notify
[[ "$CF_RUNNING_VERSION" != "$CF_LOCAL_LATEST_VERSION" ]] && exec bash
# cf_long_running_task_check
[[ "`declare -f cf_user_prompt_hook`" != "" ]] && cf_user_prompt_hook
}
## This is copied from OSX's /etc/bashrc and is used to allow Terminal to reopen
## old tabs in the same directory. It's used below in setting PROMPT_COMMAND.
# Tell the terminal about the working directory at each prompt.
if [ "$TERM_PROGRAM" == "Apple_Terminal" ] && [ -z "$INSIDE_EMACS" ]; then
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL,
# including the host name to disambiguate local vs.
# remote connections. Percent-escape spaces.
local SEARCH=' '
local REPLACE='%20'
local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
printf '\e]7;%s\a' "$PWD_URL"
}
PROMPT_COMMAND="update_terminal_cwd; $PROMPT_COMMAND"
fi
#export LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;
#35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;
#31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;
#31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jpg=01;
#35:*.gif=01;35:*.bmp=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;
#35:*.tif=01;35:*.png=01;35:*.mpg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;
#35:*.dl=01;35::'
#Keeps the terminal from doing funky wrapping stuff
shopt -s checkwinsize
#append to history file on exit instead of overwriting
shopt -s histappend
#don't try to tab complete an empty line
shopt -s no_empty_cmd_completion
#correct minor directory misspellings
shopt -s cdspell
#let * match files beginning with '.' but since GLOBIGNORE is set above it won't match '.' or '..'
shopt -s dotglob
FG_BLACK="\[\033[01;30m\]"
FG_WHITE="\[\033[01;37m\]"
FG_RED="\[\033[01;31m\]"
FG_GREEN="\[\033[01;32m\]"
FG_BLUE="\[\033[01;34m\]"
NO_COLOR="\[\e[0m\]"
WHOAMI="`/usr/bin/whoami`"
#make eterm into xterm for emacs/ssh purposes
if [[ "$TERM" = "eterm-color" ]]; then
export CF_REAL_TERM=$TERM
export TERM="xterm-color"
fi
#build PS1
#don't set PS1 for dumb terminals
if [[ "$TERM" != 'dumb' ]] && [[ -n "$BASH" ]]; then
PS1=''
#don't modify titlebar on console
[[ "$TERM" != 'linux' && "$CF_REAL_TERM" != "eterm-color" ]] && PS1="${PS1}\[\e]2;\u@\H:\W\a"
#use a red $ if you're root, white otherwise
if [[ $WHOAMI = "root" ]]; then
#red hostname
PS1="${PS1}${FG_RED}\u@"
else
#green user@hostname
PS1="${PS1}${FG_GREEN}\u@"
fi
GIT_PS1_SHOWDIRTYSTATE=1
#working dir basename and prompt
PS1="${PS1}\h ${FG_RED}\$(__git_ps1 "[%s]") ${FG_BLUE}\W ${FG_BLUE}\$ ${NO_COLOR}"
fi
#make eterm into xterm for emacs/ssh purposes
if [[ "$TERM" = "eterm-color" ]]; then
export TERM="xterm-color"
fi
if [[ $WHOAMI = 'root' ]]; then
export PATH="/bin:/sbin:/usr/bin:/usr/sbin:${ROOTPATH}"
else
export PATH="/bin:/usr/bin:${PATH}"
fi
[[ "$HOME" == "" ]] && export HOME=`grep -e "^[^:]*\:[^:]*\:$UID\:" /etc/passwd | awk -F ':' '{print $6}'`
export BC_ENV_ARGS="$HOME/.bcrc"
#Extend command history
export HISTSIZE=5000000
#Repeated commands are only stored once
export HISTCONTROL=ignoreboth
#save history with each command
export PROMPT_COMMAND='[[ "`set | grep -E \"update_terminal_cwd \(\)\"`" != "" ]] && update_terminal_cwd; [[ "`set | grep -E \"cf_prompt_command \(\)\"`" != "" ]] && cf_prompt_command'
if ! shopt -q login_shell; then
if [ -f /usr/bin/keychain ]; then
[ -f ~/.ssh/id_dsa ] && /usr/bin/keychain --noask ~/.ssh/id_dsa &> /dev/null
[ -f ~/.ssh/id_rsa ] && /usr/bin/keychain --noask ~/.ssh/id_rsa &> /dev/null
fi
[ -f ~/.keychain/$HOSTNAME-sh ] && source ~/.keychain/$HOSTNAME-sh > /dev/null &> /dev/null
fi
# load any OS specific changes we've made
[ -f ~/.common_files/cf.`uname -s`.conf ] && . ~/.common_files/cf.`uname -s`.conf
# load any cf libs
for i in ~/.common_files/lib/*; do
[ -e "$i" ] && . $i
done
# Load user specific configuration that is checked into the repo.
[ -f ~/.common_files/cf.`whoami`.conf ] && . ~/.common_files/cf.`whoami`.conf
# Last, but not least, source a configuration file so there's an easy place for
# users to make configuration changes from the default. This file isn't checked
# in so it can contain secret info.
[ -f ~/.common_files/cf.conf ] && . ~/.common_files/cf.conf
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad
[ -f ~/.bundler-exec.sh ] && source ~/.bundler-exec.sh