-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions
325 lines (279 loc) · 7.24 KB
/
functions
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
#!/bin/bash
# external functions
source "$HOME/dotfiles/fns/tab.bash"
function reload-zshrc {
source $HOME/.zshenv
source $HOME/.zshrc
}
function docker-remove-all-untagged-images {
docker rmi $(docker images | grep "^<none>" | awk '{print $3}')
}
function chrome () {
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome $* 2>&1 &
}
# week-number
function week-number {
if [ -z "$1" ]
then
date +%V
else
date -d $1 +%V
fi
}
#[[ $SHELL = "/bin/bash" ]] && echo is bash
#[[ $SHELL = "/bin/zsh" ]] && echo is zsh
# ssh autocomplete
[[ $SHELL = "/bin/bash" ]] && complete -W "$(echo $(cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | sort -u | grep -v "\["))" ssh
# git autocomplete
case $(uname) in
Darwin)
[[ $SHELL = "/bin/bash" ]] && source /usr/local/etc/bash_completion.d/git-completion.bash
source /usr/local/etc/bash_completion.d/git-prompt.sh
;;
esac
function __git_prompt {
GIT_PS1_SHOWDIRTYSTATE=1
__git_ps1 " %s" | sed 's/ \([+*]\{1,\}\)$/\1/'
}
# clone github repo url and open project in macvim
function cloneopen_github_repo {
URL=$1
DATE=`date`
mkdir -p ~/Code/github/
cd ~/Code/github/
REPO=`echo $URL | awk '{ split($0, url, "/"); print url[4] "-" url[5] }'`
FOLDER=`echo "$REPO-$DATE" | sed -E "s/[ :]/-/g"`
git clone $URL".git" $FOLDER
mvim $FOLDER
}
function bell {
echo -ne '\007'
}
function git-ignore-file-in-local-index() {
git update-index --assume-unchanged "$@"
}
function git-find-commit-with-text() {
git log -S $1 --source --all --reverse
}
# copy with progress
cp_p () {
pv < $1 > $2;
}
# mkdir and cd
function md () {
mkdir -p "$@" && cd "$@"
}
# find shorthand
function f () {
find . -name "$1"
}
# pretty print json
# function json () {
# if [ -p /dev/stdin ]; then # piping
# json
# else
# json "$*"
# fi
# }
# export repo without .git
function gitexport () {
mkdir -p "$1"
git archive master | tar -x -C "$1"
}
# gzipped size
function gz () {
ORIG=`cat "$1" | wc -c | sed -E "s/ //g"`
GZIP=`gzip -c "$1" | wc -c | sed -E "s/ //g"`
echo "$ORIG => $GZIP"
}
# escape utf-8 char into 3-byte format
function escape () {
printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u)
echo # newline
}
# decode \x{ABCD} unicode escape sequences
function unidecode () {
perl -e "binmode(STDOUT, ':utf8'); print \"$@\""
echo # newline
}
# extract archives
function extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $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
}
# marks - jump around folders
function jump {
cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
}
function mark {
mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
}
function unmark {
rm -i "$MARKPATH/$1"
}
function marks {
\ls -l "$MARKPATH" | tail -n +2 | sed 's/ / /g' | cut -d' ' -f9- | awk -F ' -> ' '{printf "%-10s -> %s\n", $1, $2}'
}
# unmarks and marks the current directory
function mark-cur {
yes | unmark cur
mark cur
}
function isup {
nc -zG 3 -L 2 $1 $2 &> /dev/null && echo "$1:$2 up"
}
# autocomplete marks
_completemarks() {
local curw=${COMP_WORDS[COMP_CWORD]}
local wordlist=$(gfind $MARKPATH -type l -printf "%f\n")
COMPREPLY=($(compgen -W '${wordlist[@]}' -- "$curw"))
return 0
}
#complete -F _completemarks jump unmark
# serve as web root
function serve {
open http://localhost:3000/ &
http-server . -p 3000
}
function prompt {
q="- $(date +%T)"; while [[ ${#q} -lt $COLUMNS ]]; do q="${q:0:1}$q"; done; echo -e "\033[0;90m$q"
}
function ips {
ifconfig | grep 'inet ' | grep -v 127 | awk '{print $2}'
ip
}
function cd () {
builtin cd "$@";
la;
}
function gr () {
git rev-parse --show-toplevel
}
function share () {
scp $1 wa.gd:~/public/
}
function share-vid-as-gif () {
gifify $1 --good
share $1".gif"
echo "public.wa.gd/$1.gif" | pbcopy
}
# Change working directory to the top-most Finder window location
function cdf () {
cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')";
}
# animated gifs from any video
# from alex sexton gist.github.com/SlexAxton/4989674
gifify() {
if [[ -n "$1" ]]; then
if [[ $2 == '--good' ]]; then
ffmpeg -i $1 -r 10 -vcodec png out-static-%05d.png
time convert -verbose +dither -layers Optimize -resize 900x900\> out-static*.png GIF:- | gifsicle --colors 128 --delay=5 --loop --optimize=3 --multifile - > $1.gif
rm out-static*.png
else
ffmpeg -i $1 -s 600x400 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=3 > $1.gif
fi
else
echo "proper usage: gifify <input_movie.mov>. You DO need to include extension."
fi
}
# take a photo, use this to find the interface
# ffmpeg -f avfoundation -list_devices true -i ""
function take-photo {
ffmpeg -ss 0.5 -r 10 -f avfoundation -i 0 -frames:v 1 -s 1280x720 ~/Desktop/capture.jpg
}
function youtube-dl-mp4 {
if [[ -n "$1" ]]; then
youtube-dl --recode-video mp4 $1
fi
}
# vterm modifications for emacs
# https://github.com/akermu/emacs-libvterm#message-passing
vterm_printf(){
if [ -n "$TMUX" ] && ([ "${TERM%%-*}" = "tmux" ] || [ "${TERM%%-*}" = "screen" ] ); then
# Tell tmux to pass the escape sequences through
printf "\ePtmux;\e\e]%s\007\e\\" "$1"
elif [ "${TERM%%-*}" = "screen" ]; then
# GNU screen (screen, screen-256color, screen-256color-bce)
printf "\eP\e]%s\007\e\\" "$1"
else
printf "\e]%s\e\\" "$1"
fi
}
vterm_cmd() {
local vterm_elisp
vterm_elisp=""
while [ $# -gt 0 ]; do
vterm_elisp="$vterm_elisp""$(printf '"%s" ' "$(printf "%s" "$1" | sed -e 's|\\|\\\\|g' -e 's|"|\\"|g')")"
shift
done
vterm_printf "51;E$vterm_elisp"
}
findfile() {
case $(uname) in
Darwin)
# torgeir: realpath is from brew coreutils
vterm_cmd find-file "$(/usr/local/opt/coreutils/libexec/gnubin/realpath "${@:-.}")"
;;
Linux)
vterm_cmd find-file "$(/usr/bin/realpath "${@:-.}")"
;;
esac
}
vterm_set_directory() {
vterm_cmd update-pwd "$PWD/"
}
gs() {
vterm_cmd magit-status "$(/usr/local/opt/coreutils/libexec/gnubin/realpath "${@:-.}")"
}
if [[ "$INSIDE_EMACS" = 'vterm' ]]; then
alias clear='vterm_printf "51;Evterm-clear-scrollback";tput clear'
fi
# set global java version
jdk() {
local version=$1
case $(uname) in
Darwin)
export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
;;
Linux)
sudo archlinux-java set $version
;;
esac
java -version
}
# list available Java versions
jdks() {
case $(uname) in
Darwin)
/usr/libexec/java_home -V 2>&1 | sed 1d | sed '$d' | cut -d, -f1
;;
Linux)
ls $(readlink -f /usr/bin/javac | sed "s:/bin/javac::")/.. | grep java-
;;
esac
}
# Automatically change java version from .java-version file
autoload -U add-zsh-hook
_jdk_autoload_hook() {
if [[ -f .java-version ]]; then
echo "Found local .java-version in folder, switching to java version $(cat .java-version)"
jdk "$(cat .java-version)"
fi
}
add-zsh-hook chpwd _jdk_autoload_hook