-
Notifications
You must be signed in to change notification settings - Fork 1
/
vi-bashrc
444 lines (405 loc) · 10.5 KB
/
vi-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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# Vi mode bash key bindings
#stty -ixon
#set -o vi
#bind '"\C-a": beginning-of-line'
#bind '"\C-e": end-of-line'
#bind '"\C-k": kill-line'
#bind '"\C-p": previous-history'
#bind '"\C-n": next-history'
#bind '"\C-l": clear-screen'
#bind '"\C-w": backward-kill-word'
# Fall back to emacs mode now
set -o emacs
bind '"\C-q": forward-search-history'
bind '"\C-f": forward-word'
bind '"\C-b": backward-word'
# alias -----------------------------------------------------------------------
alias grep='grep --color'
# Interprate color
alias less='less -R'
alias xda='tmux detach -a'
alias sudo='sudo -E '
alias cddl='cd ~/Downloads'
alias emacs='emacs -nw'
alias xs='tmux-cssh'
alias vboxm="VBoxManage"
alias vml="VBoxManage list vms"
alias vmlr="VBoxManage list runningvms"
alias vim='nvim'
# general export --------------------------------------------------------------
export EDITOR='nvim'
# less colors -----------------------------------------------------------------
export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[38;5;016m\E[48;5;220m' # begin standout-mode - info box
export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline
# PS1 -------------------------------------------------------------------------
export PS1="\[$(tput bold)\]\[$(tput setaf 2)\]\u@\[$(tput setaf 4)\]\h \[$(tput setaf 5)\]\D{%F %T} \[$(tput setaf 3)\]\W \[$(tput setaf 2)\]\\$ \[$(tput sgr0)\]"
# Bash history ----------------------------------------------------------------
export HISTCONTROL=ignoreboth:erasedups # no duplicate entries
export HISTSIZE=100000 # big big history
export HISTFILESIZE=100000 # big big history
export HISTTIMEFORMAT="%d/%m/%y %T "
shopt -s histappend # append to history, don't overwrite it
# Save and reload the history after each command finishes
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
# FZF settings ----------------------------------------------------------------
# Use ~~ as the trigger sequence instead of the default ..
export FZF_COMPLETION_TRIGGER='..'
# Options to fzf command
export FZF_COMPLETION_OPTS='+c -x'
# Use fd (https://github.com/sharkdp/fd) instead of the default find
# command for listing path candidates.
# - The first argument to the function ($1) is the base path to start traversal
# - See the source code (completion.{bash,zsh}) for the details.
_fzf_compgen_path() {
fd --hidden --follow --exclude ".git" . "$1"
}
# Use fd to generate the list for directory completion
_fzf_compgen_dir() {
fd --type d --hidden --follow --exclude ".git" . "$1"
}
# Include ~/bin if it's not in the PATH variable
[[ "$PATH" == *~/bin* ]] || export PATH="~/bin:$PATH"
# Virtualenvwrapper
# load virtualenvwrapper for python (after custom PATHs)
venvwrap="virtualenvwrapper.sh"
/usr/bin/which $venvwrap > /dev/null 2>&1
if [ $? -eq 0 ]; then
venvwrap=`/usr/bin/which $venvwrap`
source $venvwrap
fi
# Platform specific settings --------------------------------------------------
platform=$(uname)
case $platform in
Darwin)
alias ls='ls -G'
alias ftpup='sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist'
alias ftpdown='sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist'
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib
if [ `ps aux |grep ssh-agent | grep -v grep | wc -l` -lt 1 ]; then
eval `ssh-agent -s` > /dev/null
fi
ssh-add > /dev/null 2>&1
;;
Linux)
alias ls='ls --color'
alias docker='sudo docker'
;;
esac
# functions -------------------------------------------------------------------
# VNC via ssh tunnel
vnc5901via22() {
if [ "X$(netstat -an | grep 127.0.0.1.5901)" != "X" ]; then
echo "Already binded"
return 1
fi
ssh -N -T -L 5901:localhost:5901 $1 &
}
# Git
gdbranch() {
branch=$1
git push origin --delete $branch
git branch -D $branch
}
# Docker
rac() {
docker rm $(docker ps -a | grep -v CONTAINER | grep -v _keep | awk '{print $1}')
}
rai() {
docker rmi $(docker images | grep '<none>' | awk '{print $3}')
}
dockerunbash() {
host_name=$3
c_name=$2
i_name=$1
cmd="docker run -ti"
if [[ "X$c_name" != "X" ]]; then
cmd="$cmd --name='$c_name'"
fi
if [[ "X$host_name" != "X" ]]; then
cmd="$cmd -h $host_name"
fi
cmd="$cmd $i_name /bin/bash"
$cmd
}
doexecbash() {
c_id=$1
docker exec -ti $c_id /bin/bash --login
}
sshkeyrm() {
ssh-keygen -f ~/.ssh/known_hosts -R $1
}
vms () {
VBoxHeadless -s $1 -v off &
i=0
while [ $i -lt 60 ]; do
ip=$(VBoxManage guestproperty enumerate $1 | grep -i ip)
if [[ "X$ip" == "X" ]]; then
echo "Starting..."
sleep 2
i=$((i+2))
else
echo $ip
break
fi
done
}
vmip() {
VBoxManage guestproperty enumerate $1 |grep -i ip
}
vmd () { VBoxManage controlvm $1 $2; }
vmrdp () { VBoxManage controlvm $1 vrde $2; }
xopen () { cd ~/Documents/xopen/$1; }
xfolder() {
if [ $# -lt 1 ]; then
echo "I need a host to work on..."
exit 1
fi
# make the first hostname as a template to compare with other hostnames
first_hostname=$1
shift
ts_name=""
# Compare character by character with the first argument.
for j in $(seq 0 ${#first_hostname})
do
for i in $*
do
common_char=${first_hostname:$j:1}
if [ "X${i:$j:1}" != "X$common_char" ]; then
common_char="_"
break
fi
done
ts_name="${ts_name}${common_char}"
done
if [ -z ${ts_name} ]; then
if [[ -z ${first_hostname} ]]; then
ts_name="unknown_name_$RANDOM"
else
ts_name=$first_hostname
fi
else
ts_name=$(echo $ts_name | sed 's/[@.]/_/g')
fi
tmux-cfolder -ts ${ts_name} $first_hostname $*
}
hostx() {
if [[ $# -lt 2 ]]; then
echo "No enough parameters provided!"
return 1
fi
host=$1
srv=""
shift
for i in $*
do
range=$(echo $i | sed 's/\.\./\^/')
start=${range%^*}
stop=${range#*^}
for j in $(seq $start $stop)
do
host $(echo $host | sed s/?/$j/)
done
done
}
xsh() {
if [ $# -lt 1 ]; then
echo "I need a host to work on..."
exit 1
fi
# make the first hostname as a template to compare with other hostnames
first_hostname=$1
shift
ts_name=""
# Compare character by character with the first argument.
for j in $(seq 0 ${#first_hostname})
do
for i in $*
do
common_char=${first_hostname:$j:1}
if [ "X${i:$j:1}" != "X$common_char" ]; then
common_char="_"
break
fi
done
ts_name="${ts_name}${common_char}"
done
if [ -z ${ts_name} ]; then
if [[ -z ${first_hostname} ]]; then
ts_name="unknown_name_$RANDOM"
else
ts_name=$first_hostname
fi
else
ts_name=$(echo $ts_name | sed 's/[@.]/_/g')
fi
tmux-cssh -ts ${ts_name} $first_hostname $*
}
hostx() {
if [[ $# -lt 2 ]]; then
echo "No enough parameters provided!"
return 1
fi
host=$1
srv=""
shift
for i in $*
do
range=$(echo $i | sed 's/\.\./\^/')
start=${range%^*}
stop=${range#*^}
for j in $(seq $start $stop)
do
host $(echo $host | sed s/?/$j/)
done
done
}
msr() {
if [[ "X$#" != "X1" ]]; then
echo "One and only one param needed"
return 1
fi
host=$1
mosh $1 -- /bin/bash -c "tmux at || tmux"
}
ssr() {
if [[ "X$#" != "X1" ]]; then
echo "One and only one param needed"
return 1
fi
host=$1
ssh $1 -t "tmux at || tmux"
}
function x() {
if [[ $# -gt 0 ]]; then
tmux $@
else
tmux at || tmux
fi
}
function cgrep() {
which printf >/dev/null 2>&1 && print=printf || print=print # Mandriva doesn't know about printf
hide='eval tput civis'
show='eval tput cnorm'
CLS=$(tput clear)
bel=$(tput bel)
case ${UNAME} in
AIX)
# text / foreground
N=$(${print} '\033[1;30m')
n=$(${print} '\033[0;30m')
R=$(${print} '\033[1;31m')
r=$(${print} '\033[0;31m')
G=$(${print} '\033[1;32m')
g=$(${print} '\033[0;32m')
Y=$(${print} '\033[1;33m')
y=$(${print} '\033[0;33m')
B=$(${print} '\033[1;34m')
b=$(${print} '\033[0;34m')
M=$(${print} '\033[1;35m')
m=$(${print} '\033[0;35m')
C=$(${print} '\033[1;36m')
c=$(${print} '\033[0;36m')
W=$(${print} '\033[1;37m')
w=$(${print} '\033[0;37m')
END=$(${print} '\033[0m')
# background
RN=$(${print} '\033[6;40m')
Rn=$(${print} '\033[40m')
RR=$(${print} '\033[6;41m')
Rr=$(${print} '\033[41m')
RG=$(${print} '\033[6;42m')
Rg=$(${print} '\033[42m')
RY=$(${print} '\033[6;43m')
Ry=$(${print} '\033[43m')
RB=$(${print} '\033[6;44m')
Rb=$(${print} '\033[44m')
RM=$(${print} '\033[6;45m')
Rm=$(${print} '\033[45m')
RC=$(${print} '\033[6;46m')
Rc=$(${print} '\033[46m')
RW=$(${print} '\033[6;47m')
Rw=$(${print} '\033[47m')
HIGH=$(tput bold)
SMUL=$(tput smul)
RMUL=$(tput rmul)
BLINK=$(tput blink)
REVERSE=$(tput smso)
REVERSO=$(tput rmso)
;;
*)
# text / foreground
n=$(tput setaf 0)
r=$(tput setaf 1)
g=$(tput setaf 2)
y=$(tput setaf 3)
b=$(tput setaf 4)
m=$(tput setaf 5)
c=$(tput setaf 6)
w=$(tput setaf 7)
N=$(tput setaf 8)
R=$(tput setaf 9)
G=$(tput setaf 10)
Y=$(tput setaf 11)
B=$(tput setaf 12)
M=$(tput setaf 13)
C=$(tput setaf 14)
W=$(tput setaf 15)
END=$(tput sgr0)
HIGH=$(tput bold)
SMUL=$(tput smul)
RMUL=$(tput rmul)
BLINK=$(tput blink)
REVERSE=$(tput smso)
REVERSO=$(tput rmso)
# background
Rn=$(tput setab 0)
Rr=$(tput setab 1)
Rg=$(tput setab 2)
Ry=$(tput setab 3)
Rb=$(tput setab 4)
Rm=$(tput setab 5)
Rc=$(tput setab 6)
Rw=$(tput setab 7)
RN=$(tput setab 8)
RR=$(tput setab 9)
RG=$(tput setab 10)
RY=$(tput setab 11)
RB=$(tput setab 12)
RM=$(tput setab 13)
RC=$(tput setab 14)
RW=$(tput setab 15)
;;
esac
BLUEf=${B}
BLUE=${b}
REDf=${R}
RED=${r}
GREENf=${G}
GREEN=${g}
YELLOWf=${Y}
YELLOW=${y}
MANGENTAf=${M}
MANGENTA=${m}
WHITEf=${W}
WHITE=${w}
CYANf=${C}
CYAN=${c}
colors=($BLUEf $BLUE $REDf $RED $GREENf $GREEN $YELLOWf $YELLOW $MANGENTAf $MANGENTA $WHITEf $WHITE $CYANf $CYAN)
replace=""
for i in $@
do
color=${colors[$((RANDOM % 14))]}
replace="$replace s#$i#$color$REVERSE$Rn$i$END#g;"
done
perl < /dev/null > /dev/null 2>&1
if [[ $? != 0 ]]; then
echo "Perl not found, please install one"
exit
fi
perl -pe "$replace"
}