-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc
331 lines (324 loc) · 12.7 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
# Antigen ------------------------------------------------------------------{{{
[ -f ~/.antigen/antigen.zsh ] || git clone https://github.com/zsh-users/antigen.git "${HOME}/.antigen"
source ~/.antigen/antigen.zsh
antigen use oh-my-zsh
antigen bundle colored-man-pages
antigen bundle git
antigen bundle docker
antigen bundle zsh-users/zsh-syntax-highlighting
antigen bundle wu-nerd/fasd
antigen bundle supercrabtree/k
antigen bundle zsh-users/zsh-autosuggestions
antigen bundle zsh-users/zsh-completions
antigen theme https://github.com/denysdovhan/spaceship-zsh-theme spaceship
export SPACESHIP_TIME_SHOW=true
antigen apply
# }}}
# Configuration-------------------------------------------------------------{{{
# This for vim theme in tmux ---------------------------------------------{{{
export TERM=screen-256color
# }}}
# Emacs key binding ------------------------------------------------------{{{
bindkey '^P' up-line-or-search
bindkey '^N' down-line-or-search
# }}}
# Set vim as the default editor-------------------------------------------{{{
export VISUAL=vim
export EDITOR="$VISUAL"
# }}}
# Use EscEsc to sudo the last command or the current command--------------{{{
sudo-command-line() {
# If current buffer is empth, get the last command
[[ -z $BUFFER ]] && zle up-history
# If the command not start with sudo
[[ $BUFFER != sudo\ * ]] && {
typeset -a bufs
bufs=(${(z)BUFFER})
# If the first word in BUFFER is an alias, replace is with
# it's value
if (( $+aliases[$bufs[1]] )); then
bufs[1]=$aliases[$bufs[1]]
fi
bufs=(sudo $bufs)
BUFFER=$bufs
}
zle end-of-line
}
zle -N sudo-command-line
bindkey "\e\e" sudo-command-line
# }}}
# Use percol in Ctrl-R ---------------------------------------------------{{{
function exists { which $1 &> /dev/null }
if exists percol; then
function percol_select_history() {
local tac
exists gtac && tac="gtac" || { exists tac && tac="tac" || { tac="tail -r" } }
BUFFER=$(fc -l -n 1 | eval $tac | percol --query "$LBUFFER")
CURSOR=$#BUFFER # move cursor
zle -R -c # refresh
}
zle -N percol_select_history
bindkey '^R' percol_select_history
fi
# }}}
# Prevent iTerm2 from closing when typeing Ctrl-D ------------------------{{{
set -o ignoreeof
# }}}
# Load private configration ----------------------------------------------{{{
[ -f ~/.zshrc.private ] && source ~/.zshrc.private
# }}}
# Speeds up for pasting in zsh-autosuggestions ---------------------------{{{
# https://github.com/zsh-users/zsh-autosuggestions/issues/238
pasteinit() {
OLD_SELF_INSERT=${${(s.:.)widgets[self-insert]}[2,3]}
zle -N self-insert url-quote-magic # I wonder if you'd need `.url-quote-magic`?
}
pastefinish() {
zle -N self-insert $OLD_SELF_INSERT
}
zstyle :bracketed-paste-magic paste-init pasteinit
zstyle :bracketed-paste-magic paste-finish pastefinish
# }}}
# Complete for brew installed formulaes ----------------------------------{{{
fpath=(/usr/local/share/zsh/site-functions $fpath)
# The following lines were added by compinstall
# End of lines added by compinstall
zstyle :compinstall filename '/Users/nerd/.zshrc'
autoload -Uz compinit
compinit
# }}}
# }}}
# Functions ----------------------------------------------------------------{{{
# Extract ----------------------------------------------------------------{{{
# From alias.sh
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.tar.xz) tar xvf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip -O GB18030 $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
}
# }}}
# Ltree ------------------------------------------------------------------{{{
ltree()
{
tree -C $* | less -FXRS
}
# }}}
# Psg --------------------------------------------------------------------{{{
psg () {
ps -o pid,pmem,pcpu,command -A |grep -v grep|grep $1\
|awk '{printf "%-7s%-5s%-5s%-7s\n", $1,$2,$3,$4" "$5" "$6}'
}
# }}}
# Run the previous cmd and grep the output then pip to less --------------{{{
ge () {
if [ $# -ne 0 ] ; then
CURRENT_COMMAND="`echo $LAST_COMMAND` | grep $*"
eval $CURRENT_COMMAND
fi
}
# }}}
# Expand alias------------------------------------------------------------{{{
# When input space, expand alias -----------------------------------{{{
expand_alias_space () {
zle _expand_alias
zle self-insert
}
zle -N expand_alias_space
bindkey " " expand_alias_space
# }}}
# When input enter, expand alias -----------------------------------{{{
expand_alias_enter () {
if [[ -z $BUFFER ]]
then
zle clear-screen
else
zle _expand_alias
zle accept-line
# Remember the last command, useful in some alias
# Add space at the beginning of a command, this command won't
# show up in history, so use variables to store the command
LAST_COMMAND=$CURRENT_COMMAND
CURRENT_COMMAND=$BUFFER
fi
}
zle -N expand_alias_enter
bindkey "^M" expand_alias_enter
# Suggestion will clear when press enter, or suggestion will be shown
# as part to previous cmmands
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=expand_alias_enter
# }}}
# }}}
# Enter a dir contains $1-------------------------------------------------{{{
# Stolen from https://github.com/fcoury/dotfiles-1/blob/master/.zshrc
function cdf () {
cd *$1*/
}
# }}}
# Less with highlith or run the last command and pip to less--------------{{{
le() {
if [ $# -eq 0 ]
then
CURRENT_COMMAND="`echo $LAST_COMMAND` | less -FXRS"
eval $CURRENT_COMMAND
# Pipe to less didn't change anything and may case some problem
# when run this command twice. So we delete that part.
CURRENT_COMMAND="`echo $LAST_COMMAND`"
else
pygmentize -O bg=dark $1|less -FXRS
fi
}
# }}}
# Get the Xst column of clipboard ----------------------------------------{{{
cl() {
if [ -z "$2" ]
then
awk '{print $'$1'}'
else
awk -F $2 '{print $'$1'}'
fi
}
pcl() {
if [ -z "$2" ]
then
pbpaste | awk '{print $'$1'}'
else
pbpaste | awk -F $2 '{print $'$1'}'
fi
}
# And copy back to clipboack
ccl() {
if [ -z "$2" ]
then
awk '{print $'$1'}' | pbcopy
else
awk -F $2 '{print $'$1'}' | pbcopy
fi
}
pccl() {
if [ -z "$2" ]
then
pbpaste | awk '{print $'$1'}' | pbcopy
else
pbpaste | awk -F $2 '{print $'$1'}' | pbcopy
fi
}
# }}}
# Cd to the path of the front Finder window ------------------------------{{{
cdf() {
target=`osascript -e 'tell application "Finder" to \
if (count of Finder windows) > 0 \
then get POSIX path of (target of front Finder window as text)'`
if [ "$target" != "" ]; then
cd "$target"; pwd
else
echo 'No Finder window found' >&2
fi
}
# }}}
# }}}
# Alias --------------------------------------------------------------------{{{
# Source & edit zshrc ----------------------------------------------------{{{
alias sz="source ~/.zshrc"
alias ez="vim ~/.zshrc"
# }}}
# Source & edit vimrc ----------------------------------------------------{{{
alias ev="vim ~/.vimrc"
# }}}
# Git --------------------------------------------------------------------{{{
alias gl="git log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset \
%s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
alias gfl="git log -p -M --follow --stat --"
if exists percol; then
alias gb="git branch | percol | xargs git checkout"
fi
# }}}
# Show the top 15 progress that gobbling the memory and CPU---------------{{{
alias psm="ps -o pid,pmem,pcpu,command -A | sort -n -r -k 2 | head -15 | awk '{printf \"%-7s%-5s%-5s%-7s\\n\", \$1,\$2,\$3,\$4\" \"\$5\" \"\$6}'"
alias psu="ps -o pid,pmem,pcpu,command -A | sort -n -r -k 3 | head -15 | awk '{printf \"%-7s%-5s%-5s%-7s\\n\", \$1,\$2,\$3,\$4\" \"\$5\" \"\$6}'"
# }}}
# Colorfull cat ----------------------------------------------------------{{{
alias ccat='pygmentize -O bg=dark'
# }}}
# Fasd -------------------------------------------------------------------{{{
alias v='fasd -f -e vim'
alias o='fasd -f -e xdg-open'
alias sv='fasd -sif -e vim'
alias so='fasd -sif -e xdg-open'
# }}}
# P ----------------------------------------------------------------------{{{
alias p="percol"
# }}}
# Psk --------------------------------------------------------------------{{{
alias psk="ps aux | percol | awk '{ print \$2 }' | xargs kill -9"
# }}}
# brew & brew cask -------------------------------------------------------{{{
if command -v brew &> /dev/null; then
alias bi="brew install"
alias br="brew uninstall"
alias bs="brew search"
alias bl="brew list"
alias bu="brew upgrade"
alias bud="brew update"
alias bug="brew upgrade"
alias bci="brew install --cask"
alias bcr="brew uninstall --cask"
alias bcl="brew list --cask"
alias bcu="brew cask upgrade"
# force install cask to lastest even it's auto update
alias bcif="brew outdated --cask --greedy --verbose | grep -v latest | tee /dev/tty | awk '{print \$1}' | xargs brew upgrade --cask"
fi
# }}}
# Curl -------------------------------------------------------------------{{{
alias c='echo ;curl -w "\n
http_code: %{http_code}
time_namelookup: %{time_namelookup}
time_connect: %{time_connect}
time_appconnect: %{time_appconnect}
time_pretransfer: %{time_pretransfer}
time_starttransfer: %{time_starttransfer}
size_request: %{size_request}
size_download: %{size_download}
speed_download: %{speed_download}
time_total: %{time_total}\n\n" -i '
# }}}
# Apt --------------------------------------------------------------------{{{
if exists apt; then
alias agd="sudo apt update"
alias agg="sudo apt upgrade"
alias agi="sudo apt install"
alias agr="sudo apt remove"
alias agar="sudo apt autoremove"
alias agac="sudo apt autoclean"
fi
if exists apt-cache; then
alias acs="apt search"
fi
# }}}
# Dpkg -------------------------------------------------------------------{{{
if exists dpkg; then
alias di="sudo dpkg -i"
alias dr="sudo dpkg -r"
alias dl="dpkg -l"
fi
# }}}
# Git --------------------------------------------------------------------{{{
alias gcop="git branch | percol | xargs git checkout"
alias gup="git pull origin \$(git_current_branch)"
# }}}
# }}}