-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbashrc
More file actions
440 lines (375 loc) · 10 KB
/
bashrc
File metadata and controls
440 lines (375 loc) · 10 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# Source global definitions
[ -z "$PS1" ] && return
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
Blue='\[\e[01;34m\]'
White='\[\e[01;37m\]'
Red='\[\e[01;31m\]'
Green='\[\e[01;32m\]'
Reset='\[\e[00m\]'
FancyX='\342\234\227'
Checkmark='\342\234\223'
parse_git_branch() {
b=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
if [[ $? == 0 ]]; then
echo -n "$Blue[$Red$b"
if [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working tree clean" ]]; then
echo -n "$Blue] [${Red}$FancyX"
else
echo -n "$Blue] [${Green}$Checkmark"
fi
echo "$Blue]"
fi
}
git_blame_stat() {
find . -name "*.$1" | grep -v vendor -v dependencies | xargs -n 1 git blame -w | \
sed -e 's/[^(]* (//' -e 's/ *20.*//' | tr '[:upper:]' '[:lower:]' | \
sort | uniq -c
}
# User specific aliases and functions
# stolen from
# https://stackoverflow.com/questions/1862510/how-can-the-last-commands-wall-time-be-put-in-the-bash-prompt
function timer_now {
date +%s%N
}
function timer_start {
timer_start=${timer_start:-$(timer_now)}
}
function timer_stop {
local delta_us=$((($(timer_now) - $timer_start) / 1000))
local us=$((delta_us % 1000))
local ms=$(((delta_us / 1000) % 1000))
local s=$(((delta_us / 1000000) % 60))
local m=$(((delta_us / 60000000) % 60))
local h=$((delta_us / 3600000000))
# Goal: always show around 3 digits of accuracy
if ((h > 0)); then timer_show=${h}h${m}m
elif ((m > 0)); then timer_show=${m}m${s}s
elif ((s >= 10)); then timer_show=${s}.$((ms / 100))s
elif ((s > 0)); then timer_show=${s}.$(printf %03d $ms)s
elif ((ms >= 100)); then timer_show=${ms}ms
elif ((ms > 0)); then timer_show=${ms}.$((us / 100))ms
else timer_show=${us}us
fi
unset timer_start
}
set_prompt () {
Last_Command=$? # Must come first!
# Add a bright white exit status for the last command
PS1=''
# If it was successful, print a green check mark. Otherwise, print
# a red X.
if [[ $Last_Command == 0 ]]; then
PS1+="$Green[$Checkmark "
else
PS1+="$Red[$FancyX "
fi
# Add the ellapsed time and current date
timer_stop
PS1+="$Last_Command $timer_show]
"
# If root, just print the host in red. Otherwise, print the current user
# and host in green.
if [[ $EUID == 0 ]]; then
PS1+="$Red[$White\\u$Red@\\h$Red] "
else
PS1+="$Blue[$Red\\u@\\h$Blue] "
fi
PS1+="$Blue[$Red\\t$Blue] "
# Print the working directory and prompt marker in blue, and reset
# the text color to the default.
PS1+="$Blue[$Red\\w$Blue] $(parse_git_branch)
$Blue"'\[\033k\033\\\]'"\\\$$Reset"
history -a; history -c; history -r
}
# unified bash history
export HISTSIZE=""
shopt -s histappend
trap 'timer_start' DEBUG
PROMPT_COMMAND='set_prompt'
export SVN_EDITOR=vim
export EDITOR=vim
export PATH=$PATH:$HOME/.local/bin
# aliases
alias gp='grep -rin '
alias scen='screen'
alias ll='ls -hl --color '
alias la='ls -hal --color '
alias vs='vim -S .session.vim'
alias clera='clear'
alias claer='clear'
alias atp-get='apt-get'
alias atp='apt'
alias cd..='cd ..'
alias ..='cd ..'
alias mkae='make'
alias maek='make'
alias mke='make'
alias git_tree='git log --all --graph --decorate --oneline --simplify-by-decoration'
alias gtree='git_tree'
alias gpull='git pull'
alias gpsh='git push'
alias gst='git status'
alias gdiff='git diff'
alias gco='git checkout'
alias gmerge='git merge'
alias gd='git diff'
bind "set completion-ignore-case on"
# Delete duplicate blank lines, style via astyle
style() {
sed -i '/^[ \t]*/{N; /^[ \t]*\n$/d}' $@
astyle $@
}
# down - find subdir, cd to it
down() {
dest=`find . -name "$1" -type d -print -quit 2>/dev/null`
if [ "$dest" != "" ]; then
echo $dest
cd $dest
else
echo "$1 not found"
fi
}
# up - cd up some number of dirs
up() {
if [ "$#" != "0" ]; then
count=0
dest=''
while [ "$count" != "$1" ]; do
dest="$dest../"
count=`expr $count + 1`
if [ "$count" == "100" ]; then
echo "Recursion limit reached"
break
fi
done
cd $dest
else
cd ..
fi
}
# upto - cd up to a dir in your path
upto() {
OIFS=$IFS
IFS='/'
found=0
for d in $(pwd); do
if [ "$d" = "$1" ]; then
found=1
fi
done
if [ "$found" == "1" ]; then
while [ "${PWD##*/}" != "$1" ]; do
cd ..
done
else
echo "Directory $1 not in path: $(pwd)"
fi
IFS=$OIFS
}
md() {
if [ $# -lt 1 ]; then
date '+%Y.%m.%d'
else
vim `date '+%Y.%m.%d'`$1
fi
}
mdt() {
if [ $# -lt 1 ]; then
date '+%Y.%m.%d.%H:%M:%S'
else
vim `date '+%Y.%m.%d.%H:%M:%S'`$1
fi
}
histogram() {
if [ $# -lt 1 ]
then
echo "Usage: histogram [file] [field] [precision]"
return 0
fi
field=1
if [ $# -gt 1 ]
then
field=$2
fi
if [ $# -gt 2 ]
then
precision="."
for i in `seq 1 1 $3`
do
precision=$precision.
done
cat $1 | sort -k $field -n -t ',' | cut -d ',' -f $field | sed -e "s/\($precision\).*/\1/" | uniq -c
else
cat $1 | sort -k $field -n -t ',' | cut -d ',' -f $field | uniq -c
fi
}
hex() {
xxd -ps
}
fromhex() {
xxd -r -p
}
rand64() {
if [ $# -lt 1 ]; then
head -c 10 /dev/urandom | base64 -w 0 | sed -e 's/=//g'
else
head -c $1 /dev/urandom | base64 -w 0 | sed -e 's/=//g'
fi
}
tmpl() {
if [ "$#" = "0" ]; then
echo "usage: template <file> print all templates in file"
echo "usage: template <key> <val> replace key with val. template stdin to stdout"
elif [ "$#" = "1" ]; then
cat $1 | sed -e 's/}}"/}}"\n/' | grep "{{[^}]*}}" | sed -e 's/[^{}]*{{ *//' -e 's/ *}}[^{}]*//' | sort | uniq
elif [ "$#" = "2" ]; then
sed -e "s/{{ *$1 *}}/$2/g"
else
echo "incorrect number of args"
fi
}
gerp() {
if [ "$#" = "2" ]; then
if [ "$1" = "go" ]; then
grep -in "$2" `find . -name "*.$1"` | grep -v Godeps | grep -v vendor | less -F --no-init
elif [ "$1" = "js" ]; then
grep -in "$2" `find . -name "*.$1" | grep -v node_modules | grep -v .chunk.js` | less -F --no-init
elif [ "$1" = "php" ]; then
grep -in "$2" `find . -name "*.$1" | grep -v vendor` | less -F --no-init
else
grep -in "$2" `find . -name "*.$1"` | less -F --no-init
fi
else
echo "usage: gerp <filextension> <pattern>"
fi
}
gerpf() {
gerp "$@" | sed -e 's/:.*//' | sort | uniq | less -F --no-init
}
docker_ip() {
if [ $# -lt 2 ]; then
docker inspect --format '{{ .NetworkSettings.IPAddress }}' $1
else
HOST=$2
IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' $2)
cat /etc/hosts | grep -v $HOST > newhosts
echo $IP $HOST >> newhosts
sudo mv newhosts /etc/hosts
fi
}
docker_cleanup() {
DEADINSTANCES=$(docker ps -a -q -f status=exited)
if [ "$DEADINSTANCES" != "" ]; then
docker rm $DEADINSTANCES
fi
DANGLINGIMAGES=$(docker images | grep "^<none>" | awk "{print $3}")
if [ "$DANGLINGIMAGES" != "" ]; then
docker rmi $DANGLINGIMAGES
fi
}
docker_kill() {
INSTANCES=$(docker ps -a -q)
if [ "$INSTANCES" != "" ]; then
docker rm -f $INSTANCES
fi
}
swp() {
FILES=$(find . -name ".*.sw[po]")
if [ "$FILES" == "" ]; then
echo "No swap files found."
return
fi
echo $FILES
echo "Delete? (y/n)"
read YN
if [ "$YN" == "y" ] || [ "$YN" == "Y" ]; then
rm `find . -name ".*.sw[po]"`
fi
}
mv() {
if [ "$#" -ne 1 ]; then
command mv "$@"
return
fi
read -ei "$1" newfilename
command mv -v -- "$1" "$newfilename"
}
zoom_in() {
SIZE=`grep 'FontName' ~/.config/xfce4/terminal/terminalrc | cut -d' ' -f 2`
NEWSIZE=$((SIZE + 2))
echo $NEWSIZE
REGEXPR='s/FontName.*/FontName=Monospace '$NEWSIZE'/g'
sed -i "$REGEXPR" ~/.config/xfce4/terminal/terminalrc
}
zoom_set() {
SIZE=`grep 'FontName' ~/.config/xfce4/terminal/terminalrc | cut -d' ' -f 2`
NEWSIZE=$1
echo $NEWSIZE
REGEXPR='s/FontName.*/FontName=Monospace '$NEWSIZE'/g'
sed -i "$REGEXPR" ~/.config/xfce4/terminal/terminalrc
}
zoom_out() {
SIZE=`grep 'FontName' ~/.config/xfce4/terminal/terminalrc | cut -d' ' -f 2`
NEWSIZE=$((SIZE - 2))
if [ $NEWSIZE -lt 6 ]; then
NEWSIZE=6
fi
echo $NEWSIZE
REGEXPR='s/FontName.*/FontName=Monospace '$NEWSIZE'/g'
sed -i "$REGEXPR" ~/.config/xfce4/terminal/terminalrc
}
go_list_deps() {
go list -f '{{ range .Imports }}{{ . }}{{ "\n" }}{{ end }}' ./... | sort | uniq
}
play_result() {
if [ $? -eq 0 ]; then
aplay /usr/share/sounds/sound-icons/xylofon.wav
aplay /usr/share/sounds/sound-icons/xylofon.wav
aplay /usr/share/sounds/sound-icons/xylofon.wav
else
aplay /usr/share/sounds/sound-icons/klavichord-4.wav
aplay /usr/share/sounds/sound-icons/klavichord-4.wav
aplay /usr/share/sounds/sound-icons/klavichord-4.wav
fi
}
urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
base64pad() {
read DATA
REM=$(expr ${#DATA} % 4)
if [ "$REM" == "2" ]; then
echo "$DATA=="
elif [ "$REM" == "3" ]; then
echo "$DATA="
else
echo "$DATA"
fi
}
jwsq() {
IFS='.' read PROTECTED PAYLOAD SIG
PROTECTED=$(echo $PROTECTED | base64pad | base64 -d)
PAYLOAD=$(echo $PAYLOAD | base64pad | base64 -d)
echo $PAYLOAD
echo '{"protected":'"$(echo $PROTECTED)"', "payload":'"$(echo $PAYLOAD)"', "signature":"'"$(echo $SIG)"'"}' | jq $@
}
jwesq() {
IFS='.' read ENCHEAD SIGHEAD SIG IV CIPHER TAG
ENCHEAD=$(echo $ENCHEAD | base64pad | base64 -d)
SIGHEAD=$(echo $SIGHEAD | base64pad | base64 -d)
echo '{"encHead":'$(echo $ENCHEAD)', "sigHead":'$(echo $SIGHEAD)',"sig":"'"$(echo $SIG)"'", "iv":"'"$(echo $IV)"'", "cipher":"'$(echo $CIPHER)'", "tag":"'$(echo $TAG)'"}' | jq $@
}
if [ -f $HOME/.dotfiles/bash/git-completion.bash ]; then
. $HOME/.dotfiles/bash/git-completion.bash
fi
if [ -f $HOME/.dotfiles/bash/kubectl-completion.bash ]; then
. $HOME/.dotfiles/bash/kubectl-completion.bash
fi
if [ -f $HOME/.dotfiles/bash/docker-completion.bash ]; then
. $HOME/.dotfiles/bash/docker-completion.bash
fi
complete -W "\`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|$)' ?akefile | sed 's/[^a-zA-Z0-9_.-]*$//'\`" make
if [ -f $HOME/.bashrc_local ]; then
. $HOME/.bashrc_local
fi
export SASS_PATH=./node_modules