-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-aliases
377 lines (335 loc) · 9.7 KB
/
bash-aliases
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
# shellcheck shell=bash
# -*- mode: sh; -*-
# vi: set ft=sh :
alias ..='cd ..'
alias .2='cd ../..'
alias .3='cd ../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'
alias b='btop'
alias curldump='curl --trace -'
alias delpyc='find . -name '\*.pyc' -ls -exec rm {} \;'
alias dps='docker ps'
alias du='du -h -c'
alias du1='du -h --max-depth=1'
alias h='history | ${GREPPAGE} -i'
alias kcC='keychain --clear' # Safely decache keychain keys.
alias l='ls -halG --color'
alias lift='sudo $(history -p \!\!)' # Sudo previous command.
alias myip='curl --silent http://tnx.nl/ip; printf "\n"'
alias mv='mv -iv'
alias n='ncmpcpp -s playlist -S browser'
alias np='mpc toggle'
alias p='ipython'
alias pmg='cd $(gh) && ./manage.py migrate'
alias psh='pipenv shell'
alias pt='py.test'
alias pts='py.test -s'
alias qp='ping -c 5 8.8.8.8'
alias rds='cd $(gh) && ./manage.py runserver_plus --configuration=Dev'
alias rdsql='cd $(gh) && ./manage.py runserver_plus --print-sql --configuration=Dev'
alias reload='source ${HOME}/.bash_profile'
alias rs='cd $(gh) && ./manage.py runserver'
alias tree='tree -I "node_modules|__pycache__|.git|*venv"'
alias whoami-aws='aws sts get-caller-identity'
alias yt='yarn test'
alias v='${VISUAL}'
alias vf='f -e ${VISUAL}' # Quick open with fasd + vim.
alias vv='${EDITOR} ${HOME}/.vimrc'
alias suv='sudo ${VISUAL}'
[[ "${IS_LINUX}" == true ]] && {
alias exm='ec ${HOME}/.xmonad/xmonad.hs'
alias shclr='msgcat --color=test'
alias shred='shred -zuv'
alias sue='sudo emacs -nw'
alias tv1='xset s off && xset -dpms && echo "screen powersaving off..."'
alias tv0='xset s default && xset +dpms && echo "screen powersaving on..."'
alias x='startx'
}
[[ "${IS_OSX}" == true ]] && {
alias ls='gls'
alias shred='gshred -zuv'
alias su='sudo su -'
}
# Usage: aws-login-sso $AWS_PROFILE $ENVIRONMENT
aws-login-sso() {
if [ "$#" -ne 2 ]; then
# shellcheck disable=SC2016
echo 'Usage: aws-login-sso $AWS_PROFILE $ENVIRONMENT'
return 1
fi
aws sso login --profile "${1}"
export AWS_PROFILE="${1}" ENVIRONMENT="${2}"
err "AWS_PROFILE -> ${1}"
err "ENVIRONMENT -> ${2}"
aws sts get-caller-identity
}
# Usage: confirm-potentially-stupid
confirm-potentially-stupid() {
echo "DESTRUCTIVE OPERATION - Are you sure? [y/N] " && \
read -r ans && [ $"${ans:-N}" = y ]
}
# Usage: cp-p $SOURCE $DESTINATION
# Copy with progress info.
cp-p() {
rsync -WavP --human-readable --progress "$1" "$2";
}
# Usage: create-tf-module $DIRECTORY $MODULE
# Create a Terraform module with a well-specified structure.
#
# The resulting module doesn't cleave to 'terraform_standard_module_structure'
# as understood by tflint (that shape is too constrained for my tastes so I
# have some additional files) so you'll need to disable that rule if you are
# linting your Terraform.
create-tf-module() {
if [ "$#" -ne 2 ]; then
echo "Usage: create-tf-module <directory> <module>"
return 1
fi
local dir="$1"
local module="$2"
local module_dir="${dir}/${module}"
if [ -z "$dir" ] || [ -z "$module" ]; then
err "Both directory and module arguments must be provided"
return 1
fi
local readme="${module_dir}/README.md"
if [ -e "${readme}" ]; then
err "File ${path} already exists. Skipping."
else
touchp "${readme}"
echo "# ${module^}" > "${readme}"
err "Created ${readme}"
fi
local filenames=('data' 'iam' 'labels' 'main' 'outputs' 'variables' 'versions')
for filename in "${filenames[@]}"; do
path="${module_dir}/${filename}.tf"
if [ -e "${path}" ]; then
err "File ${path} already exists. Skipping."
continue
else
touchp "${path}"
if [ "${filename}" == 'data' ]; then
echo "# Data sources for the ${module} module." > "${path}"
elif [ "${filename}" == 'iam' ]; then
echo "# IAM roles and permissions for the ${module} module." > "${path}"
elif [ "${filename}" == 'main' ]; then
echo "# Entrypoint for the ${module} module." > "${path}"
elif [ "${filename}" == 'versions' ]; then
echo "# Providers for the ${module} module." > "${path}"
else
echo "# ${filename^} for the ${module} module." > "${path}"
fi
fi
err "Created ${path}"
done
}
# Usage dsh $CONTAINER
# Shell into a docker container.
dsh() {
docker exec -it "${1}" sh
}
# Usage: err $MESSAGE
# Stderr helper function.
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2;
}
# Usage: extract $COMPRESSED-FILE
extract() {
if [[ -f "${1}" ]]; then
case "${1}" in
*.tar.bz2) tar xvjf "${1}" ;;
*.tar.gz) tar xvzf "${1}" ;;
*.bz2) bunzip2 "${1}" ;;
*.rar) unrar x "${1}" ;;
*.gz) gunzip "${1}" ;;
*.tar) tar xvf "${1}" ;;
*.tbz2) tar xvjf "${1}" ;;
*.tgz) tar xvzf "${1}" ;;
*.zip) unzip "${1}" ;;
*.Z) uncompress "${1}" ;;
*.7z) 7z x "${1}" ;;
*) echo "${1} cannot be extracted via >extract<" ;;
esac
else
err "Error: ${1} is not a valid file"
fi
}
# Usage: ff $DIR-SPECIFIER
# Find directories.
# TODO: Consider using '*/\.*'
fd() {
find . \
-not -iwholename '*.git*' \
-not -iwholename '*.mypy_cache*' \
-not -iwholename '*.terragrunt-cache*' \
-not -iwholename '*node_modules*' \
-not -iwholename '*venv*' \
-not -name '*.pyc' \
-type d -iname "*""$*""*";
}
# Usage: ff $FILE-SPECIFIER
# Find files.
ff() {
find . \
-not -iwholename '*.git*' \
-not -iwholename '*.mypy_cache*' \
-not -iwholename '*.terragrunt-cache*' \
-not -iwholename '*node_modules*' \
-not -iwholename '*venv*' \
-not -name '*.pyc' \
-type f -iname "*""$*""*";
}
# Usage: frep $REGEX $FILE-SPECIFIER
# Grep for patterns within the results of a call to `find'.
frep() {
find . \
-not -iwholename '*.git*' \
-not -iwholename '*.mypy_cache*' \
-not -iwholename '*.terragrunt-cache*' \
-not -iwholename '*node_modules*' \
-not -iwholename '*venv*' \
-not -name '*.pyc' \
-name "${2}" -print0 \
| xargs -0 "${GREPPAGE}" --pretty -i "${1}" \
| less;
}
# Usage: fx $FILE-SPECIFIER $COMMAND
# Execute a command over the results of a call to `find'.
fx() {
find . \
-not -iwholename '*.git*' \
-not -iwholename '*.mypy_cache*' \
-not -iwholename '*.terragrunt-cache*' \
-not -iwholename '*node_modules*' \
-not -iwholename '*venv*' \
-not -name '*.pyc' \
-type f -iname "*""${1:-}""*" \
-exec "${2:-file}" {} \; ;
}
# Usage: fwc $FILE-SPECIFIER
# Find files and count the number of lines.
fwc() {
find . -name "*.${1}" -print0 \
| xargs -0 wc -l;
}
# Usage: grep-less $REGEX
# Grep for patterns under cwd and pipe the results through the pager.
grep-less() {
if command -v 'rg' >/dev/null 2>&1; then
"${GREPPAGE}" --pretty --hidden \
-g '!.git/' \
-g '!.venv/' \
-g '!static/' \
-g '!yarn.lock' \
-g '!node_modules/' \
-g '!.terragrunt-cache/' \
-i "$@" \
| ${PAGER}
else
"${GREPPAGE}" --color -r "$@" | ${PAGER}
fi
}
alias g='grep-less'
# Usage: grep-todo
# Show all 'todo' items relative to cwd.
grep-todo() {
grep-less "todo" .
}
alias todo='grep-todo'
# Usage: kill-port $PORT
# Kill a bound port.
kill-port() {
lsof -t -i tcp:"$1" | xargs kill -9
}
# Usage: pskill $PID
# Hard kill a running process.
pskill() {
local pid
pid=$(pgrep -f "$1")
printf "Killing: %s\\n" "$pid"
kill -9 "$pid"
}
# Usage: scan-ports
# List all files listening on ports.
scan-ports() {
sudo lsof -i -n -P | grep LISTEN
}
# Usage: shellcheck-add-shebang $SHELL $FILE
shellcheck-add-shebang() {
if command -v 'gsed' >/dev/null 2>&1; then
sed -i "1s/^/# shellcheck shell=$1\\n" "$2"
else
echo 'Install gnu-sed!' >&2
fi
}
# Usage: sreboot
[[ "${IS_LINUX}" == true ]] &&
sreboot() {
if [[ "t" == "$(server_ok)" ]]; then
echo "Shutting down the emacs server..."
emacsclient -e '(client-save-kill-emacs)'
fi
echo "Shutting down MPD..."
mpd --kill
sudo shutdown -r now
}
# Usage: generate-timestamped-ssh-key $DOMAIN
ssh-generate-timestamped-key() {
if [[ -n $1 ]]; then
local timestamp ssh_host
timestamp="$(date -u +"%Y%m%dT%H%M%Z")"
ssh_host="$(whoami)@$(hostname)--$1-$timestamp-rsa"
ssh-keygen -f "${HOME}/.ssh/$ssh_host"
else
err "Please provide the ssh server's domain name"
fi
}
# Usage: touchp $DIRECTORY/$FILENAME
touchp() {
for file in "$@"; do
mkdir -p "$(dirname "${file}")" && touch "${file}"
done
}
# Usage: vim-add-plugins $REPO-URL
vim-add-plugins() {
for repo in "$@"; do
git -C "${HOME}/.vim/pack/jhrr/start" submodule add "${repo}"
done
}
# Usage: vim-is-running
# Enumerate any Vim processes running in the shell.
vim-is-running() {
local proc
proc=$(tty | sed -e "s:/dev/::")
ps \
| "${GREPPAGE}" vim \
| "${GREPPAGE}" -v rg \
| "${GREPPAGE}" "$proc";
}
alias iv='vim-is-running'
# Usage: vim-nuke-all
# A brute force way to kill all instances of Vim at once.
# We wouldn't need such a murderous device if we just got to grips with tmux
# properly instead of being such a ctrl-z lazybones.
vim-nuke-all() {
iv | awk '{ print $1 }' | xargs kill -9
}
# Usage: vim-remove-plugin $PLUGIN
vim-remove-plugin() {
git -C "${HOME}/.vim/pack/jhrr/start" submodule deinit -f -- "${1}"
git -C "${HOME}/.vim/pack/jhrr/start" rm -f "${1}"
rm -rf ".git/modules/vim/pack/jhrr/start/${1}"
}
# Usage: vim-update-plugins
vim-update-plugins() {
git -C "${HOME}/.vim/pack/jhrr/start" submodule update --remote --merge;
}
# Usage: vrep REGEX FILE-SPECIFIER
# Grep for patterns and open matches for editing.
vrep() {
${GREPPAGE} -il "$@" | while IFS='' read -r line
do
xargs -o "${VISUAL}" "${line}"
done
}