-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_aliases
453 lines (364 loc) · 12.8 KB
/
dot_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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# -*-mode:sh-*- vim:ft=sh
# shellcheck shell=bash
# ~/.aliases
# =============================================================================
# Shell aliases sourced by `~/.bashrc` and `~/.zshrc`.
#
# OSTYPE values used:
# - darwin: macOS
# - cygwin: Cygwin POSIX-compatible environment for Windows
# - mingw: MinGW Minimalist GNU for Windows
# - msys: MinGW Lightweight shell and GNU utilities for Windows
# ==================================
# === Redefine standard commands ===
# ==================================
# Colour directory and file listings.
if ls --color &> /dev/null; then # GNU `ls`
colorflag='--color=auto'
else # macOS `ls`
colorflag='-G'
fi
# Always use color output for `ls`.
alias ls='command ls ${colorflag}'
# Make file commands verbose.
alias cp='cp -v';
alias mv='mv -v';
# Create parent directories on demand.
alias mkdir='mkdir -pv'
# Display drives and space in human readable format.
alias df='df -h'
# Print disk usage per directory non-recursively, in human readable format.
alias du='du -h -d1'
# Because less is more and more is less
alias more='less'
# Disable wrapping of long lines in Nano.
alias nano='nano -w';
# Enable aliases to be sudo’ed.
# See https://www.gnu.org/software/bash/manual/bash.html#Aliases
alias sudo='sudo '
# Stop ping after sending 4 ECHO_REQUEST packets.
alias ping='ping -c 4'
# Always enable colored `grep` output.
# Note: `GREP_OPTIONS="--color=auto"` is deprecated, hence the alias usage.
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
# Colour diff output, if possible.
if type 'colordiff' &> /dev/null; then
alias diff='colordiff'
fi
if type 'dir' &> /dev/null; then
alias dir='dir --color=auto'
fi
if type 'vdir' &> /dev/null; then
alias vdir='vdir --color=auto'
fi
# Improve top with modern alternatives.
if type 'atop' &> /dev/null; then
alias top='atop'
elif type 'htop' &> /dev/null; then
alias top='htop'
elif type 'ntop' &> /dev/null; then
if [[ "${OSTYPE}" =~ ^(cygwin|mingw|msys) ]]; then
alias top='ntop'
fi
fi
# Use neovim instead of vim if it is available.
if type 'nvim' &> /dev/null; then
alias vim='nvim'
alias vi='nvim'
alias oldvim='\vim'
fi
# Avoid issues with environment variables mangling in tmux's subshells when
# using direnv.
if type 'direnv' &> /dev/null; then
alias tmux='direnv exec / tmux'
fi
# =======================
# === General aliases ===
# =======================
# Recursively remove files and directories.
alias rmrf='rm -rf'
# Print each PATH entry on a separate line.
alias path='echo -e ${PATH//:/\\n}'
# Become root user preserving environment variables.
alias esudo='sudo -Ei'
# Clear both the screen and the buffer history.
alias cls="clear && printf '\e[3J'"
# Print the current working directory. Essentialy this is the last component in
# path.
alias cwd='printf "%q\n" "${PWD##*/}"'
# Escape whistspacs in strings. This is useful when working with applications
# that cannot deal with spaces in paths.
alias escape='sed '"'"'s/ /\\ /g'"'"''
# http://xkcd.com/1168/
alias archive='tar -cvzf'
alias extract='tar -xvzf'
# Intuitive map function
# For example, to list all directories that contain a certain file:
# find . -name .gitattributes | map dirname
alias map='xargs -n1'
# Reload the shell.
alias reload='exec ${SHELL} -l'
# Canonical hex dump; some systems have this symlinked.
if ! command -v hd > /dev/null; then
if command -v hexdump > /dev/null; then
alias hd='hexdump'
fi
fi
if command -v jq > /dev/null; then
alias jwtheader="jq -R 'split(\".\") | .[0] | @base64d | fromjson'"
alias jwtpayload="jq -R 'split(\".\") | .[1] | @base64d | fromjson'"
fi
if command -v gpg > /dev/null; then
alias gpg-create-socketdir="gpgconf --create-socketdir"
fi
# ==================
# === Navigation ===
# ==================
# Navigate up a single directory.
alias cd..='cd ..'
alias ..='cd ..'
# Navigate up two directories.
alias ...='cd ../../'
# Navigate up three directories.
alias ....='cd ../../../'
# Navigate up four directories.
alias .....='cd ../../../../'
# Navigate up five directories.
alias ......='cs ../../../../../'
# Navigate to the last used directory.
alias -- -='cd -'
# =========================
# === Directory listing ===
# =========================
# List all files colorized in long format.
alias ll='ls -lF ${colorflag}'
# List all files colorized in long format, excluding . and ..
alias la='ls -lAF ${colorflag}'
# Larger directory lists are paged using `more`.
alias lm='ls -lAF ${colorflag} | more'
# List only directories.
alias lsd='ls -lF ${colorflag} | grep --color=never "^d"'
# =======================
# === File management ===
# =======================
# Copy a file securely.
alias cpv='rsync -ah --info=progress2'
# Find directories.
alias fd='find . -type d -name'
# Find files.
alias ff='find . -type f -name'
# ============
# === Time ===
# ============
# Print local/UTC date and time in ISO-8601 format `YYYY-MM-DDThh:mm:ss`.
alias now='date +"%Y-%m-%dT%H:%M:%S"'
alias unow='date -u +"%Y-%m-%dT%H:%M:%S"'
# Print date in `YYYY-MM-DD` format`
alias nowdate='date +"%Y-%m-%d"'
alias unowdate='date -u +"%Y-%m-%d"'
# Print time in `hh:mm:ss` format`
alias nowtime='date +"%T"'
alias unowtime='date -u +"%T"'
# Print local/UTC date and time in RFC3339 format 'YYYY-MM-DDThh:mm:ssZ
alias rfc3339='date +"%FT%TZ"'
alias urfc3339='date -u +"%FT%TZ"'
# Print Unix timestamp`
alias timestamp='date -u +%s'
# Print week number in ISO-8601 format `YYYY-Www`.
alias week='date +"%Y-W%V"'
# Print weekday number.
alias weekday='date +"%u"'
# ===============
# === Hashing ===
# ===============
# Calculate MD5 hashes.
if ! command -v md5sum > /dev/null; then
if command -v md5 > /dev/null; then
alias md5sum='md5 -r'
else
alias md5sum='openssl md5 -r'
fi
fi
# Calculate SHA1 hashes.
if ! command -v sha1sum > /dev/null; then
if command -v shasum > /dev/null; then
alias sha1sum='shasum -a 1'
else
alias sha1sum='openssl sha1 -r'
fi
fi
# Calculate SHA256 hashes.
if ! command -v sha256sum > /dev/null; then
if command -v shasum > /dev/null; then
alias sha256sum='shasum -a 256'
else
alias sha256sum='openssl sha256 -r'
fi
fi
# ==================
# === Networking ===
# ==================
# Ping hostname(s) 30 times in quick succession.
if [[ "${OSTYPE}" =~ ^(cygwin|mingw|msys) ]]; then
alias fastping='ping -n 30'
else
alias fastping='ping -c 30 -i.2'
fi
# Flush the DNS cache.
if [[ "${OSTYPE}" =~ ^darwin ]]; then
alias flushdns='dscacheutil -flushcache; sudo killall -HUP mDNSResponder;echo DNS cache flushed'
elif [[ "${OSTYPE}" =~ ^(cygwin|mingw|msys) ]]; then
alias flushdns='ipconfig //flushdns'
else
alias flushdns='sudo /etc/init.d/dns-clean restart && echo DNS cache flushed'
fi
# Get all IP addresses.
alias ips="ifconfig -a | grep -o 'inet6\? \(addr:\)\?\s\?\(\(\([0-9]\+\.\)\{3\}[0-9]\+\)\|[a-fA-F0-9:]\+\)' | awk '{ sub(/inet6? (addr:)? ?/, \"\"); print }'"
# Get local IP address.
if [[ "${OSTYPE}" =~ ^darwin ]]; then
alias localip='ipconfig getifaddr en0'
elif [[ "${OSTYPE}" =~ ^(cygwin|mingw|msys) ]]; then
# shellcheck disable=SC2142
alias localip="netstat -rn | grep -w '0.0.0.0' | awk '{print \$4}'"
else
alias localip="ip route get 1 | awk '{print \$NF;exit}'"
fi
# Get external IP address.
if command -v dig &> /dev/null; then
alias publicip='dig +short myip.opendns.com @resolver1.opendns.com'
elif command -v curl &> /dev/null; then
alias publicip='curl --silent --compressed --max-time 5 --url "https://ipinfo.io/ip"'
else
alias publicip='wget -qO- --compression=auto --timeout=5 "https://ipinfo.io/ip"'
fi
# ========================
# === Power management ===
# ========================
# Lock the session.
if [[ "${OSTYPE}" =~ ^darwin ]]; then
alias lock='pmset displaysleepnow'
elif [[ "${OSTYPE}" =~ ^(cygwin|mingw|msys) ]]; then
alias lock='rundll32.exe user32.dll,LockWorkStation'
elif command -v vlock &> /dev/null; then
alias lock='vlock --all'
elif command -v gnome-screensaver-command &> /dev/null; then
alias lock='gnome-screensaver-command --lock'
fi
# Hibernate the system.
if [[ "${OSTYPE}" =~ ^darwin ]]; then
alias hibernate='pmset sleep now'
elif [[ "${OSTYPE}" =~ ^(cygwin|mingw|msys) ]]; then
alias hibernate='shutdown.exe /h'
else
alias hibernate='systemctl suspend'
fi
# Restart the system.
if [[ "${OSTYPE}" =~ ^darwin ]]; then
alias reboot='osascript -e "tell application \"System Events\" to restart"'
elif [[ "${OSTYPE}" =~ ^(cygwin|mingw|msys) ]]; then
alias reboot='shutdown.exe /r'
else
alias reboot='sudo /sbin/reboot'
fi
# Shutdown the system.
if [[ "${OSTYPE}" =~ ^darwin ]]; then
alias poweroff='osascript -e "tell application \"System Events\" to shut down"'
elif [[ "${OSTYPE}" =~ ^(cygwin|mingw|msys) ]]; then
alias poweroff='shutdown.exe /s'
else
alias poweroff='sudo /sbin/poweroff'
fi
# ================
# === Sysadmin ===
# ================
# List drive mounts.
if [[ "${OSTYPE}" =~ ^darwin ]]; then
alias mnt='mount | grep -E ^/dev | column -t'
elif [[ "${OSTYPE}" =~ ^(cygwin|mingw|msys) ]]; then
alias mnt='mount | sed "s/Program Files/Program-Files/i" | column -t'
else
# shellcheck disable=2142
alias mnt="mount | awk -F' ' '{ printf \"%s\t%s\n\",\$1,\$3; }' | column -t | egrep ^/dev/ | sort"
fi
# Print the last 100 entires of the syslog and message.
# The command will continue to follow the output.
if [[ ! "${OSTYPE}" =~ ^(cygwin|mingw|msys) ]]; then
alias syslog='sudo tail -100f /var/log/syslog'
alias messages='sudo tail -100f /var/log/messages'
fi
# Display information about the system.
for command in winfetch neofetch screenfetch; do
if type "${command}" &> /dev/null; then
alias sysinfo='${command}'
break
fi
done
# =============
# === macOS ===
# =============
if [[ "${OSTYPE}" =~ ^darwin ]]; then
# Toggle display of desktop icons (useful when presenting).
alias hidedesktop='defaults write com.apple.finder CreateDesktop -bool false && killall Finder'
alias showdesktop='defaults write com.apple.finder CreateDesktop -bool true && killall Finder'
# Toggle hidden files in Finder.
alias hidefiles='defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder'
alias showfiles='defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder'
# Toggle Spotlight.
alias spotoff='sudo mdutil -a -i off'
alias spoton='sudo mdutil -a -i on'
# PlistBuddy alias, because sometimes `defaults` just doesn’t cut it
alias plistbuddy='/usr/libexec/PlistBuddy'
# Airport CLI alias
alias airport='/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport'
# Empty the Trash on all mounted volumes and the main HDD.
# Also, clear Apple’s System Logs to improve shell startup speed.
# Finally, clear download history from quarantine. https://mths.be/bum
alias emptytrash='sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl; sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV* "delete from LSQuarantineEvent"'
# Clear Google Chrome autofill data ONLY. The Chrome process must not be
# running.
alias clearautofill='sqlite3 ~/Library/Application\ Support/Google/Chrome/Default/Web\ Data "DELETE FROM autofill;"'
# Recursively delete `.DS_Store` files
alias cleanup="find . -type f -name '*.DS_Store' -ls -delete"
fi
# ===========================
# === Configuration paths ===
# ===========================
# Navigate to Chezmoi's local repo.
if command -v chezmoi > /dev/null; then
if [[ "${OSTYPE}" =~ ^(cygwin|mingw|msys) ]]; then
# TODO verify
# shellcheck disable=2142
alias chezmoiconf="cd \$(chezmoi source-path | tr '\\/' '/' | tr -d ':' | awk '{print \$1}')"
else
alias chezmoiconf='cd $(chezmoi source-path)'
alias chezmoidiff='chezmoi diff --color true | less -R'
fi
else
alias chezmoiconf='cd ${HOME}/.local/share/chezmoi'
fi
if command -v nvim > /dev/null; then
alias vimconf='cd ${HOME}/.config/nvim'
fi
# Navigate to Powershell's profile location.
if [[ "${OSTYPE}" =~ ^(cygwin|mingw|msys) ]]; then
alias powershellconf='cd ${HOME}/Documents/PowerShell'
else
alias powershellconf='cd ${HOME}/.config/powershell'
fi
# ===============
# === Weather ===
# ===============
# Display detailed weather and forecast.
if command -v curl > /dev/null; then
alias forecast='curl --silent --compressed --max-time 10 --url "https://wttr.in/Greenwich,%20London?mAFI"'
else
alias forecast='wget -qO- --compression=auto --timeout=10 "https://wttr.in/Greenwich,%20London?mAFI"'
fi
# Display current weather.
if command -v curl > /dev/null; then
alias weather='curl --silent --compressed --max-time 10 --url "https://wttr.in/Greenwich,%20London?m&format=%l:+(%C)+%c++%t+\[%h,+%w\]\n"'
else
alias weather='wget -qO- --compression=auto --timeout=10 "https://wttr.in/Greenwich,%20London?m&format=%l:+(%C)+%c++%t+\[%h,+%w\]\n"'
fi