-
Notifications
You must be signed in to change notification settings - Fork 3
/
zshrc_base
executable file
·397 lines (304 loc) · 11.5 KB
/
zshrc_base
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
## GENERAL EXPORTS ############################################################################################################
export CLICOLOR="xterm-color"
export WORDS=/usr/share/dict/words
## GENERAL ############################################################################################################
alias dh='dirs -v' # directory history
alias is_port_open='nc -z localhost'
function wait_for_8080() {
echo "waiting for port 8080 to open up"
while ! is_port_open 8080; do sleep 0.1; done;
}
# grep history
alias gh='fc -l 0 | grep'
alias spwd='pwd | pbcopy' # copy the current working directory to the clipboard
alias pwp='pwd -P'
# give it an url and it will see how long it takes to make 100 requests with 10 connections
alias absimple='ab -n 100 -c 10 -g gnuplot.tsv'
export VIM_EDITOR=code
alias v='$VIM_EDITOR'
alias vz='v $ZSHDIR'
# needs gxargs - brew install findutils
open_zshrc(){
cd $HOME/shared-zshrc && fd -t f -L -H -I -E ".git" | fzf -m | gxargs -ro -d "\n" "$VIM_EDITOR"
}
zle -N open_zshrc
# \e is escape, or option if checked in iterm preferences, so opt-z
bindkey '\ez' open_zshrc
alias agrep='alias | grep -i'
# copy the last command to your clipboard
alias clc='fc -ln -1 | pbcopy && echo $(pbpaste)'
# builtins don't have their own man page
alias manbi='man zshbuiltins'
alias myip="curl icanhazip.com"
alias curltime="curl -sL -w ' namelookup: %{time_namelookup}\n connect: %{time_connect}\n appconnect: %{time_appconnect}\n pretransfer: %{time_pretransfer}\n redirect: %{time_redirect}\nstarttransfer: %{time_starttransfer}\n total: %{time_total}\n' "
alias httpcode='curl --write-out %{http_code} --head --silent --output /dev/null'
# global aliases
alias -g G='| grep'
alias -g GI='| grep -i'
alias -g GV='| grep -v'
alias -g GE='| egrep'
alias -g PC="| pc"
alias -g XGI="| xargs grep -ni"
alias -g XG="| xargs grep -n"
alias -g L='| less'
alias -g PBC='| pbcopy'
alias -g X1="| xargs -L1"
alias -g ND='*(/om[1])' # newest directory
alias -g NF='*(.om[1])' # newest file
rot13 () { tr "[a-m][n-z][A-M][N-Z]" "[n-z][a-m][N-Z][A-M]" }
alias duc='du -sh *(/)'
alias duca='du -sh ./*'
function epochToDate() {
if [ -z $1 ] ; then
echo "USAGE: epochToDate 1509821064514"
return 1
fi
date -j -r $(($1 / 1000))
}
# forward requests from port 80 to 8080
alias ipfw80to8080='sudo ipfw add 100 fwd 127.0.0.1,8080 tcp from any to any 80 in'
# starts a server on port 8000 that makes the current directory browsable with a webbrowser
alias webshare='python -m SimpleHTTPServer'
function server() {
local port="${1:-8000}"
open "http://localhost:${port}/"
python -m SimpleHTTPServer "$port"
}
function open_when_resolves() {
echo "Waiting for $1 to resolve and not redirect to open"
while [ ! $(curl --write-out %{http_code} --max-time 10 --silent --output /dev/null $1) = 200 ]; do printf "."; sleep 0.5; done;
open $1
}
# SSL #
alias sslserver=': SSL print out a list of the acceptable CAs for a server, as well as its public key, pass host:port; openssl s_client -prexit -showcerts -connect'
# alias sslprintcert=': SSL print out the details of a cert using keytool; keytool -v -printcert -file'
alias sslprintcert=': SSL print out the details of a cert using openssl; openssl x509 -noout -text -in'
alias sslprintprivate=': SSL print out the details of a private key using openssl; openssl rsa -noout -text -in'
alias check_pem_cert='openssl x509 -subject -noout -nameopt RFC2253 -enddate -in'
## if these match, they are the public/private pair
function sslx509md5() { openssl x509 -noout -modulus -in $1 | openssl md5 }
function sslrsamd5() { openssl rsa -noout -modulus -in $1 | openssl md5 }
alias sslpublicmd5=': SSL md5 of public key, check match against private; sslx509md5 $1'
alias sslprivatemd5=': SSL md5 of private key, check match against public; sslrsamd5 $1'
alias sslpk8topem=': SSL converte pk8 private key used by java to rsa pem format private key; openssl pkcs8 -nocrypt -in'
alias sslstoredkeys=': SSL print out all of the keys in a keystore file; keytool -list -v -keystore'
alias sslstoredkeys2=': SSL print out all of the raw public keys in a keystore file; keytool -list -rfc -keystore'
alias sslcerthash=': SSL print out the hash for a certificate; openssl x509 -hash -noout -in'
# brew install xmlstarlet
# sudo easy_install pygments
alias xmlpretty='xml fo | pygmentize -l xml'
## VERSION CONTROL ############################################################################################################
alias gs="git st"
alias grh="git reset --hard"
alias grs="git reset --soft"
alias gd="git diff"
alias gdt="git diff"
alias gds="git diff --stat"
alias gdth1="git difftool HEAD~1"
alias gdh1="git diff HEAD~1"
alias gdtod="git difftool origin/devleop"
alias gdod="git diff origin/develop"
alias gdtom="git difftool origin/master"
alias gdom="git diff origin/master"
alias -g H1="HEAD~1"
alias -g OD="origin/develop"
alias -g OM="origin/master"
alias grom="git rebase -p origin/master"
## Java
function jdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $1`
export PATH=$JAVA_HOME/bin:$PATH
else
java -version
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
# jdk 1.8
jdk 11
function find_file_up() {
ls (../)#"$1"(:a)
}
function execute_up() {
local find_me="$1"
shift
local found=$(find_file_up "$find_me")
if [[ -f "$found" ]]; then
"$found" $*
else
echo "no $find_me in parent directories"
fi
}
alias gw="execute_up gradlew"
# auto fix any ktlint errors, install with: brew install ktlint
alias kgw="ktlint -F && gw"
alias kgwc="kgw check"
alias kgwcc="kgw clean check"
# docker
alias dc="docker-compose"
function drma() {
## docker remove all containers
[[ -z $(docker ps -aq) ]] || docker rm -f $(docker ps -aq)
}
## watchexec
# live reload like env for a single file
# watch a file (create it if it doesn't exist) if it changes run a command
function wexec() {
command -v watchexec >/dev/null 2>&1 || { echo "'watchexec' is required. e.g. brew install watchexec or cargo install watchexec" >&2; exit 1; }
if [[ -z $1 || "$1" == "--help" ]]; then
echo "usage: "
echo " wexec <file/command to watch>"
echo " the file will be assumed to be an executable command that will be run"
echo " when it changes"
echo ""
echo " ex: wexec foo.sh"
echo ""
echo " the file will be created and marked as executable if it does not exist"
echo "or:"
echo ""
echo " wexec <file/command to watch> <command args to run when file is changed>"
echo ""
echo " ex: wexec yolo.sql psql -f yolo.sql"
echo ""
return -1
fi
FILE=$1
if [[ -z $2 ]]; then
# if no further commands are passed, then the file is the command
COMMAND="./$FILE"
if [ ! -f $FILE ]; then
# create the file as it doesn't exist, make it executable as it is the command
touch "$FILE"
chmod +x "$FILE"
fi
else
if [ ! -f $FILE ]; then
# create the file as it doesn't exist, don't make it executable, it is not the command
touch "$FILE"
fi
# all the args after the first are the command to run
COMMAND="${@:2}"
fi
# open the file in an editor for changes
$VIM_EDITOR "$FILE"
bash -c "watchexec -s SIGINT -r -d 100 -f '*/$FILE' '$COMMAND'"
}
## Pony
# regenerate ctags, including tags for the pony standard library, run from the git project root
alias pctags='echo -e "$(cd -P "$(which ponyc)/../.."; pwd)\n$(pwd)" | ctags --tag-relative --extra=f -R -L -'
# brew install llvm
# from: https://www.ponylang.io/reference/pony-lldb-cheatsheet/
export LDFLAGS="-L/usr/local/opt/llvm/lib"
export CPPFLAGS="-I/usr/local/opt/llvm/include"
## Python pyenv/direnv
if command -v direnv >/dev/null 2>&1; then
eval "$(direnv hook zsh)"
else
echo "missing direnv, install with:"
echo "brew install direnv"
fi
if command -v pyenv >/dev/null 2>&1; then
eval "$(pyenv init -)"
else
echo "missing pyenv, install with:"
echo "brew install pyenv"
echo "pyenv install 3.8.3"
fi
## nim
if [[ -d "$HOME/.nimble/bin" ]]; then
export PATH="$HOME/.nimble/bin":$PATH
fi
## Fuzzy Finder Auto Completion
if [[ -d "/opt/homebrew/opt/fzf/shell" ]]; then
FZF_SHELL="/opt/homebrew/opt/fzf/shell"
else
FZF_SHELL="/usr/local/opt/fzf/shell"
fi
if [[ -d "$FZF_SHELL" ]]; then
export FZF_CTRL_R_OPTS="--min-height=20 --exact --preview 'echo {}' --preview-window down:3:wrap"
export FZF_DEFAULT_COMMAND='rg --files --no-ignore --hidden --follow -g "!{.git,node_modules,build}/*" 2> /dev/null'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_CTRL_T_OPTS=$'--min-height=20 --preview \'[[ $(file --mime {}) =~ binary ]] && echo {} is a binary file ||
(bat --style=numbers --color=always {} ||
cat {}) 2> /dev/null | head -500
\''
export FZF_DEFAULT_OPTS="
--layout=reverse
--info=inline
--height=80%
--bind '?:toggle-preview'
"
source "${FZF_SHELL}/completion.zsh" 2> /dev/null
source "${FZF_SHELL}/key-bindings.zsh"
alias fzfp="fzf $FZF_CTRL_T_OPTS"
alias -g F='| fzfp'
# needs findutils for gxargs and fd for better find
# open a file somewhere under the current directory, press "?" for preview window
open_fzf() {
fd -t f -L -H -E ".git" |\
fzf -m --min-height=20 \
--preview-window=:hidden \
--preview '[[ $(file --mime {}) =~ binary ]] && echo {} is a binary file || (bat --style=numbers --color=always {} || cat {}) 2> /dev/null | head -500' |\
gxargs -ro -d "\n" open
}
zle -N open_fzf
bindkey '^o' open_fzf
# cd into a directory based on and fzf directory search
cd_fzf() {
local basedir=${1:-.} # default to starting from current directory (.) but allow override
local directory
if directory=$(fd -t d -L -H -I -E ".git" . "$basedir" | fzf --preview="tree -L 1 {}" ); then
cd $directory && fzf-redraw-prompt
fi
}
zle -N cd_fzf
bindkey '^f' cd_fzf
# cd into a directory somewhere under the home directory
cd_home_fzf() {
cd_fzf "$HOME"
}
zle -N cd_home_fzf
# bindkey '\ef' cd_home_fzf # meta-f (opt)
export NOTES_DIR="$HOME/Dropbox/notes" # override in nonshared-zshrc/zshrc to get a different location
# needs gxargs and fd - brew install findutils fd
open_notes(){
pushd -q "$NOTES_DIR"
fd -t f -L -H -I -E ".git" | fzf -m | gxargs -ro -d "\n" "$VIM_EDITOR"
popd -q
}
zle -N open_notes
bindkey '^n' open_notes
jqpath_cmd='
def path_str: [.[] | if (type == "string") then "." + . else "[" + (. | tostring) + "]" end] | add;
. as $orig |
paths(scalars) as $paths |
$paths |
. as $path |
$orig |
[($path | path_str), "\u00a0", (getpath($path) | tostring)] |
add
'
# pipe json in to use fzf to search through it for jq paths, uses a non-breaking space as an fzf column delimiter
alias jqpath="jq -rc '$jqpath_cmd' | cat <(echo $'PATH\u00a0VALUE') - | column -t -s $'\u00a0' | fzf +s -m --header-lines=1"
alias jqpathr="jq -rc '$jqpath_cmd'"
else
echo "missing fzf: brew install fzf ripgrep bat fd findutils jq"
fi
# navi
if command -v navi >/dev/null 2>&1; then
source <(navi widget zsh)
fi
## Antibody ZSH Plugins
if command -v antibody >/dev/null 2>&1; then
source <(antibody init)
antibody bundle < "$ZSHDIR/zsh_plugins.txt"
else
echo "antibody?"
echo "brew install getantibody/tap/antibody"
fi