forked from mathiasbynens/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.zshrc
428 lines (359 loc) · 9.78 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
#!/usr/bin/env zsh
# Load main files.
# echo "Load start\t" $(gdate "+%s-%N")
source "$HOME/terminal/startup.sh"
# echo "$HOME/terminal/startup.sh"
source "$HOME/terminal/completion.sh"
source "$HOME/terminal/highlight.sh"
# echo "Load end\t" $(gdate "+%s-%N")
autoload -U colors && colors
# Load and execute the prompt theming system.
fpath=("$HOME/terminal" $fpath)
autoload -Uz promptinit && promptinit
prompt 'asilluron'
# ==================================================================
# = Aliases =
# ==================================================================
alias -g f2='| head -n 2'
alias -g f10='| head -n 10'
alias -g l10='| tail -n 10'
# Simple clear command.
alias cl='clear'
# Disable sertificate check for wget.
alias wget='wget --no-check-certificate'
# JSHint short-cut.
alias lint=jshint
# Faster NPM for europeans.
# alias npme='npm --registry http://registry.npmjs.eu'
# Some macOS-only stuff.
if [[ "$OSTYPE" == darwin* ]]; then
# Short-cuts for copy-paste.
alias c='pbcopy'
alias p='pbpaste'
# Remove all items safely, to Trash (`brew install trash`).
alias rm='trash'
# Lock current session and proceed to the login screen.
alias lock='/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'
# Sniff network info.
alias sniff="sudo ngrep -d 'en1' -t '^(GET|POST) ' 'tcp and port 80'"
# Developer tools shortcuts.
alias tower='gittower'
alias t='gittower'
# Process grep should output full paths to binaries.
alias pgrep='pgrep -fli'
else
# Process grep should output full paths to binaries.
alias pgrep='pgrep -fl'
fi
# Git short-cuts.
alias g='git'
alias ga='git add'
alias gr='git rm'
alias gf='git fetch'
alias gu='git pull'
alias gup='git pull && git push'
alias gs='git status --short'
alias gd='git diff'
alias gds='git diff --staged'
alias gdn='git diff --name-only'
alias gdisc='git discard'
alias grao='git remote add origin'
function gc() {
args=$@
git commit -m "$args"
}
function gca() {
args=$@
git commit --amend -m "$args"
}
function cherry() {
is_range=''
case "$1" in # `sh`-compatible substring.
*\.*)
is_range='1'
;;
esac
# Check if it's one commit vs set of commits.
if [ "$#" -eq 1 ] && [[ $is_range ]]; then
log=$(git rev-list --reverse --topo-order $1 | xargs)
setopt sh_word_split 2> /dev/null # Ignore for `sh`.
commits=(${log}) # Convert string to array.
unsetopt sh_word_split 2> /dev/null # Ignore for `sh`.
else
commits=("$@")
fi
total=${#commits[@]} # Get last array index.
echo "Picking $total commits:"
for commit in ${commits[@]}; do
echo $commit
git cherry-pick -n $commit || break
[[ CC -eq 1 ]] && cherrycc $commit
done
}
alias gp='git push'
function gcp() {
title="$@"
git commit -am $title && git push -u origin
}
alias gcl='git clone'
alias gch='git checkout'
alias gbr='git branch'
alias gbrcl='git checkout --orphan'
alias gbrd='git branch -D'
function gl() {
count=$1
[[ -z "$1" ]] && count=10
git graph --no-merges | head -n $count
}
# own git workflow in hy origin with Tower
# Dev short-cuts.
# Brunch.
alias bb='brunch build'
alias bbp='brunch build --production'
alias bw='brunch w'
alias bws='brunch w --server'
alias nr='npm run'
# Package managers.
alias ni='npm install'
alias nid='npm install --save-dev'
alias nibi='npm install & bower install'
alias nibir='rm -rf {node_modules} && npm install'
alias nup='npm update'
alias ns='npm search'
alias brewup='brew update && brew upgrade'
alias jk='jekyll serve --watch' # lol jk
# alias serve='python -m SimpleHTTPServer'
alias serve='http-serve' # npm install http-server
alias server='http-serve'
alias python2='python2.7'
# Ruby.
alias bx='bundle exec'
alias bex='bundle exec'
alias migr='bundle exec rake db:migrate'
# Nginx short-cuts.
alias ngup='sudo nginx'
alias ngdown='sudo nginx -s stop'
alias ngre='sudo nginx -s stop && sudo nginx'
alias nglog='tail -f /usr/local/var/log/nginx/access.log'
alias ngerr='tail -f /usr/local/var/log/nginx/error.log'
# Checks whether connection is up.
alias net="ping ya.ru | grep -E --only-match --color=never '[0-9\.]+ ms'"
# Pretty print json
alias json='python -m json.tool'
# Burl: better curl shortcuts (https://github.com/visionmedia/burl).
if (( $+commands[burl] )); then
alias GET='burl GET'
alias HEAD='burl -I'
alias POST='burl POST'
alias PUT='burl PUT'
alias PATCH='burl PATCH'
alias DELETE='burl DELETE'
alias OPTIONS='burl OPTIONS'
fi
# Lists the ten most used commands.
alias history-stat="history 0 | awk '{print \$2}' | sort | uniq -c | sort -n -r | head"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
# PYEnv
eval "$(pyenv init -)"
# ==================================================================
# = Functions =
# ==================================================================
# Show man page in Preview.app.
# $ manp cd
function manp {
local page
if (( $# > 0 )); then
for page in "$@"; do
man -t "$page" | open -f -a Preview
done
else
print 'What manual page do you want?' >&2
fi
}
# Show current Finder directory.
function finder {
osascript 2>/dev/null <<EOF
tell application "Finder"
return POSIX path of (target of window 1 as alias)
end tell
EOF
}
# Gets password from macOS Keychain.
# $ get-pass github
function get-pass() {
keychain="$HOME/Library/Keychains/login.keychain"
security -q find-generic-password -g -l $@ $keychain 2>&1 |\
awk -F\" '/password:/ {print $2}';
}
# Opens file in EDITOR.
function edit() {
local dir=$1
[[ -z "$dir" ]] && dir='.'
$EDITOR $dir
}
alias e=edit
# Execute commands for each file in current directory.
function each() {
for dir in *; do
# echo "${dir}:"
cd $dir
$@
cd ..
done
}
# Find files and exec commands at them.
# $ find-exec .coffee cat | wc -l
# # => 9762
function find-exec() {
find . -type f -iname "*${1:-}*" -exec "${2:-file}" '{}' \;
}
# Better find(1)
function ff() {
find . -iname "*${1:-}*"
}
# Count code lines in some directory.
# $ loc py js css
# # => Lines of code for .py: 3781
# # => Lines of code for .js: 3354
# # => Lines of code for .css: 2970
# # => Total lines of code: 10105
function loc() {
local total
local firstletter
local ext
local lines
total=0
for ext in $@; do
firstletter=$(echo $ext | cut -c1-1)
if [[ firstletter != "." ]]; then
ext=".$ext"
fi
lines=`find-exec "*$ext" cat | wc -l`
lines=${lines// /}
total=$(($total + $lines))
echo "Lines of code for ${fg[blue]}$ext${reset_color}: ${fg[green]}$lines${reset_color}"
done
echo "${fg[blue]}Total${reset_color} lines of code: ${fg[green]}$total${reset_color}"
}
# Show how much RAM application uses.
# $ ram safari
# # => safari uses 154.69 MBs of RAM.
function ram() {
local sum
local items
local app="$1"
if [ -z "$app" ]; then
echo "First argument - pattern to grep from processes"
else
sum=0
for i in `ps aux | grep -i "$app" | grep -v "grep" | awk '{print $6}'`; do
sum=$(($i + $sum))
done
sum=$(echo "scale=2; $sum / 1024.0" | bc)
if [[ $sum != "0" ]]; then
echo "${fg[blue]}${app}${reset_color} uses ${fg[green]}${sum}${reset_color} MBs of RAM."
else
echo "There are no processes with pattern '${fg[blue]}${app}${reset_color}' are running."
fi
fi
}
# $ size dir1 file2.js
function size() {
# du -sh "$@" 2>&1 | grep -v '^du:' | sort -nr
du -shck "$@" | sort -rn | awk '
function human(x) {
s="kMGTEPYZ";
while (x>=1000 && length(s)>1)
{x/=1024; s=substr(s,2)}
return int(x+0.5) substr(s,1,1)
}
{gsub(/^[0-9]+/, human($1)); print}'
}
# $ git log --no-merges --pretty=format:"%ae" | stats
# # => 514 [email protected]
# # => 200 [email protected]
function stats() {
sort | uniq -c | sort -r
}
# Shortcut for searching commands history.
# hist git
function hist() {
history 0 | grep $@
}
# $ aes-enc file.zip
function aes-enc() {
openssl enc -aes-256-cbc -e -in $1 -out "$1.aes"
}
# $ aes-dec file.zip.aes
function aes-dec() {
openssl enc -aes-256-cbc -d -in $1 -out "${1%.*}"
}
# Converts a.mkv to a.m4v.
function mkv2mp4() {
for file in "$@"; do
ffmpeg -i $file -map 0 -c copy "${file%.*}.m4v"
done
}
function mkv2mp4_1() {
for file in "$@"; do
ffmpeg -i $file -map 0:0 -map 0:1 -c copy -c:s mov_text "${file%.*}.m4v"
done
}
function mkv2mp4_2() {
for file in "$@"; do
ffmpeg -i $file -map 0:0 -map 0:2 -c copy -c:s mov_text "${file%.*}.m4v"
done
}
function mkv2mp4_3() {
for file in "$@"; do
ffmpeg -i $file -map 0:0 -map 0:3 -c copy -c:s mov_text "${file%.*}.m4v"
done
}
# Adds subs from a.srt to a.m4v.
function addsubs() {
for file in "$@"; do
local raw="${file%.*}"
local old="$raw.m4v"
local new="$raw-sub.m4v"
ffmpeg -i $old -i $file -map 0 -map 1 -c copy -c:s mov_text $new
mv $new $old
rm $file
done
}
# Shortens GitHub URLs. By Sorin Ionescu <[email protected]>
function gitio() {
local url="$1"
local code="$2"
[[ -z "$url" ]] && print "usage: $0 url code" >&2 && exit
[[ -z "$code" ]] && print "usage: $0 url code" >&2 && exit
curl -s -i 'http://git.io' -F "url=$url" -F "code=$code"
}
# Monitor IO in real-time (open files etc).
function openfiles() {
sudo dtrace -n 'syscall::open*:entry { printf("%s %s",execname,copyinstr(arg0)); }'
}
# 4 lulz.
function compute() {
while true; do head -n 100 /dev/urandom; sleep 0.1; done \
| hexdump -C | grep "ca fe"
}
# Load 8 cores at once.
function maxcpu() {
dn=/dev/null
yes > $dn & yes > $dn & yes > $dn & yes > $dn &
yes > $dn & yes > $dn & yes > $dn & yes > $dn &
}
# $ retry ping google.com
function retry() {
echo Retrying "$@"
$@
sleep 1
retry $@
}
# Open curr dir in preview.app.
function preview() {
local item=$1
[[ -z "$item" ]] && item='.'
open $1 -a 'Preview'
}