-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions
329 lines (278 loc) · 8.11 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
326
327
328
329
# vi: ft=bash
# 8 8888888888 8 8888 88 b. 8 ,o888888o.8888888 8888888888 d888888o.
# 8 8888 8 8888 88 888o. 8 8888 `88. 8 8888 .`8888:' `88.
# 8 8888 8 8888 88 Y88888o. 8 ,8 8888 `8. 8 8888 8.`8888. Y8
# 8 8888 8 8888 88 .`Y888888o. 8 88 8888 8 8888 `8.`8888.
# 8 888888888888 8 8888 88 8o. `Y888888o. 8 88 8888 8 8888 `8.`8888.
# 8 8888 8 8888 88 8`Y8o. `Y88888o8 88 8888 8 8888 `8.`8888.
# 8 8888 8 8888 88 8 `Y8o. `Y8888 88 8888 8 8888 `8.`8888.
# 8 8888 ` 8888 ,8P 8 `Y8o. `Y8 `8 8888 .8' 8 8888 8b `8.`8888.
# 8 8888 8888 ,d8P 8 `Y8o.` 8888 ,88' 8 8888 `8b. ;8.`8888
# 8 8888 `Y88888P' 8 `Yo `8888888P' 8 8888 `Y8888P ,88P'
# Create a new directory and enter it
function md() {
mkdir -p "$@" && cd "$@"
}
# Fussily jump to any directory of importance
function d() {
# Config directories
local dirs="$HOME/dotfiles"
dirs+="\n$HOME/dotfiles/.config/nvim"
dirs+="\n$HOME/.local/share/nvim/"
dirs+="\n~/Library/Mobile\ Documents/com\~apple\~CloudDocs"
# Personal and work projects
for parent in ~/code ~/brightin ~/pilloxa ~/spronq
do
dirs+="\n"
dirs+=$(find $parent -type d -maxdepth 1 -mindepth 1)
done
selected=$(echo $dirs | fzf --preview 'tree -L 1 -C {} | head -200')
if [ -n "$selected" ]; then
cd $selected
fi
}
# find shorthand
function f() {
mdfind -onlyin ./ "kMDItemDisplayName == $1"
}
# cd into forefront finder window
cdf() { # short for cdfinder
cd "`osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)'`"
}
# Print the forefront finder window
function pwf() {
osascript 2>/dev/null <<EOF
tell application "Finder"
return POSIX path of (target of window 1 as alias)
end tell
EOF
}
# Start an HTTP server from a directory, optionally specifying the port
function server() {
local port="${1:-8000}"
open "http://localhost:${port}/"
# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
}
function bat_themes() {
bat --list-themes | fzf --preview="bat --theme={} --color=always ${1}"
}
# Copy w/ progress
cp_p () {
rsync -WavP --human-readable --progress $1 $2
}
# Syntax-highlight JSON strings or files
function json() {
if [ -p /dev/stdin ]; then
# piping, e.g. `echo '{"foo":42}' | json`
python -mjson.tool | pygmentize -l javascript
else
# e.g. `json '{"foo":42}'`
python -mjson.tool <<< "$*" | pygmentize -l javascript
fi
}
function getline() {
if [[ -z "$1" || -z "$2" ]]; then
echo "Usage: getline <line_number> <file_path>"
return 1
fi
sed -n "${1}p" $2
}
# get gzipped size
function gz() {
echo "orig size (bytes): "
cat "$1" | wc -c
echo "gzipped size (bytes): "
gzip -c "$1" | wc -c
}
# Extract archives - use: extract <file>
# Based on http://dotfiles.org/~pseup/.bashrc
function extract() {
if [ -f "$1" ] ; then
local filename=$(basename "$1")
local foldername="${filename%%.*}"
local fullpath=`perl -e 'use Cwd "abs_path";print abs_path(shift)' "$1"`
local didfolderexist=false
if [ -d "$foldername" ]; then
didfolderexist=true
read -p "$foldername already exists, do you want to overwrite it? (y/n) " -n 1
echo
if [[ $REPLY =~ ^[Nn]$ ]]; then
return
fi
fi
mkdir -p "$foldername" && cd "$foldername"
case $1 in
*.tar.bz2) tar xjf "$fullpath" ;;
*.tar.gz) tar xzf "$fullpath" ;;
*.tar.xz) tar Jxvf "$fullpath" ;;
*.tar.Z) tar xzf "$fullpath" ;;
*.tar) tar xf "$fullpath" ;;
*.taz) tar xzf "$fullpath" ;;
*.tb2) tar xjf "$fullpath" ;;
*.tbz) tar xjf "$fullpath" ;;
*.tbz2) tar xjf "$fullpath" ;;
*.tgz) tar xzf "$fullpath" ;;
*.txz) tar Jxvf "$fullpath" ;;
*.zip) unzip "$fullpath" ;;
*) echo "'$1' cannot be extracted via extract()" && cd .. && ! $didfolderexist && rm -r "$foldername" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# turn that video into webm.
# brew reinstall ffmpeg --with-libvpx
webmify(){
ffmpeg -i $1 -vcodec libvpx -acodec libvorbis -isync -copyts -aq 80 -threads 3 -qmax 30 -y $2 $1.webm
}
# Open a file in Google Chrome
chrome() {
open -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome $1
}
# Open a file in FireFox
firefox() {
open -a /Applications/Firefox.app/Contents/MacOS/firefox $1
}
# Allows to write commit messages without the quotes (thanks Orenstein - http://www.github.com/r00k)
function gc {
git commit -m "$*"
}
# go to the node-docs of current node version
node-docs () {
local open_cmd
if [[ "$OSTYPE" = darwin* ]]
then
open_cmd='open'
else
open_cmd='xdg-open'
fi
$open_cmd "http://nodejs.org/docs/$(node --version)/api/all.html#all_$1"
}
# print out the path of a given file
function printpath {
if (($# == 0)); then
pwd
else
echo $(cd $(dirname $1); pwd)/$(basename $1)
fi
}
function copypath {
if [ "$TMUX" ]; then
local attach='reattach-to-user-namespace'
fi
local filepath=$(printpath $1)
echo $filepath | $attach pbcopy
echo "Copied '$filepath' to clipboard"
}
# Search through history
function hs {
history | grep $*
}
# Symbolic link to realpath of file
function link {
ln -s "$(pwd)/$1" "$2"
}
# Follow binary symlinks
function where {
realpath $(which "$1")
}
function goto {
cd $(dirname $(where "$1"))
}
function realpath() {
for f in "$@"; do echo ${f}(:A); done
}
# use cheats in a lesser format :)
function ch() {
cheat "$1" | less
}
# Go up x number of dirs (e.g: $ up 4)
function up() {
DEEP=$1;
[ -z "${DEEP}" ] && { DEEP=1; };
for i in $(seq 1 ${DEEP}); do cd ../; done;
}
# Startup a tmuxinator project with the same name as current dir
function tmn() {
tmuxinator start ${PWD##*/}
}
function _rspec_command() {
if [ -e "bin/rspec" ]
then
bin/rspec $@
elif [ -e "script/rspec" ]
then
ruby script/rails $@
else
command rails $@
fi
}
function paste_in_keynote() {
highlight -O rtf $1 --syntax=clojure --font Inconsolata -s $1 | pbcopy
osascript <<'END'
tell application "Keynote" to activate
tell application "System Events"
tell process "Keynote"
keystroke "v" using command down
end tell
end tell
tell application "iTerm" to activate
END
}
function find-dir() {
find ./ -type d -name "*$1*" -print
}
function current_git_branch() {
git branch 2> /dev/null | awk '{ if ( $1 == "*" ) { print $2 } }'
}
function reset_origin() {
git reset origin/$(current_git_branch)
}
# Rebases all commits starting from the mergebase with the first argument, and
# applies them to the second argument ref
# Ex:
# # Will rebase all commits started from master onto prouduction
# $ rebase_onto master production
function rebase_onto() {
git rebase --onto "$2" `git merge-base HEAD "$1"` `current_git_branch`
}
# Shows the diff between HEAD and the merge base with the branch
function gh_diff() {
git diff "$1"...HEAD
}
function git_date() {
git rev-list -n 1 --first-parent --before="$1 23:59" master
}
function tomp3() {
ffmpeg -i $1 -vn -ar 44100 -ac 2 -ab 192k -f mp3 $2
}
# NOT WORKING, WHY?
recursive_replace() {
Ag -l "'$1'" | xargs sed -i '' -e 's/$2/$3/'
}
compress_vid () {
ffmpeg -i "$1" -c:v libx264 -preset slow -crf 22 -c:a copy "$2"
}
function bisect() {
git bisect start && git bisect good "$1" && git bisect bad "$2" && git bisect run "$3"
}
function resetdb() {
dropdb $1 && createdb $1;
psql $1 < $2
}
function awsv() {
local env=$1;
shift;
aws-vault exec $env --assume-role-ttl=60m -- $@;
}
function interval() {
local interval=$1;
shift;
$@;
while sleep $interval;
do
$@;
done
}