-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_shell.aliases
394 lines (323 loc) · 12.8 KB
/
dot_shell.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
# vim: set filetype=sh
#
##########################
# Aliases & Functions
##########################
TMUX_POPUP_NAME="popup-session"
alias vim='nvim'
################################################################################
################################################################################
# A command to fetch all registered subdomains
################################################################################
################################################################################
subdomains() {
name="${1:=saasak.studio}"
url="https://subdomains.whoisxmlapi.com/api/v1?apiKey=at_5YimX3xqySyqdZc63cuTEnevrAFaR&domainName=${name}"
curl "${url}" | jq '.result.records[].domain'
}
################################################################################
################################################################################
# Wrapper around the tree command
################################################################################
################################################################################
show_tree() {
depth="${1:=1}"
shift;
tree -L $depth -I "node_modules|.git" --dirsfirst "$@"
}
################################################################################
################################################################################
# On linux, open nautilus with fzf
################################################################################
################################################################################
finder() {
# Determine the system's file manager command
if [ "$(uname)" = "Darwin" ]; then
open_cmd="open"
elif [ -n "$WAYLAND_DISPLAY" ] || [ -n "$DISPLAY" ]; then
open_cmd="xdg-open"
else
echo "No suitable file manager found"
return 1
fi
if [ -f "$1" ] || [ -d "$1" ]; then
("$open_cmd" "$1" &>/dev/null &)
else
dir=$(find . -type d | fzf-tmux -q "${1:-}")
if [ -n "$dir" ] && [ -d "$dir" ]; then
("$open_cmd" "$dir" &>/dev/null &)
else
("$open_cmd" . &>/dev/null &)
fi
fi
}
################################################################################
################################################################################
# Open an editor quickly
################################################################################
################################################################################
open_editor() {
if [ -z "$1" ]; then
file=$(ag --nofollow -l | fzf-tmux)
[ -z "$file" ] \
&& echo "No file selected" \
|| $EDITOR "$file"
else
$EDITOR "$1"
fi
}
################################################################################
################################################################################
# Failed attempt at a lighter zoxide
################################################################################
################################################################################
move_to_dir() {
if [ -z "$1" ] || [ ! -d "$1" ]; then
cd $(find . -type d | fzf-tmux -q "$1")
else
cd "$1"
fi
}
################################################################################
################################################################################
# Utility to print tmux colors
################################################################################
################################################################################
print_tmux_colors() {
for i in {0..255} ; do
printf "\x1b[38;5;${i}mcolour${i}\n"
done
}
################################################################################
################################################################################
# Floating tmux session. Does not work...
################################################################################
################################################################################
tmux_popup() {
width=${1:-"80%"}
height=${2:-"80%"}
name="${TMUX_POPUP_NAME}"
if [ ! -z "$TMUX" ]; then
sess=$(tmux display-message -p -F "#{session_name}")
# if we are in popup session
if [ "${sess}" = "${name}" ]; then
tmux detach-client
else
# check if popup session exists
tmux has-session -t "${name}" &>/dev/null
# if yes, attach to it in a popup
if [ $? -eq 0 ]; then
tmux popup -d '#{pane_current_path}' -xC -yC -w"$width" -h"$height" -E "tmux attach-session -t ${name}"
# otherwise create a session in a popup
else
tmux popup -d '#{pane_current_path}' -xC -yC -w"$width" -h"$height" -E "tmux new-session -s ${name}"
fi
fi
fi
}
################################################################################
################################################################################
# Quickly open a tmux session
################################################################################
################################################################################
tmux_open() {
sess=${1}
directory=${2}
[ -z "${directory}" ] && directory="${pwd}"
if [ -z "${sess}" ]; then
nbSession=$(tmux list-sessions | grep -v "$TMUX_POPUP_NAME" | wc -l)
if [ $nbSession -eq 0 ]; then
# if no session, create a default one named default
sess="default"
else
# if only one session, do not ask to select
[ $nbSession -eq 1 ] \
&& sess=$(tmux list-sessions | grep -v "$TMUX_POPUP_NAME" | head -n 1 | cut -d ":" -f 1) \
|| sess=$(tmux list-sessions | grep -v "$TMUX_POPUP_NAME" | fzf-tmux | cut -d ":" -f 1)
fi
fi
# check if we have a session with this name
tmux has-session -t "${sess}" &>/dev/null
# if not create a detached session with this name
[ $? -eq 0 ] || (TMUX= tmux -u new-session -d -s "${sess}" -c "${directory}")
# if inside tmux switch o/w attach to newly created detached session
[ -z "$TMUX" ] && tmux -u attach-session -t "${sess}" || tmux -u switch-client -t "$sess"
}
################################################################################
################################################################################
# Quickly kill a tmux session
################################################################################
################################################################################
tmux_kill() {
sess="${1}"
if [ -z "${sess}" ]; then
if [ -z "$TMUX" ]; then
nbSession=$(tmux list-sessions | grep -v "$TMUX_POPUP_NAME" | wc -l)
if [ $nbSession -eq 0 ]; then
sess=""
else
# if only one session, do not ask to select
[ $nbSession -eq 1 ] \
&& sess=$(tmux list-sessions | grep -v "$TMUX_POPUP_NAME" | head -n 1 | cut -d ":" -f 1) \
|| sess=$(tmux list-sessions | grep -v "$TMUX_POPUP_NAME" | fzf-tmux | cut -d ":" -f 1)
fi
# if inside tmux
else
seed=$(tmux display-message -p '#S') # kill current session
sess=$(TMUX= tmux list-sessions | grep -v "$TMUX_POPUP_NAME" | fzf-tmux -q "${seed:-}" | cut -d ":" -f 1)
fi
fi
tmux has-session -t "${sess}" &>/dev/null
[ $? -eq 0 ] && (TMUX= tmux kill-session -t "${sess}")
}
tmux_kill_all() {
# Get all sessions except popup
sessions=$(tmux list-sessions | cut -d ":" -f 1)
if [ ! -z "$sessions" ]; then
echo "$sessions" | while read sess; do
TMUX= tmux kill-session -t "$sess"
done
fi
}
################################################################################
################################################################################
# Quickly open a workspace
################################################################################
################################################################################
work_on() {
workspace=$(find ~/Workspace/Projects -mindepth 1 -maxdepth 3 -type d -name ".git" -exec dirname {} \; | fzf-tmux -q $1)
if [ ! -z "$workspace" ]; then
lastname=$(basename "$workspace")
firstname=$(basename $(dirname "$workspace"))
tmux_open "${firstname}/${lastname}" "$workspace"
else
echo "No workspace selected"
fi
}
################################################################################
################################################################################
# Quickly create a workspace
################################################################################
################################################################################
mkpro() {
local projectDir="${HOME}/Workspace/Projects"
local name="${1}"
local scope=${2:-"perso"}
if [ -z "${name}" ]; then
echo "Usage: mkpro <name> [scope]"
return 1
fi
if [ -d "${projectDir}/${scope}/${name}" ]; then
echo "Project already exists"
return 1
fi
mkdir -p "${projectDir}/${scope}/${name}"
(cd "${projectDir}/${scope}/${name}" && git init --quiet)
work_on "${scope}/${name}"
}
################################################################################
################################################################################
# Quickly kill a process on a port
################################################################################
################################################################################
__kill_ports() {
if [ $# -eq 0 ]; then
echo "Usage: __kill_ports port1 [port2 port3 ...]"
return 1
fi
for port in "$@"; do
echo "Killing processes on port $port..."
lsof -ti:$port | xargs -I {} kill -9 {} 2>/dev/null
if [ $? -eq 0 ]; then
echo "✓ Killed process(es) on port $port"
else
echo "✗ No process found on port $port"
fi
done
}
# Keep the old function for backward compatibility
__kill_port() {
__kill_ports "$1"
}
################################################################################
################################################################################
# Change theme for the whole system
################################################################################
################################################################################
chth() {
theme="${1:=eighties}"
sway="/home/georgio/.local/share/base16-manager/rkubosz/base16-sway/themes/base16-${theme}.config"
base16-manager set "$theme" 2>/dev/null
[ -f "${sway}" ] && cp "${sway}" "/home/georgio/.config/sway/colorscheme"
}
################################################################################
################################################################################
# Aliases
################################################################################
################################################################################
alias c='clear'
hash jc 2>/dev/null && hash mlr 2>/dev/null && alias li='\ls -la | jc --ls | mlr --ijson --opprint cut -f link_to -x' || alias li='\ls -la'
alias lila='\ls -la'
alias lisa='\ls -lsah'
hash jc 2>/dev/null && alias what='__g() { \file "$1" | jc --file; }; __g' || what='file'
hash jc 2>/dev/null && alias lidi='\df -h | jc --df' || alias lidi='df'
alias mvd='move_to_dir'
alias edit='open_editor'
alias op='finder'
alias treee='show_tree'
alias home='cd $HOME'
alias work='work_on'
#################
# Tmux
#################
alias tm='tmux_open'
alias tmk='tmux_kill'
alias tmka='tmux_kill_all'
alias tmls='tmux list-sessions'
alias tmpop='tmux_popup'
alias tmcolors='print_tmux_colors'
#################
# Custom bins
#################
[ -e "$HOME/.local/bin/dot.sh" ] && alias dot='$HOME/.local/bin/dot.sh'
#################
# Remaps
#################
# hash podman 2>/dev/null && alias docker='podman'
# hash podman-compose 2>/dev/null && alias docker-compose='podman-compose'
hash bat 2>/dev/null && alias cat='bat'
hash diff-so-fancy 2>/dev/null && alias diff='diff-so-fancy'
hash hub 2>/dev/null && alias git='hub'
hash code-git 2>/dev/null && alias code='code-git'
#################
# Nix
#################
alias nix-ls-installed='nix-env -q | fzf-tmux'
alias nix-ls-available='nix-env -qaP | fzf-tmux'
alias nix-ls-generations='nix-env --list-generations'
alias nix-update='nix-channel --update'
alias nix-rebuild='nix-env -ir -f ~/.env.nix'
alias nix-upgrade='nix-update && nix-rebuild'
alias nix-purge="echo \"nix-env -e '.*' to purge env\""
alias nix-clean='nix-collect-garbage'
alias nix-cleanup='nix-env --delete-generations +3 && nix-collect-garbage'
alias nix-rollback='nix-env --rollback'
alias nix-uninstall='/nix/nix-installer uninstall'
alias devga='devbox global add'
alias devgc='nix-collect-garbage'
alias deva='devbox add'
alias devrm='devbox rm'
alias devse='devbox search'
#################
# TUI
#################
alias ldok="DOCKER_HOST=$(docker context ls --format json | jq '. | select(.Current == true) | .DockerEndpoint') lazydocker"
alias lgit='lazygit'
#################
# Network
#################
alias kport='__kill_port'
alias kports='__kill_ports'
alias myip='dig +short txt ch whoami.cloudflare @1.0.0.1 | tr -d "\""'
alias find-rpi='arp -na | grep -i "dc:a6:32"'
alias find-old-rpi='arp -na | grep -i "b8:27:eb"'