-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.aliasrc
More file actions
237 lines (211 loc) · 8.76 KB
/
.aliasrc
File metadata and controls
237 lines (211 loc) · 8.76 KB
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
#!/usr/bin/env bash
# shellcheck disable=SC1090,SC2142
# Interactive shell configuration: aliases, functions, bindings, completions
# =============================================================================
# Shell settings
# =============================================================================
PROMPT_COMMAND='history -a; [[ -r venv/bin/activate ]] && . venv/bin/activate'
# =============================================================================
# Short aliases
# =============================================================================
alias \
q="exit" \
c="clear" \
e="\$EDITOR" \
g="git" \
k='kubectl' \
p="paccy" \
se="sudoedit" \
cat="bat"
# =============================================================================
# Navigation
# =============================================================================
alias \
..='cd ..' \
...='cd ../..' \
....='cd ../../..' \
h='cd ~' \
bin='cd ~/dotfiles/.local/bin' \
dl='cd ~/Downloads' \
dots='cd ~/dotfiles' \
config='cd ~/dotfiles/.config'
# =============================================================================
# Better defaults
# =============================================================================
alias \
grep="grep --color=auto" \
diff="diff --color=auto" \
ip="ip -color=auto" \
cp="cp -i" \
mv="mv -i" \
rm="rm -v" \
mkdir="mkdir -p" \
free="free -h" \
df="df --human-readable --output=source,target,size,used,avail,pcent" \
du='du --human-readable' \
dd="dd bs=512k status=progress" \
gcc="gcc -Wall -W -pedantic" \
htop="htop -s PERCENT_CPU" \
mpv='mpv --hwdec=auto' \
upower="upower -i /org/freedesktop/UPower/devices/battery_BAT0"
# =============================================================================
# Kubectl
# =============================================================================
# shellcheck disable=SC2154
alias \
kgc='kubectl config current-context' \
kgn='kubectl config get-contexts --no-headers "$(kgc)" | awk "{print \$5}" | sed "s/^$/default/"' \
klc='yq ".contexts.[].name" ~/.kube/config' \
kln='kubectl get namespaces -o custom-columns=NAME:.metadata.name --no-headers' \
ksn='kubectl config set-context --current --namespace "$(kln | fzf --header "select ns. ctx: ["$(kubectl config current-context)"]")"' \
ksc='_x() { set -euo pipefail; s="$(klc | fzf)"; yq e -i ".current-context = \"$s\"" ~/.kube/config; }; (_x)'
# =============================================================================
# Helm
# =============================================================================
alias \
hls="helm ls" \
hdu="helm dependency update" \
hui="helm upgrade --install" \
hur="helm upgrade --reuse-values" \
hua='helm uninstall $(helm ls --short)'
# =============================================================================
# Docker
# =============================================================================
alias \
dshell='_x() { docker run --entrypoint /bin/bash --rm -it "$1" || docker run --entrypoint /bin/sh --rm -it "$1"; }; _x' \
dprune='docker system prune -a --volumes' \
drr='docker run --rm'
# =============================================================================
# Utilities
# =============================================================================
alias \
sshp='ssh -o "StrictHostKeyChecking=no" -o PreferredAuthentications=password -o PubkeyAuthentication=no' \
task='go-task' \
ch="code . && exit" \
bye='systemctl suspend-then-hibernate' \
utop='htop -t -u $(whoami)' \
perms="stat -c '%a %n' *" \
mimetype="file --dereference --brief --mime-type" \
archive="7z a -m0=LZMA2 -mx=9 -mmt\$(nproc) archive.7z" \
update="paccy -Syu" \
cheat='_x() { curl cht.sh/"$1"; }; _x' \
tgs='tmux-go-session' \
pnav='cd "$(_projselect)"' \
dlmv='mv ~/Downloads/"$(\ls -At ~/Downloads | head -n1)" .' \
urldecode='python3 -c "import sys, urllib.parse as ul; print(ul.unquote_plus(sys.argv[1]))"' \
urlencode='python3 -c "import sys, urllib.parse as ul; print (ul.quote_plus(sys.argv[1]))"' \
vset='_x() { read -r "$1" && export "$1"; }; _x' \
set_aws_profile='export AWS_PROFILE="$(aws configure list-profiles | fzf)"' \
unset_aws_profile='unset AWS_PROFILE' \
ytmp3='_x() { yt-dlp -x --audio-format mp3 --audio-quality 0 -o "$HOME/Downloads/%(title)s.%(ext)s" "$1"; }; _x'
# Fuzzy file picker
alias \
pf='rg --files | fzf --preview "bat --color=always --style=numbers --line-range :200 {}"' \
pf.='rg --files --hidden | fzf --preview "bat --color=always --style=numbers --line-range :200 {}"' \
ef='nvim "$(pf)"' \
ef.='nvim "$(pf.)"'
# =============================================================================
# Platform-specific aliases
# =============================================================================
if [[ $OSTYPE == darwin* ]]; then
alias \
ls="gls -hN --color=auto --group-directories-first" \
ll="gls -hNlA --color=auto" \
find="gfind" \
sed="gsed"
else
alias \
ls="ls -hN --color=auto --group-directories-first" \
ll="ls -hNlA --color=auto"
fi
# =============================================================================
# Functions
# =============================================================================
sd() {
set -e
tmp_file="$(mktemp)"
trap 'rm -rf -- "$tmp_file"' EXIT INT TERM HUP
sail -write-wd "$tmp_file"
pushd "$(\cat "$tmp_file")" >/dev/null
set +e
}
gbrowse() {
git log --graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
fzf --ansi --no-sort --reverse --tiebreak=index +m \
--bind "ctrl-m:execute: git show --color=always \$(echo {} | cut -d' ' -f2)" \
--preview "git show --stat --color=always \$(echo {} | cut -d' ' -f2)" \
--preview-window=right,60
}
launch() {
"$@" >/dev/null 2>&1 &
disown
}
_man_help() {
res="${READLINE_LINE%% *}"
help "$res" 2>/dev/null || man "$res"
}
_fzf_file_insert() {
local preview_cmd picker file
command -v fd >/dev/null 2>&1 || {
printf "fd is required for Alt+f file picker\n" >&2
return
}
preview_cmd="cat {}"
command -v bat >/dev/null 2>&1 && preview_cmd="bat --color=always --style=numbers --line-range :200 {}"
if command -v fzf-tmux >/dev/null 2>&1 && [ -n "$TMUX" ]; then
picker=(fzf-tmux -p "60%,80%")
else
picker=(fzf --height=40% --layout=reverse --border)
fi
file="$(
fd --type f --hidden --follow --exclude .git --color=never --strip-cwd-prefix 2>/dev/null |
"${picker[@]}" --prompt 'files > ' --preview "$preview_cmd" --preview-window=right,60%
)" || return
[[ -z $file ]] && return
READLINE_LINE="${READLINE_LINE:0:READLINE_POINT}${file}${READLINE_LINE:READLINE_POINT}"
READLINE_POINT=$((READLINE_POINT + ${#file}))
}
# =============================================================================
# Key bindings
# =============================================================================
bind -x '"\eF": _fzf_file_insert'
bind -m vi-insert -x '"\eF": _fzf_file_insert'
bind -m vi-insert -x '"\eh": _arg_help'
bind -m vi-insert -x '"\eH": _man_help'
bind -x $'"\C-l":clear;' # Ctrl+l to clear screen
# =============================================================================
# Completions
# =============================================================================
_lazy_completion() {
local cmd=$1 aliases=$2 loader=$3
eval "_lazy_${cmd}() {
unset -f _lazy_${cmd}
command -v $cmd >/dev/null 2>&1 || return
. <($loader)
complete -o default -F __start_${cmd} ${cmd} ${aliases}
}
complete -F _lazy_${cmd} ${cmd} ${aliases}"
}
_lazy_completion kubectl "k" "kubectl completion bash"
_lazy_completion helm "" "helm completion bash"
_lazy_completion k6 "" "k6 completion bash"
_lazy_completion gh "" "gh completion -s bash"
_lazy_completion orb "" "orb completion bash"
command -v direnv >/dev/null 2>&1 && eval "$(direnv hook bash)"
# =============================================================================
# External sources
# =============================================================================
_sh="${SHELL##*/}"
# shellcheck disable=SC1091
[ -r "/opt/homebrew/etc/profile.d/bash_completion.sh" ] && . "/opt/homebrew/etc/profile.d/bash_completion.sh"
[ -f "/usr/share/fzf/key-bindings.$_sh" ] && . "/usr/share/fzf/key-bindings.$_sh"
[ -f "/usr/share/doc/pkgfile/command-not-found.$_sh" ] && . "/usr/share/doc/pkgfile/command-not-found.$_sh"
# shellcheck disable=SC1091
[ -r "/usr/share/bash-completion/bash_completion" ] && . "/usr/share/bash-completion/bash_completion"
# shellcheck disable=SC2206
_fzf_dirs=(/opt/homebrew/Cellar/fzf/*/shell)
[ -f "${_fzf_dirs[-1]}/key-bindings.$_sh" ] && . "${_fzf_dirs[-1]}/key-bindings.$_sh"
# keychain
command -v keychain >/dev/null 2>&1 && eval "$(keychain --eval --quiet --noask)"
[ -r "$HOME/.keychain/$HOSTNAME-sh" ] && . "$HOME/.keychain/$HOSTNAME-sh" 2>/dev/null
unset _sh