blob: a1c510e6e9818265c75c0e9588ca4e1041b89da4 (
plain) (
blame)
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
|
#!/usr/bin/env zsh
# Docker
dsh() {
# Drop into a Docker shell. Shell defaults to /bin/bash.
container=$1
cmd=${2-/bin/bash}
docker exec -it "${container}" "${cmd}"
}
# Git
conflicts() {
# Edit git conflicts one-by-one in your favorite editor.
${EDITOR} "$(git status --porcelain | awk '/^UU/ { print $2 }')"
}
# GPG
gpg_encrypt() {
# Convenience function around encryping files and directories.
# Appends a .gpg extension and deletes the unencrypted source.
local file=${1}
echo "Encrypting..."
if [ -f "${file}" ]; then
gpg --symmetric "${file}" && \
rm "${file}"
elif [ -d "${file}" ]; then
tar -cz "${file}" | gpg --symmetric --output "${file}.tar.gz.gpg"
fi
echo "Done."
}
gpg_decrypt() {
# Convenience function around decrypting .gpg files and directories.
# Deletes the original encrypted file with the .gpg extension.
local file=$1
echo "Decrypting..."
if [ -f "${file}" ]; then
gpg --decrypt "${file}" >"${file%.gpg}" && \
rm "${file}"
elif [ -d "${file}" ]; then
local outdir="${dirname%.tar.gz.gpg}"
if [ -d "${outdir}" ]; then
echo "Output directory, ${outdir}, already exists and will be overwritten by this command. Aborting..."
return 1
else
gpg --decrypt "${dirname}" | tar -xv
fi
fi
echo "Done."
}
# Python
python_sandbox() {
# Creates a nix-shell with the specified arguments as Python packages
nix-shell -p "python36.withPackages(p: with p; [$@])"
}
# Haskell
cabal_unhell() {
# Run this function to save yourself from Cabal hell.
# Note: this will require that you reinstall packages for your projects again.
find ~/.ghc -maxdepth 1 -type d -exec rm -rf {} \;
rm -rf ~/.cabal/{lib,packages,share}
}
haskell_sandbox() {
# Creates a nix-shell with the specified arguments as Haskell packages
nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [$@])"
}
_haskell_test_watch_path() {
# Runs and watches the tests for a provided file path.
ghcid -c "stack ghci grid:lib grid:grid-test --ghci-options=-fobject-code" \
--height="$(tput lines)" --width="$(tput cols)" --warnings --test "$1"
}
_haskell_test_watch_pattern() {
# Runs and watches the tests that match a provided pattern.
stack test --file-watch grid:grid-test --ta "-p \"${1}\""
}
haskell_test_watch() {
# Accepts either a filepath or a pattern and runs a test-watcher for either.
if [ -f "$1" ]; then
_haskell_test_watch_path "$1"
else
_haskell_test_watch_pattern "$1"
fi
}
# Kubernetes
kush() {
# Drop into a shell via Kubernetes. Shell defaults to /bin/bash.
local name=$1
local cmd=${2-/bin/bash}
kubectl exec -it "${name}" -- "${cmd}"
}
# Misc
path() {
# Pretty-print the $PATH variable
echo "$PATH" | tr : '\n'
}
nix_store() {
# Print the packages in /nix/store without the preceding hash
ls /nix/store | sed 's/[a-z0-9]*-//'
}
browse() {
# Open a URL in $BROWSER. Friendly for terminal input and output.
nohup "$BROWSER" $@ &
}
essids() {
# Returns a list of all ESSIDs the network card detects
local interface=${1-wlp4s0}
sudo iwlist "${interface}" scan | awk -F \" '{print $2}' | sed '/^\s*$/d'
}
mkdir_cd() {
# Make and cd into a directory or path
mkdir -p "$1" && cd "$1"
}
swap() {
# Swaps the names of files and directories.
local file_a="${1}"
local file_b="${2}"
if [ -d "${file_a}" ] && [ -d "${file_b}" ]; then
local backup=$(mktemp -d backup.XXX)
mv "${file_a}" "${backup}"
mv "${file_b}" "${file_a}"
mv "${backup}/${file_a}" "${file_b}"
rm -rf "${backup}"
elif [ -f "${file_a}" ] && [ -f "${file_b}" ]; then
local backup=$(mktemp backup.XXX)
mv "${file_a}" "${backup}"
mv "${file_b}" "${file_a}"
mv "${backup}" "${file_b}"
rm "${backup}"
fi
echo "Swapped: ${file_a} <-> ${file_b}"
}
bak() {
# Backup a file or a directory by appending a .bak extension to it.
mv "$1" "$1.bak"
}
unbak() {
# Restore a file by removing the .bak extension from it.
mv "$1.bak" "$1"
}
is_online() {
# Pings google.com and echos "Online" or "Offline" and returns the appropriate
# exit code. Could be useful in the ${PS1} variable.
wget -q --spider "http://google.com"
if [ $? -eq 0 ]; then
echo "Online"
return 0
else
echo "Offline"
return 1
fi
}
du_it_live() {
# Outputs and refreshes the size of a directory's content.
# Useful for watching a directory as large amounts of data are
# downloaded into it.
local directory="${1}"
while true; do
du -hc "${directory}" | tail -n 1 | tr -d '\n' && echo -n ' ' && sleep 0.5
# elipsis
echo -n '.' && sleep 0.5 &&
echo -n '.' && sleep 0.5 &&
echo -n '.' && sleep 0.5 &&
# clear the three-dots
echo -n '\b\b\b' && echo -n ' ' && echo -n '\r'
done
}
router() {
# Returns the IP address of the network's router.
# Useful in a call like `ping $(router)` to diagnose an internet problem.
netstat -nr | grep default | head -n 1 | awk '{ print $2 }'
}
lt() {
# Convenience wrapper around `exa --tree`.
# Optionally accepts a number for the max-depth and a directory to list.
# $ lt 2 ./scripts
# lt
if [ -z ${1} ]; then
exa --tree --all
# lt 2
elif [[ "${1}" =~ '^[0-9]+$' ]] && [ -z ${2} ]; then
local depth="${1}"
exa --tree -all --level "${depth}"
# lt ./scripts
elif [ -z ${2} ]; then
local directory="${1}"
exa --tree --all "${directory}"
# lt 2 ./scripts
else
local depth=${1}
local directory="${2}"
exa --tree --all --level ${depth} "${directory}"
fi
}
gql() {
# Convenience wrapper around `http POST` that allows you write GQL queries in
# Vim before posting them to the server.
local endpoint="${1}"
local query="/tmp/http-query.gql"
vim "${query}" && \
echo "{\"query\":\"$(cat ${query})\"}" | \
http --body POST "${endpoint}"
}
# Nix
nix_introspect() {
# Greps through my local nixpkgs repo for
rg --after-context 5 "\\b$1\\b\\s*=" "$(nix-instantiate --find-file nixpkgs)"
}
# Tmux
t() {
# Find or create a Tmux session.
local session_name="${1}"
if ! tmux has-session -t "${session_name}" 2> /dev/null; then
local oldTMUX="${TMUX}"
unset TMUX
tmux new -d -s "${session_name}" -n "${session_name}"
export TMUX="${oldTMUX}"
unset oldTMUX
if command -v j >/dev/null; then
tmux send-keys -t "${session_name}" "j ${session_name}; clear" "C-m"
else
tmux send-keys -t "${session_name}"
fi
fi
if [[ -n "${TMUX}" ]]; then
tmux switch-client -t "${session_name}"
else
tmux attach -t "${session_name}"
fi
}
tk() {
# `tk`: "tmux kill". Kills a tmux session by name.
# If no arguments are provided, kills the current session after jumping to the previous session.
session_name="${1}"
if [ ! -z "${session_name}" ]; then
tmux kill-session -t "${session_name}"
else
session_name=tmux ls -f '#{?session_attached,#{session_name},}' | xargs
tmux switch-client -l
tmux kill-session -t "${session_name}"
fi
}
# zsh
zle_insert_subshell() {
LBUFFER+='$(' ; RBUFFER=")$RBUFFER"
}
zle -N zle_insert_subshell
bindkey '^j' zle_insert_subshell
zle_insert_variable() {
LBUFFER+='${' ; RBUFFER="}$RBUFFER"
}
zle -N zle_insert_variable
bindkey '^v' zle_insert_variable
zle_insert_2x_dash() {
LBUFFER+=' --'
}
zle -N zle_insert_2x_dash
bindkey '^[^f' zle_insert_2x_dash
zle_insert_2x_quote() {
LBUFFER+=' "' ; RBUFFER="\"$RBUFFER"
}
zle -N zle_insert_2x_quote
bindkey '^["' zle_insert_2x_quote
zle_insert_quote() {
LBUFFER+=" '" ; RBUFFER="'$RBUFFER"
}
zle -N zle_insert_quote
bindkey "^['" zle_insert_quote
|