-
Notifications
You must be signed in to change notification settings - Fork 41
/
bashrc
409 lines (364 loc) · 11.3 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
#!/bin/bash
# .bashrc
# === INTRO ===
#
# Welcome to the most awesome bash startup script you'll ever see.
# It tries to be BSD and GNU compatable, which means it works on
# your MAC, cygwin, and sandbox server. It also tries to be safe
# to add as a source to your .bashrc and .bash_profile. I usually
# source this file from both.
# It is split into three files
# (1) Generic bash stuff
# * This is the stuff you would want to show and run anywhere
# you ever use bash: Work, home, your personal web-site, share
# with the internet, etc
# (2) Per group files
# * This is the file you would want to always run in whatever
# group you're in. For example, at work on every server or at
# school on every server, these commands make sense.
# (3) Per hostname files
# * These are the commands you want to run only on the specified
# host
#
# This also sources executable files, inside 'bin' split the same way
#
# === FILE STRUCTURE ===
#
# The file is organized into the following parts
#
# (1) Source global definitions
# (2) Create global shell variables
# (3) Create BSD vs GNU compatable variables
# (4) Set shell options
# (5) Set aliases
# (6) Set autocomplete options
# (7) Create useful utility bash functions()
# (8) Setup global path
# (9) Setup the prompt
# (10) Source per group file
# (11) Source per hostname file
# (12) Clean up PATH
#
#
# === HOW TO INSTAL ===
# Execute
# ./install_bashrc
#
# === HOW TO MAINTAIN ===
#
# It is split into two .git repositories. The first is public and you can get
# that code by executing:
# git clone git://github.com/cep21/jackbash.git
#
# The second is private. You should make your own .git repository inside $HOME/.bash/group
# Put private information there, like your email address or SSH keys
#
# Source global definitions
#GLOBAL_BASH_DEF='/etc/bashrc'
#if [ -f $GLOBAL_BASH_DEF ]
#then
# source $GLOBAL_BASH_DEF
#fi;
# Create a scrubed hostname
HOSTNAME_SCRUB=$(hostname | sed -e s/[^a-zA-Z0-9_]//g)
# Global variables
# Sometimes EDITOR require a complete path
VIM_PATH=$(which vim &> /dev/null)
if [ $? -eq 0 ]; then
VIM_PATH=$(which vim)
export EDITOR=$VIM_PATH
export SVN_EDITOR=$VIM_PATH
export GIT_EDITOR=$VIM_PATH
fi
unset VIM_PATH
LESS_PATH=$(which less &> /dev/null)
if [ $? -eq 0 ]; then
LESS_PATH=$(which less)
export PAGER=$LESS_PATH
fi
unset LESS_PATH
export LS_COLORS="no=00:\
fi=00:\
di=01;36:\
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:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:\
*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:\
*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:\
*.ogg=01;35:*.mp3=01;35:*.wav=01;35:\
";
#export GREP_OPTIONS='--color=auto'
export GIT_CEILING_DIRECTORIES
GIT_CEILING_DIRECTORIES=$(echo $HOME | sed 's#/[^/]*$##') # Either /home(linux) or /Users(mac)
export HISTTIMEFORMAT="%y/%m/%d %T "
export HISTFILESIZE=1000000000
export HISTSIZE=1000000
export PROMPT_COMMAND='history -a'
export BROWSER='firefox'
export LANG='en_US.UTF-8'
#export LANG='C' # Testing: Try out the C locale
if [ -f "$HOME/.inputrc" ]; then
export INPUTRC="$HOME/.inputrc"
fi;
export MAN_AUTOCOMP_FILE
MAN_AUTOCOMP_FILE="/tmp/man_completes_$(whoami)"
# Compatability options
# The BSD sed on mac uses -E, while the GNU one on linux uses -r
(echo '' | sed -r /GG/g &> /dev/null)
if [ $? -eq "0" ]; then
export SED_EXT='-r'
else
export SED_EXT='-E'
fi
# GNU vs BSD ls for color
(ls --color=tty &> /dev/null)
if [ $? -eq 0 ]; then
export LS_COLOR='--color=tty'
else
export LS_COLOR='-G'
fi;
#GNU vs BSD top command line arguments
# Delay updates by 10 sec and sort by CPU
(man top 2>&1 | grep Linux> /dev/null)
if [ $? -eq 0 ]; then
export TOP_OPTIONS='-c -d10'
else
export TOP_OPTIONS='-s10 -ocpu'
fi;
# Options
shopt -s checkwinsize
shopt -s histappend # Append to history rather than overwrite
# Aliases
alias ls='ls -h $LS_COLOR'
alias la='ls -lah $LS_COLOR'
alias ll='ls -lh $LS_COLOR'
alias ssh='ssh -A'
alias g='git'
alias top='top $TOP_OPTIONS'
alias rcopy='rsync -az --stats --progress --delete'
alias ..='cl ..'
alias trim_whitespace="sed -i 's/[ \t]*$//' "
alias sush='ssh -l root'
alias http_headers='curl -svo /dev/null'
# Auto completion
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
complete -cf sudo
complete -cf which
#autocomplete ssh commands with the hostname
complete -W "$(echo `cat ~/.ssh/known_hosts 2> /dev/null | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" ssh
# Maven auto completion
. ~/.bash/config/mvn_bash_completion.bash
# For _get_cword from https://code.google.com/p/bash-completion-lib/source/browse/trunk/include/_get_cword?spec=svn85&r=85
. ~/.bash/config/get_cword.bash
# autocomplete man commands
function listmans_raw() {
local manpath_func
which manpath &> /dev/null
if [ $? -eq 0 ]; then
manpath_func='manpath'
else
manpath_func='man -W 2> /dev/null'
fi;
for dir in $($manpath_func | /usr/bin/tr ':' '\n'); do
find "${dir}" ! -type d -name "*.*" 2>/dev/null | sed -e 's#/.*/##g' | sed -e 's#.[^.]*$##g' | sed -e 's#\.[0123456789].*##g'
done
}
function regen_man_args() {
listmans_raw | sort -u > "$MAN_AUTOCOMP_FILE"
}
function listmans() {
if [ ! -f "$MAN_AUTOCOMP_FILE" ]; then
regen_man_args
fi;
cat "$MAN_AUTOCOMP_FILE"
}
complete -W "$(listmans)" man
#### RANDOM FUNCTIONS #####
# awesome! CD AND LA. I never use 'cd' anymore...
function cl(){ cd "$@" && la; }
# Two standard functions to change $PATH
add_path() { export PATH="$PATH:$1"; }
add_pre_path() { export PATH="$1:$PATH"; }
# Misc utilities:
# Repeat a command N times. You can do something like
# repeat 3 echo 'hi'
function repeat()
{
local i max
max=$1; shift;
for ((i=1; i <= max ; i++)); do
eval "$@";
done
}
# Lets you ask a command. Returns '0' on 'yes'
# ask 'Do you want to rebase?' && git svn rebase || echo 'Rebase aborted'
function ask()
{
echo -n "$@" '[y/n] ' ; read -r ans
case "$ans" in
y*|Y*) return 0 ;;
*) return 1 ;;
esac
}
#Simple blowfish encryption
function blow()
{
[ -z "$1" ] && echo 'Encrypt: blow FILE' && return 1
openssl bf-cbc -salt -in "$1" -out "$1.bf"
}
function fish()
{
test -z "$1" -o -z "$2" && echo \
'Decrypt: fish INFILE OUTFILE' && return 1
openssl bf-cbc -d -salt -in "$1" -out "$2"
}
# Extract based upon file ext
function ex() {
if [ -f "$1" ] ; then
case "$1" in
*.tar.bz2) tar xvjf "$1" ;;
*.tar.gz) tar xvzf "$1" ;;
*.tar.xz) tar xvf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xvf "$1" ;;
*.tbz2) tar xvjf "$1" ;;
*.tgz) tar xvzf "$1" ;;
*.jar) jar xf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "'$1' cannot be extracted via >extract<" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Compress with tar + bzip2
function bz2 () {
tar cvpjf "$1.tar.bz2" "$1"
}
function myip () {
# GNU vs BSD hostname
(hostname -i &> /dev/null)
if [ $? -eq 0 ]; then
hostname -i
else
# default to eth0 IP, for MAC
ipconfig getifaddr en0
fi;
}
function dumptcp() {
# TCPDUMP all the data on port $1 into rotated files /tmp/results. Note,
# this can get VERY large
sudo /usr/sbin/tcpdump -i any -s0 tcp port "$1" -A -w /tmp/results -C 100
}
# anyvi <file>
# run EDITOR on a script no matter where it is
function anyvi()
{
if [ -e "$1" ] || [ -f "$1" ]; then
$EDITOR "$1"
else
$EDITOR "$(which "$1")"
fi
}
complete -cf anyvi #autocomplete the anyvi command
# Grep for a process while at the same time ignoring the grep that
# you're running. For example
# ps awxxx | grep java
# will show "grep java", which is probably not what you want
function psgrep(){
OUTFILE=$(mktemp /tmp/psgrep.XXXXX)
ps awxxx > "$OUTFILE"
grep "$@" "$OUTFILE"
rm "$OUTFILE"
}
# Quick and dirty calculator. Goes right to ruby and lets you print out
# the results of math operations, or other ruby expressions
function calc(){ ruby -e "puts $*"; }
add_path "$HOME/bin"
add_path "$HOME/.bash/bin"
add_path "$HOME/.bash/group/bin"
# Set up git completion
source "$HOME/.bash/config/git-completion.bash"
source "$HOME/.bash/config/git-prompt.sh"
function _ps1_kube_(){
[[ "${KUBE_PS1_ENABLED}" == "off" ]] && return
kube_ps1 $@
}
###### PROMPT ######
# Set up the prompt colors
source "$HOME/.bash/term_colors"
PROMPT_COLOR=$G
if [ ${UID} -eq 0 ]; then
PROMPT_COLOR=$R ### root is a red color prompt
fi
# I like this prompt for a few reasons:
# (1) The time shows when each command was executed, when I get back to my terminal
# (2) Git information really important for git users
# (3) Prompt color is red if I'm root
# (4) The last part of the prompt can copy/paste directly into an SCP command
# (5) Color highlight out the current directory because it's important
# (6) The export PS1 is simple to understand!
# (7) If the prev command error codes, the prompt '>' turns red
export PS1="$Y\t$N $W"'$(__git_ps1 "(%s) ")''$(_ps1_kube_)'"$N$PROMPT_COLOR\u@\H$N:$C\w$N\n"'$CURSOR_PROMPT '
# TODO: Find out why my $R and $N shortcuts don't work here!!!
export PROMPT_COMMAND='if [ $? -ne 0 ]; then CURSOR_PROMPT=`bad_prompt`; else CURSOR_PROMPT="<"; fi;'
# Attach k8s ns and context to prompt if we have kubectl installed
# Must do this after setting PROMPT_COMMAND
KUBE_PATH=$(which kubectl &> /dev/null)
if [ $? -eq 0 ]; then
source "$HOME/.bash/config/kube-ps1.sh"
source <(kubectl completion bash) # setup autocomplete in bash into the current shell, bash-completion package should be installed first.
else
KUBE_PS1_ENABLED=off
fi
unset KUBE_PATH
# Add autocomplete for google cloud
GCLOUD_PATH=$(which gcloud &> /dev/null)
if [ $? -eq 0 ]; then
source ~/.bash/config/gcloud-bash-completion.bash
fi
unset GCLOUD_PATH
HELM_PATH=$(which helm &> /dev/null)
if [ $? -eq 0 ]; then
source <(helm completion bash)
fi
unset HELM_PATH
function bad_prompt(){
# red='\033[0;31m'
# NC='\033[0m' # No Color
# echo -e "${red}>${NC}"
echo -e ">"
}
# Get a simple kubectl shell up and running
function kshell() {
kubectl run my-shell --rm -i --tty --image ubuntu -- bash
}
#### Source group
GROUP_FILE="$HOME/.bash/group/group.bash"
if [ -f "$GROUP_FILE" ]; then
source "$GROUP_FILE"
fi;
##### Source the correct per-host file
PERHOST_FILE="$HOME/.bash/group/hostnames/$HOSTNAME_SCRUB.bash"
if [ -f "$PERHOST_FILE" ]; then
source "$PERHOST_FILE"
fi;
# remove duplicate path entries and preserve PATH order
export PATH
PATH=$(echo "$PATH" | awk -F: '
{ start=0; for (i = 1; i <= NF; i++) if (!($i in arr) && $i) {if (start!=0) printf ":";start=1; printf "%s", $i;arr[$i]}; }
END { printf "\n"; } ')
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH