-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc
501 lines (390 loc) · 10.6 KB
/
.zshrc
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
################################################################################
umask 022
################################################################################
# Path to your oh-my-zsh installation
export ZSH=$HOME/.oh-my-zsh
# GPG always wants to know what TTY it's running on
export GPG_TTY=$(tty)
# Set default theme to KAOS
ZSH_THEME="kaos"
# Create array with plugins
plugins=()
# Enable fzf plugin if fzf is installed
if [[ -d "$HOME/.fzf" ]] ; then
export PATH="$HOME/.fzf:$PATH"
plugins+=(fzf)
fi
# Disable automatic oh-my-zsh update
zstyle ':omz:update' mode disabled
# Enable oh-my-zsh
source $ZSH/oh-my-zsh.sh
################################################################################
HISTFILE="$HOME/.zsh_history"
HISTSIZE=10000000
SAVEHIST=10000000
HISTORY_IGNORE="(ls|ll|cd|pwd|exit|export)*"
setopt APPEND_HISTORY # Append to history file (Default)
setopt EXTENDED_HISTORY # Write the history file in the ':start:elapsed;command' format
setopt INC_APPEND_HISTORY # Write to the history file immediately, not when the shell exits
setopt SHARE_HISTORY # Share history between all sessions
setopt HIST_IGNORE_DUPS # Do not record an event that was just recorded again
setopt HIST_IGNORE_ALL_DUPS # Delete an old recorded event if a new event is a duplicate
setopt HIST_IGNORE_SPACE # Do not record an event starting with a space
setopt HIST_SAVE_NO_DUPS # Do not write a duplicate event to the history file
setopt HIST_VERIFY # Do not execute immediately upon history expansion
setopt HIST_NO_STORE # Don't store history commands
setopt HIST_REDUCE_BLANKS # Remove superfluous blanks from each command line being added to the history
################################################################################
setopt nocaseglob # ignore case
setopt correct # correct spelling mistakes
setopt auto_cd # if there is no app with given name, try to cd to it
################################################################################
SSH_QUIET_OPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet"
################################################################################
# Export env vars
export PAGER="more"
export LESS="-MQR"
export VISUAL="nano"
export EDITOR="nano"
export LANG="en_US.UTF-8"
export TERM="xterm-256color"
export COLORTERM="truecolor"
# Go env
export GOPROXY=direct
export GO111MODULE=auto
export GOTOOLCHAIN=local
export CGO_ENABLED=0
export GOPATH=~/projects/gocode
export GOBIN=~/projects/gocode/bin
export PATH=~/projects/gocode/bin:$PATH
# Add local bin dir to PATH
export PATH=$HOME/.bin:/usr/local/bin:$PATH
# Aliases
alias sshk="ssh $SSH_QUIET_OPTS"
alias scpk="scp $SSH_QUIET_OPTS"
alias dl="curl -ZL --max-redirs 3 --parallel-max 5 --remote-name-all"
alias c="clear"
alias g="grep --color=auto"
alias e="$EDITOR"
alias b="bat"
alias d="docker"
alias dr="docker run --rm -it"
alias de="docker exec -it"
# Custom functions
alias hf="history_find"
alias txc="tmux_win_to_path"
alias txn="rename_pane"
alias goc="go_cover"
alias gcl="go_clone"
alias bkp="create_backup"
alias ssht="ssh_multi"
alias flat="cat_flat"
alias ll="ls_ext"
alias l="ls_ext"
# Traps
alias git="git_trap"
alias cd="cd_trap"
alias ssh="ssh_trap"
alias scp="scp_trap"
# Use screen-256color on CentOS 7 for fix colors rendering (can break copy & paste)
if [[ $(grep 'CPE_NAME' /etc/os-release | tr -d '"' | cut -d':' -f5) == "7" ]] ; then
alias tx="TERM=screen-256color tmux attach 2>/dev/null || TERM=screen-256color tmux new -n HOME"
else
alias tx="tmux attach 2>/dev/null || tmux new -n HOME"
fi
################################################################################
function ls_ext() {
if [[ $# -eq 0 ]] ; then
ls -lhv --color=always | sed 's/ -> / → /g'
else
ls -lhv --color=always "$@" | sed 's/ -> / → /g'
fi
return $?
}
function git_trap() {
if [[ "$1" == "release" ]] ; then
shift
git_release $*
elif [[ "$1" == "tag-delete" ]] ; then
shift
git_tag_delete $*
elif [[ "$1" == "tag-update" ]] ; then
shift
git_tag_update $*
elif [[ "$1" == "undo" ]] ; then
shift
git_undo $*
elif [[ "$1" == "pr" ]] ; then
shift
git_pr $*
else
\git $*
fi
return $?
}
function cd_trap() {
# We not in a tmux session
if [[ -z "$TMUX" ]] ; then
\cd $*
return $?
fi
local window_name="$(tmux display-message -p '#W')"
local window_name_fs="$window_name[0,1]"
local shell_name=$(printenv SHELL | sed 's/.*\///')
# Name of shell it is default name for new tmux window
if [[ "$window_name" != "$shell_name" ]] ; then
# Window has custom name, not path, so do not rename window
if [[ "$window_name_fs" != "/" && "$window_name_fs" != "~" ]] ; then
\cd $*
return $?
fi
fi
\cd $*
if [[ $? -ne 0 ]] ; then
return $?
fi
tmux_win_to_path
}
function ssh_trap() {
# We not in a tmux session
if [[ -z "$TMUX" ]] ; then
ssh_exec $*
return $?
fi
local ssh_session="$@[-1]"
local window_name="$(tmux display-message -p '#W')"
local window_name_fs="$window_name[0,1]"
# Window has custom name, not path, so do not rename window
if [[ "$window_name_fs" != "/" && "$window_name_fs" != "~" ]] ; then
rename_pane "SSH ($ssh_session)"
ssh_exec $*
return $?
fi
local window_index="$(tmux display-message -p '#I')"
tmux rename-window "SSH ($ssh_session)"
rename_pane "SSH ($ssh_session)"
ssh_exec $*
local ec=$?
# Restore original window name
tmux rename-window -t "$window_index" "$window_name"
rename_pane "$window_name"
return $ec
}
function ssh_exec() {
if \ssh -V 2>&1 | grep -q '7.4' ; then
\ssh $*
else
\ssh -o StrictHostKeyChecking=accept-new $*
fi
return $?
}
function scp_trap() {
if \ssh -V 2>&1 | grep -q '7.4' ; then
\scp $*
else
\scp -o StrictHostKeyChecking=accept-new $*
fi
return $?
}
function git_release() {
if [[ $# -eq 0 ]] ; then
echo "Usage: git release {version}"
return 0
fi
local defBranch=$(\git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
if [[ $(\git rev-parse --abbrev-ref HEAD 2>/dev/null) != "$defBranch" ]] ; then
if ! \git push ; then
return 1
fi
if ! \git checkout "$defBranch" ; then
return 1
fi
fi
if ! \git pull ; then
return 1
fi
sleep 3
if [[ -e $HOME/.gnupg ]] ; then
\git tag -s "v$1" -m "Version $1"
else
\git tag "v$1" -m "Version $1"
fi
[[ $? -ne 0 ]] && return 1
if ! \git push --tags ; then
return 1
fi
if \git rev-parse --quiet --verify develop &> /dev/null ; then
\git checkout develop
fi
return $?
}
function git_tag_update() {
if [[ $# -eq 0 ]] ; then
echo "Usage: git tag-update {tag}"
return 0
fi
if ! \git pull ; then
return 1
fi
if [[ -e $HOME/.gnupg ]] ; then
\git tag -f -s "$1" -m "Version ${1/v/}"
else
\git tag -f "$1" -m "Version ${1/v/}"
fi
[[ $? -ne 0 ]] && return 1
\git push -f --tags
return $?
}
function git_tag_delete() {
if [[ $# -eq 0 ]] ; then
echo "Usage: git tag-delete {tag}"
return 0
fi
if ! \git tag -d "$1" ; then
return 1
fi
\git push origin ":refs/tags/$1"
return $?
}
function git_pr() {
if [[ $# -ne 2 ]] ; then
echo "Usage: git pr {get|rm} {pr-id}"
return 0
fi
if [[ "$1" == "get" ]] ; then
if ! \git fetch origin "pull/$2/head:PR-$2" ; then
return 1
fi
if ! \git checkout "PR-$2" ; then
return 1
fi
elif [[ "$1" == "rm" ]] ; then
if ! \git checkout - ; then
return 1
fi
if ! \git branch -D "PR-$2" ; then
return 1
fi
else
echo "Usage: git pr {get|rm} {pr-id}"
fi
}
function git_undo() {
\git reset HEAD~
return $?
}
function history_find() {
if [[ $# -eq 0 ]] ; then
echo "Usage: hf {string}"
return 0
fi
history | grep --color=always "$@" | cut -f4-99 -d" "
}
function go_cover() {
if ! go test -coverprofile=cover.out $* ; then
rm -f cover.out &> /dev/null
return 1
fi
if which htmlcov &> /dev/null ; then
htmlcov -R cover.out
else
go tool cover -html=cover.out -o coverage.html
rm -f cover.out
fi
}
function go_clone() {
if [[ $# -eq 0 ]] ; then
echo "Usage: gcl {org}/{repo}"
return 0
fi
if ! echo "$1" | grep -q '/' ; then
echo "Usage: gcl {org}/{repo}"
return 0
fi
if ! which go &> /dev/null ; then
print_error "Go is not installed"
return 1
fi
local org=$(echo "$1" | cut -f1 -d'/')
local repo=$(echo "$1" | cut -f2 -d'/')
if [[ "$org" == "" || "$repo" == "" ]] ; then
print_error "Wrong source format"
return 1
fi
local clone_dir="$GOPATH/src/github.com/$org/$repo"
if [[ -d "$clone_dir" ]] ; then
print_error "Target directory ($clone_dir) already exist"
return 1
fi
mkdir -p "$clone_dir"
if ! \git clone "[email protected]:$org/$repo.git" "$clone_dir" ; then
return 1
fi
pushd $clone_dir &> /dev/null
\git checkout develop &> /dev/null
popd &> /dev/null
return 0
}
function create_backup() {
if ! cp -rp "$1" "$1.bak" ; then
return 1
fi
if [[ -d "$1" ]] ; then
chmod 0700 "$1.bak"
else
chmod 0600 "$1.bak"
fi
return $?
}
function ssh_multi() {
if [[ $# -lt 2 ]] ; then
echo "Usage: ssht host1 host2 host3…"
return 0
fi
if [[ -z "$TMUX" ]] ; then
print_error "This alias works only with TMUX"
return 1
fi
tmux new-window -n "SSH ($#)" "ssh $SSH_QUIET_OPTS $1 ; sleep 5"
rename_pane "$1"
shift 1
for conn in "$@" ; do
tmux split-window "ssh $SSH_QUIET_OPTS $conn ; sleep 5"
rename_pane "$conn"
done
tmux select-layout even-vertical
}
function cat_flat() {
if [[ $# -lt 2 ]] ; then
echo "Usage: flat file"
return 0
fi
cat $1 | tr '\n' ' ' ; echo ""
}
function tmux_win_to_path() {
if [[ -z "$TMUX" ]] ; then
return 1
fi
tmux rename-window "$(get_current_dir_short)"
}
function get_current_dir_short() {
pwd | sed "s|^$HOME|~|" 2> /dev/null | sed 's:\(\.\?[^/]\)[^/]*/:\1/:g'
}
function rename_pane() {
tmux set-option -p @custom_pane_title "$*"
}
function print_error() {
echo -e "\e[31m▲ $*\e[0m"
}
################################################################################
function reset-prompt-and-accept-line {
zle reset-prompt
zle .accept-line
}
zle -N accept-line reset-prompt-and-accept-line
################################################################################
# Include local zshrc
if [[ -f $HOME/.zshrc.local ]] ; then
source $HOME/.zshrc.local
fi
################################################################################