-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_aliasrc
415 lines (384 loc) · 9.93 KB
/
dot_aliasrc
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/bin/bash
# common shell alias / functions
# author: delucks
# use freely, share and contribute
cmd_exists() { test -f "$(command -v "$1")"; }
export LS_COLORS=
export LC_ALL="en_US.UTF-8"
if cmd_exists vim
then
export EDITOR='vim'
else
export EDITOR='vi'
fi
export PAGER='less -r'
_OS_TYPE="$(uname)" # For use in further OS detection instead of bash OSTYPE
# add a directory to PATH, but only if it exists and if it's not already in PATH
pathappend() {
for pathname in "$@"; do
if ! test -d "$pathname"; then
continue
fi
case "$PATH" in
*:$pathname:*)
# path exists in the middle
continue
;;
*:$pathname)
# path exists on the end
continue
;;
$pathname:*)
# path exists in the beginning
continue
;;
"$pathname")
# this is the only entry in PATH
continue
;;
*)
# path does not yet exist in PATH
PATH="${PATH:+"$PATH:"}$pathname"
;;
esac
done
}
pathprepend() {
for pathname in "$@"; do
if ! test -d "$pathname"; then
continue
fi
case "$PATH" in
*:$pathname:*)
# path exists in the middle
continue
;;
*:$pathname)
# path exists on the end
continue
;;
$pathname:*)
# path exists in the beginning
continue
;;
"$pathname")
# this is the only entry in PATH
continue
;;
*)
# path does not yet exist in PATH
PATH="${pathname}${PATH:+":$PATH"}"
;;
esac
done
}
# source a file, but only if it exists
sourcep() {
if [ $# -lt 1 ]
then
echo "Usage: sourcep <path to source>"
return 1
fi
if test -f "$1"
then
# This is intentionally sourcing a dynamic path, which we don't need shellcheck to cover
# shellcheck disable=SC1090
. "$1"
return 0
fi
return 1
}
# This file includes fallbacks if delucks/multitool isn't installed in ~/bin
# Also, prepending here allows me to drop scripts into ~/bin and override system executables by name
pathprepend "$HOME/bin"
# cd
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'
if cmd_exists git
then
alias gita='git add'
alias gitd='git diff'
alias gits='git status'
alias gitl='git log --all --graph --pretty=format:"%Cred%h%Creset %C(bold blue)%an%Creset :%C(yellow)%d%Creset %s %Cgreen(%cr) %Creset"'
fi
# platform detection
case "$_OS_TYPE" in
Linux*)
# linux specific aliases
alias ls='ls --color=auto'
alias la='ls -aChkopl --group-directories-first --color=auto'
alias errno="grep '#define' /usr/include/asm-generic/errno.h"
ununix() {
date -d "@$1"
}
;;
Darwin*)
# osx
alias ls='ls -G'
alias la='ls -aChGkopl --group-directories-first'
alias ununix='date -r'
alias errno="grep '#define' /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/errno.h"
;;
*)
alias la='ls -aChkopl --group-directories-first'
;;
esac
alias l='exa -l -h'
# This adds an argument to BSD paste to accept stdin by default
# The only use case I have for paste is joining standard input
mypaste() {
if [ "$_OS_TYPE" = "Linux" ]
then
paste "$@"
else
paste "$@" -
fi
}
alias paste=mypaste
alias lt='ls -lt'
alias :q='exit'
alias strings='strings -a'
alias trace='mtr -rw4 -c 5'
alias whereami='echo "$(uname -n) in $(pwd)"'
alias d='date +%R'
alias wgot='wget -e robots=off -r -nc -np '
alias rot13='tr "a-zA-Z" "n-za-mN-ZA-M"'
alias mkdir='mkdir -p'
alias datehelp='for F in {a..z} {A..Z} :z ::z :::z;do echo $F: $(date +%$F);done|sed "/:[\ \t\n]*$/d;/%[a-zA-Z]/d"'
alias ve='source $(find $(git rev-parse --show-toplevel 2>/dev/null) -name activate | grep -v hooks | head -1) &> /dev/null'
alias apropy="pydoc -k"
alias apropos="man -k" # yeah, some platforms don't do this
alias meh='echo "¯\_(ツ)_/¯"'
alias whom='who'
alias tmuxa='tmux a'
alias sl='ls'
if cmd_exists ncdu; then
alias ncdu='ncdu --color dark'
fi
alias j='jrnl --edit -1'
copyjrnl() {
jrnl --ex md -1 | jrnl
}
ssh-addall() {
ssh-add $(grep IdentityFile ~/.ssh/config | sed 's/.*\///g' | awk -v dir="$HOME/.ssh/" '{print dir$NF}' | sort -u | while read line; do test -f "$line" && echo "$line"; done)
}
# tx and ta are my two tmux manipulation functions
#
# tx() creates a tmux session at a path, the current directory by default
# ta() attaches a tmux session (using fzf if available)
tx() {
# Opens a tmux session named after the basename of a path
DIR="${1:-.}"
RESOLVED="$(cd "$DIR" 2>/dev/null && pwd -P)" # Cross-platform, readlink is Linux specific
SESSION_NAME="$(basename "$RESOLVED" | tr '.' '_')"
# Older versions of tmux do not support the "-c dir_for_session" flag
cd "$RESOLVED" && tmux new-session -A -s "${SESSION_NAME}"
}
ta() {
if cmd_exists fzf; then
# shellcheck disable=SC2033
SESSION=$(tmux ls -F '#{session_name}' | fzf)
test -z "$SESSION" && return 1
tmux attach-session -t "$SESSION"
return $?
fi
tmux a
}
if ! cmd_exists log
then
log() {
echo "$@"
}
fi
if cmd_exists figlet
then
alias big="figlet -t"
figletsample() {
for font in $(find /usr -name '*.flf' -type f | sort -u); do echo "$font"; echo "${1:-sample}" | figlet -f "$font"; echo; echo; echo; done
}
fi
ssidgrep() {
if [ $# -lt 1 ]; then
log "Usage: ssidgrep <Regex matching SSID from network management program" err
return 1
fi
if cmd_exists netctl
then
netctl list | grep -E "$1" | awk '{print $NF}' | xargs -L 1 sudo netctl start
elif cmd_exists nmcli
then
nmcli c show | grep -E "$1" | awk '{print $2}' | xargs -L 1 sudo nmcli c up
else
log "ssidgrep requires either NetworkManager or systemd-netctl to be installed" err
return 1
fi
}
if cmd_exists jq
then
jless() {
# use jq to format & colorize json, then display it inside `less`
if [ $# -lt 1 ]
then
# with no arguments, jless expects stdin to be json
jq -C . | less -rN
elif [ $# -lt 2 ]
then
# single argument: a file to view
jq -C . "$1" | less -rN
else
# two arguments: the first is a file to read and the second is a jq pattern
jq -C "$2" "$1" | less -rN
fi
}
fi
if cmd_exists curl
then
# get approximate geo-location of IP from public API
iploc() {
if cmd_exists jq
then
curl -s "https://ipinfo.io/$(dig "$1" +short)" | jq .
else
curl -s "https://ipinfo.io/$(dig "$1" +short)"
fi
}
# wikipedia summary
wiki() {
if [ $# -lt 1 ]
then
log "Usage: wiki <search string>" error
return 1
else
curl -s -L -d "search=$1" https://en.wikipedia.org/w/index.php | w3m -T text/html -dump | less
fi
}
alias headers='curl -sLD - -o /dev/null'
fi
# applies standard coloration for pager programs
less_color_env() {
CMD=$1
shift
# shellcheck disable=SC2046
env \
LESS_TERMCAP_mb=$(printf "\\e[1;31m") \
LESS_TERMCAP_md=$(printf "\\e[1;31m") \
LESS_TERMCAP_me=$(printf "\\e[0m") \
LESS_TERMCAP_se=$(printf "\\e[0m") \
LESS_TERMCAP_so=$(printf "\\e[1;44;33m") \
LESS_TERMCAP_ue=$(printf "\\e[0m") \
LESS_TERMCAP_us=$(printf "\\e[1;32m") \
"$CMD" "$@"
}
alias pydoc2='less_color_env pydoc2'
alias pydoc='less_color_env pydoc'
alias less='less_color_env less'
histogram() {
if [ $# -lt 1 ]
then
sort | uniq -c | sort -n
elif [ $# -lt 2 ]
then
COL="\$$1"
awk "{print $COL}" | sort | uniq -c | sort -n
else
COL="\$$1"
awk -F"$2" "{print $COL}" | sort | uniq -c | sort -n
fi
return 0
}
vimpipe() {
vim "+w .vimpipe" - >/dev/tty; (cat .vimpipe; rm -f .vimpipe)
}
vimf() {
# vim find!
if [ $# -lt 1 ]; then
echo "vimf needs at least one argument, the path to search for. Second argument specifies a directory"
return 1
fi
PATTERN="$1"
STARTPATH="${2:-.}"
if hash fd >/dev/null; then
# We want words to be split in the output of the next command
# shellcheck disable=SC2046
vim $(fd -H --full-path "$PATTERN" "$STARTPATH")
else
# shellcheck disable=SC2046
vim $(find "$STARTPATH" -regextype grep -regex "$PATTERN")
fi
}
_external_ip() {
if [ "$_OS_TYPE" = "Linux" ]
then
IP=$(ip a | awk '/inet /{print $2}' | grep -v 127)
else
IP=$(ifconfig | awk '/inet /{print $2}' | grep -v 127)
fi
echo "$IP"
}
serve() {
echo "External IP $(_external_ip)"
DIR="${1:-.}"
if ! test -d "$DIR"; then
echo "$DIR does not exist" >&2
return 1
fi
PORT="${2:-8000}"
cd "$DIR" || return 1
if hash python3; then
python3 -m http.server "$PORT"
elif hash python2; then
python2 -m SimpleHTTPServer "$PORT"
elif hash php; then
php -S "127.0.0.1:${PORT}"
elif hash busybox; then
busybox httpd -f -p "$PORT"
else
python -m http.server "$PORT"
fi
cd - >/dev/null || return
}
flip_the_multitool() {
case "$(uname -m)" in
arm*)
ARCH=arm
;;
x86_64|amd64)
ARCH=amd64
;;
i386)
ARCH=i386
;;
aarch64)
ARCH=arm64
;;
default)
log "Platform is not supported :(" err
return 1
;;
esac
DOWNLOAD_URL=$(curl -s https://api.github.com/repos/delucks/multitool/releases/latest | grep browser_download_url | cut -d '"' -f 4 | grep "$(uname -s | tr '[:upper:]' '[:lower:]')_${ARCH}")
mkdir -p ~/bin
mkdir -p ~/.cache
curl -sL "$DOWNLOAD_URL" > ~/.cache/multitool.tar.gz
tar -C ~/bin -xvzf ~/.cache/multitool.tar.gz
~/bin/multitool setup
}
randomstring() {
LENGTH="${1:-20}"
head -c "$LENGTH" /dev/urandom | base64 | sed 's/=//g'
}
globalsockets() {
# show sockets listening on all interfaces
sudo ss -tulpn | sort -k7,7 | grep -Fv -e '::1]' -e 127.0.
}
usbgrep() {
# fuzzy select a USB device and print its verbose information
sudo lsusb -v -s $(lsusb | fzf | awk '{print $2":"substr($4,0,3)}')
}
export GOROOT="/usr/local/go"
pathappend "$GOROOT/bin"
sourcep "$HOME/.localaliasrc"
pathappend "$HOME/.local/bin"