This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfunctions.zsh
254 lines (216 loc) · 4.71 KB
/
functions.zsh
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
# use local npm binaries
npm-exec() {
PATH="$(npm bin):$PATH" "$@"
}
# switch npm account
# I use .npmrc.account-name so I can easily switch between public and companies accounts
npm-account() {
rm -f ~/.npmrc > /dev/null 2>&1
ln -s ~/.npmrc.$1 ~/.npmrc
}
# sudo previous command
sudothat() {
echo -e "$(tput setaf 1)sudo:$(tput sgr0) $(fc -ln -1)"
eval "sudo $(fc -ln -1)"
}
# grep with vim
vigrep() {
$EDITOR -c "Grep \"$@\""
}
# history
h() {
if [ "$#" -eq 0 ]; then
history
else
history 0 | egrep -i --color=auto $@
fi
}
# go up 'n' directories
up() {
for updirs in $(seq ${1:-1}); do
cd ..
done
}
# mkdir & cd
cdir() {
if [ ! -d "$@" ]; then
mkdir -p "$@"
fi
cd "$@"
}
# quickly add and remove '.bak' to files
bak() {
for file in "$@"; do
if [[ $file =~ "\.bak$" ]]; then
mv -iv "$file" "$(basename ${file} .bak)"
else
mv -iv "$file" "${file}.bak"
fi
done
}
# quickly duplicate things
dup() {
for file in "$@"; do
cp -f "$file" "${file}.dup"
done
}
# rename files
name() {
local newname="$1"
vared -c -p "rename to: " newname
command mv "$1" "$newname"
}
# simple httpserver
serve() {
local port="3000"
if [ "$#" -ne 0 ]; then
port="$@"
fi
if hash http-server 2> /dev/null; then
http-server -p $port -c-1
elif hash caddy 2> /dev/null; then
caddy -port "$port" browse
else
local command=""
if [ "$(uname)" = "Darwin" ]; then
command="SimpleHTTPServer"
else
command="http.server"
fi
python -m $command $port
fi
}
# simple find functions
if hash fd 2> /dev/null; then
alias fn="$(which fd) --hidden --follow --exclude .git"
alias fd="fn --type directory"
alias ff="fn --type file"
else
fn() { find . -iname "*$@*" 2>/dev/null }
fd() { find . -iname "*$@*" -type d 2>/dev/null }
ff() { find . -iname "*$@*" -type f 2>/dev/null }
fi
# extract archives
extract() {
if [[ -z "$1" ]]; then
echo "extracts files based on extensions"
elif [[ -f $1 ]]; then
case ${(L)1} in
*.tar.bz2) tar -jxvf $1 ;;
*.tar.gz) tar -zxvf $1 ;;
*.tar.xz) tar -xvf $1 ;;
*.bz2) bunzip2 $1 ;;
*.gz) gunzip $1 ;;
*.jar) unzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar -xvf $1 ;;
*.tbz2) tar -jxvf $1 ;;
*.tgz) tar -zxvf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*) echo "unable to extract '$1'"
esac
else
echo "file '$1' does not exist!"
fi
}
# sanitize permissions
sanitize() {
if [ "$#" -eq 0 ]; then
local DIR="."
else
local DIR="$@"
fi
find "$DIR" -type d -print0 | xargs -0 chmod 755
find "$DIR" -type f -print0 | xargs -0 chmod 644
}
# recompile zsh
zsh-recompile() {
autoload -Uz zrecompile
[ -f ~/.zshrc ] && zrecompile -p ~/.zshrc
[ -f ~/.zcompdump ] && zrecompile -p ~/.zcompdump
for f in ~/.zsh/*.zsh; do
zrecompile -p $f
done
}
# load zmv only when needed
mmv() {
autoload -Uz zmv
noglob zmv -W $@
}
# re-run command if failed
retry() {
until $@; do
sleep 1
echo -e "\n[$(date +'%T')] $(tput setaf 1)retrying:$(tput sgr0) $@\n"
done
}
# re-run command every second
repeatedly() {
while true; do
echo -e "[$(date +'%T')] $(tput setaf 1)running:$(tput sgr0) $@\n"
$@
echo
sleep 1
done
}
timer() {
local start=$(($(date +%s) + $(seconds $1)));
while [ "$start" -ge $(date +%s) ]; do
echo -ne "$(date -u --date @$(($start - $(date +%s))) +%H:%M:%S)\r";
sleep 0.1
done
}
stopwatch() {
local start=$(date +%s);
while true; do
echo -ne "$(date -u --date @$(($(date +%s) - $start)) +%H:%M:%S)\r";
sleep 0.1
done
}
# wiki
if [ -d $HOME/Documents/Dropbox/Wiki ]; then
alias wiki-search="v +'WikiSearch'"
wiki() {
if [ "$#" -eq 0 ]; then
v +"Wiki index"
else
v +"Wiki $1"
fi
}
# even quicker inbox access
inbox() {
local file=$HOME/Documents/Dropbox/Wiki/index.md
if [ "$#" -eq 0 ]; then
cat $file
else
echo "\n$@\n" >> $file
fi
}
fi
# faster youtbe-dl --batch-file
parallel-youtube-dl() {
cat "$@" | sort | uniq | parallel -u -j 16 --progress --eta "youtube-dl --newline {}"
}
# useful if there's a lot of files, but they download slowly
parallel-wget() {
cat "$@" | sort | uniq | parallel -u -j 16 --progress --eta "wget -c {}"
}
# check if website is up
is-up() {
if curl --silent https://downforeveryoneorjustme.com/"$1" | grep -q "just you"; then
echo "$1 is up"
else
echo "$1 is down"
fi
}
alias is-down="is-up"
# nnn
n() {
export NNN_TMPFILE=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd
nnn "$@"
if [ -f $NNN_TMPFILE ]; then
source $NNN_TMPFILE
/bin/rm $NNN_TMPFILE
fi
}