-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.zshrc
619 lines (505 loc) · 14.3 KB
/
.zshrc
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
export ZSH="$HOME/.zshconfig/.oh-my-zsh"
ZSH_THEME="powerlevel10k/powerlevel10k"
plugins=(git)
source $ZSH/oh-my-zsh.sh
alias reload="source ~/.zshrc && echo 'Reloaded Zsh Config'"
alias list="ls -lah"
alias takeown="sudo chown -R $USER:$USER ."
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
export PATH=/opt/cuda/bin:$PATH
touch ~/tmp/_FilesStoredHereAreNotBackedUp
touch ~/Videos/_FilesStoredHereAreNotBackedUp
alias killdesktop="killall plasmashell"
alias refreshdesktop="killall plasmashell; kstart5 plasmashell"
alias lsgpu="nvidia-smi | sed '/Processes:/q' | head -n -3"
alias vizsh="vim ~/.zshconfig/.zshrc && reload"
alias extra
extract() {
# Check if the file is specified
if [[ -z "$1" ]]; then
echo "Usage: extract <file>"
return 1
fi
# Get the file extension
file="$1"
extension="${file##*.}"
# Extract based on file extension
case "$extension" in
tar)
echo "Extracting tar archive..."
tar -xvf "$file"
;;
tar.gz|tgz)
echo "Extracting tar.gz archive..."
tar -xzvf "$file"
;;
tar.bz2|tbz)
echo "Extracting tar.bz2 archive..."
tar -xjvf "$file"
;;
tar.xz|txz)
echo "Extracting tar.xz archive..."
tar -xJvf "$file"
;;
gz)
echo "Extracting gzipped file..."
gunzip "$file"
;;
zip)
echo "Extracting zip archive..."
unzip "$file"
;;
rar)
echo "Extracting rar archive..."
unrar x "$file"
;;
7z)
echo "Extracting 7z archive..."
7z x "$file"
;;
*)
echo "Unsupported file type: $extension"
return 1
;;
esac
}
function compress() {
# Display usage if arguments are missing
if [[ "$#" -lt 2 ]]; then
echo "Error: Missing arguments."
echo "Usage: compress <archive_name> [-n <num_cores>] <source1> [<source2> ...]"
return 1
fi
# Initialize variables
archive_name=""
num_cores=""
sources=()
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case "$1" in
-n)
shift
if [[ "$#" -gt 0 && "$1" =~ ^[0-9]+$ ]]; then
num_cores="$1"
else
echo "Error: Invalid or missing value for -n option."
return 1
fi
;;
-*)
echo "Error: Unknown option $1"
return 1
;;
*)
if [[ -z "$archive_name" ]]; then
archive_name="$1"
else
sources+=("$1")
fi
;;
esac
shift
done
# Ensure archive name and sources are provided
if [[ -z "$archive_name" || "${#sources[@]}" -eq 0 ]]; then
echo "Error: Missing archive name or sources."
echo "Usage: compress <archive_name> [-n <num_cores>] <source1> [<source2> ...]"
return 1
fi
# Determine number of cores to use
total_cores=$(nproc)
if [[ -z "$num_cores" ]]; then
num_cores=$((total_cores / 2)) # Default to half of the cores
fi
# Set the XZ_OPT with the number of threads
export XZ_OPT="-9T$num_cores"
# Create the tarball
tar -cvJf "$archive_name" "${sources[@]}"
}
alias catzsh="cat ~/.zshconfig/.zshrc"
alias copy="rsync -avh --progress"
alias sucopy="sudo rsync -avh --progress"
function clone() {
# Check if two parameters are provided
if [[ $# -ne 2 ]]; then
echo "Usage: clone <source> <target>"
return 1
fi
# Assign parameters to source and target
local source="$1"
local target="$2"
# Run sudo dd with status=progress
sudo dd if="$source" of="$target" bs=4M status=progress
}
alias del="rm -rf"
function updatezsh() {
cd $HOME/.zshconfig
git pull --recurse-submodules
cd -
}
if grep -q '^ID=arch' /etc/os-release; then
function updateyay() {
echo "Updating/Installing yay"
del $HOME/tmp/yay
git clone https://aur.archlinux.org/yay.git $HOME/tmp/yay
cd $HOME/tmp/yat
makepkg -si
cd -
}
fi
function status() {
if [[ -z "$1" ]]; then
echo "Usage: status <service_name>"
return 1
fi
local service="$1"
#
# # Validate service name with systemctl
# if ! systemctl list-units --type=service | grep -q "^$service"; then
# echo "Error: '$service' is not a valid service."
# return 1
# fi
# Display service status and logs
echo "\n--- Last 256 lines of logs for $service ---"
sudo journalctl -u "$service" -n 256 --no-pager
sudo systemctl status "$service" --lines=0
}
function restart() {
if [[ -z "$1" ]]; then
echo "Usage: restart <service_name>"
return 1
fi
local service="$1"
# Validate service name with systemctl
if ! systemctl list-units --type=service | grep -q "^$service"; then
echo "Error: '$service' is not a valid service."
return 1
fi
# Restart the service
echo "Restarting service: $service"
sudo systemctl restart "$service"
# Check the restart status
if [[ $? -eq 0 ]]; then
echo "Service '$service' restarted successfully."
else
echo "Failed to restart service '$service'."
return 1
fi
}
function start() {
if [[ -z "$1" ]]; then
echo "Usage: start <service_name>"
return 1
fi
local service="$1"
# Validate service name with systemctl
if ! systemctl list-units --type=service | grep -q "^$service"; then
echo "Error: '$service' is not a valid service."
return 1
fi
# Start the service
echo "Starting service: $service"
sudo systemctl start "$service"
# Check the start status
if [[ $? -eq 0 ]]; then
echo "Service '$service' started successfully."
else
echo "Failed to start service '$service'."
return 1
fi
}
# Define the stop function
function stop() {
if [[ -z "$1" ]]; then
echo "Usage: stop <service_name>"
return 1
fi
local service="$1"
# Validate service name with systemctl
if ! systemctl list-units --type=service | grep -q "^$service"; then
echo "Error: '$service' is not a valid service."
return 1
fi
# Stop the service
echo "Stopping service: $service"
sudo systemctl stop "$service"
# Check the stop status
if [[ $? -eq 0 ]]; then
echo "Service '$service' stopped successfully."
else
echo "Failed to stop service '$service'."
return 1
fi
}
# Define the enable function
function enable() {
# Check if no arguments are provided
if [[ -z "$1" ]]; then
echo "Usage: enable [-n] <service_name>"
return 1
fi
# Parse arguments
local start_now=false
local service=""
if [[ "$1" == "-n" ]]; then
start_now=true
service="$2"
else
service="$1"
fi
# Validate service name with systemctl
if [[ -z "$service" ]]; then
echo "Error: No service specified."
return 1
fi
if ! systemctl list-units --type=service | grep -q "^$service"; then
echo "Error: '$service' is not a valid service."
return 1
fi
# Enable the service
echo "Enabling service: $service"
sudo systemctl enable "$service"
if [[ $? -eq 0 ]]; then
echo "Service '$service' enabled successfully."
else
echo "Failed to enable service '$service'."
return 1
fi
# Optionally start the service now
if [[ "$start_now" == true ]]; then
echo "Starting service: $service"
sudo systemctl start "$service"
if [[ $? -eq 0 ]]; then
echo "Service '$service' started successfully."
else
echo "Failed to start service '$service'."
return 1
fi
fi
}
# Define the disable function
function disable() {
# Check if no arguments are provided
if [[ -z "$1" ]]; then
echo "Usage: disable [-n] <service_name>"
return 1
fi
# Parse arguments
local stop_now=false
local service=""
if [[ "$1" == "-n" ]]; then
stop_now=true
service="$2"
else
service="$1"
fi
# Validate service name with systemctl
if [[ -z "$service" ]]; then
echo "Error: No service specified."
return 1
fi
if ! systemctl list-units --type=service | grep -q "^$service"; then
echo "Error: '$service' is not a valid service."
return 1
fi
# Disable the service
echo "Disabling service: $service"
sudo systemctl disable "$service"
if [[ $? -eq 0 ]]; then
echo "Service '$service' disabled successfully."
else
echo "Failed to disable service '$service'."
return 1
fi
# Optionally stop the service now
if [[ "$stop_now" == true ]]; then
echo "Stopping service: $service"
sudo systemctl stop "$service"
if [[ $? -eq 0 ]]; then
echo "Service '$service' stopped successfully."
else
echo "Failed to stop service '$service'."
return 1
fi
fi
}
# Define the follow function
function follow() {
# Check if no arguments are provided
if [[ -z "$1" ]]; then
echo "Usage: follow <service_name>"
return 1
fi
local service="$1"
# Validate service name with systemctl
if ! systemctl list-units --type=service | grep -q "^$service"; then
echo "Error: '$service' is not a valid service."
return 1
fi
# Follow the service logs
echo "Following logs for service: $service (Press Ctrl+C to stop)"
sudo journalctl -u "$service" -f --no-pager
}
function is_arch_linux() {
# Check if the system is Arch Linux
if grep -q '^ID=arch' /etc/os-release 2>/dev/null; then
return 1
else
return 0
fi
}
function is_yay_installed() {
if command -v yay &>/dev/null; then
return 0
else
return 1
fi
}
function safeupdate() {
# Function to check if a mount point is mounted
is_mounted() {
mount | grep -q " on $1 "
}
# Check if /boot is mounted
if ! is_mounted "/boot"; then
echo "Error: /boot is not mounted. Please mount /boot and try again."
return 1
fi
# Check if /efi is mounted
if ! is_mounted "/boot/efi"; then
echo "Error: /boot/efi is not mounted. Please mount /boot/efi and try again."
return 1
fi
# Determine package manager and perform update
if command -v pacman &>/dev/null; then
echo "Detected Arch-based system. Performing system update with pacman..."
sudo pacman -Syu
# Run yay if available
if command -v yay &>/dev/null; then
echo "yay detected. Performing AUR updates..."
yay -Syu
else
echo "yay not found. Skipping AUR updates."
fi
elif command -v apt &>/dev/null; then
echo "Detected Debian-based system. Performing full system update..."
sudo apt update && sudo apt full-upgrade -y
else
echo "Error: No supported package manager found. Please install pacman or apt."
return 1
fi
echo "System update completed successfully."
}
function install() {
if [[ $# -eq 0 ]]; then
echo "Usage: install <package1> [package2 ... packageN]"
return 1
fi
if command -v yay &>/dev/null; then
echo "Detected yay. Using yay to install packages."
yay -S --needed "$@"
elif command -v pacman &>/dev/null; then
echo "yay not found. Falling back to pacman."
sudo pacman -S --needed "$@"
elif command -v apt-get &>/dev/null; then
echo "Detected Debian-based system. Using apt-get to install packages."
sudo apt-get install -y "$@"
else
echo "Error: No supported package manager found. Please install yay, pacman, or apt-get."
return 1
fi
}
function hist() {
if [[ $# -eq 0 ]]; then
echo "Usage: hist <search_term>"
return 1
fi
history | grep -i -B 5 -A 5 "$1"
}
function zpool-replace() {
if [[ $# -ne 3 ]]; then
echo "Usage: zpool-replace <pool_name> <guid> <device>"
return 1
fi
local pool_name="$1"
local guid="$2"
local device="$3"
echo "Clearing ZFS label on $device..."
sudo zpool labelclear -f "$device"
echo "Replacing device in ZFS pool $pool_name..."
sudo zpool replace "$pool_name" "$guid" "$device"
if [[ $? -eq 0 ]]; then
echo "Device replacement for pool $pool_name completed successfully."
else
echo "Failed to replace device in pool $pool_name."
return 1
fi
}
function zpool-import() {
if [[ $# -lt 1 ]]; then
echo "Usage: zpool-import-crypt [-R <alt_root>] <pool_name>"
return 1
fi
local alt_root=""
local pool_name=""
# Parse arguments
if [[ "$1" == "-R" ]]; then
if [[ -z "$2" ]]; then
echo "Error: -R requires an argument."
return 1
fi
alt_root="$2"
pool_name="$3"
else
pool_name="$1"
fi
if [[ -z "$pool_name" ]]; then
echo "Error: No pool name specified."
return 1
fi
# Check if the pool is already imported
if zpool list "$pool_name" &>/dev/null; then
echo "Pool '$pool_name' is already imported. Skipping import step."
else
# Import the pool
if [[ -n "$alt_root" ]]; then
echo "Importing pool '$pool_name' with alternate root '$alt_root'..."
sudo zpool import -R "$alt_root" "$pool_name"
else
echo "Importing pool '$pool_name'..."
sudo zpool import "$pool_name"
fi
if [[ $? -ne 0 ]]; then
echo "Error: Failed to import pool '$pool_name'."
return 1
fi
fi
# Load the encryption key
echo "Loading encryption key for pool '$pool_name'..."
sudo zfs load-key "$pool_name"
# Mount the pool recursively
echo "Mounting pool '$pool_name' recursively..."
sudo zfs mount -R "$pool_name"
if [[ $? -eq 0 ]]; then
echo "Pool '$pool_name' imported (if needed), key loaded, and mounted successfully."
else
echo "Error: Failed to mount pool '$pool_name'."
return 1
fi
}
alias cdzsh="cd ~/.zshconfig"
# Add ~/.zshconfig/scripts to PATH if not present
if [[ ":$PATH:" != *":$HOME/.zshconfig/scripts:"* ]]; then
PATH="$HOME/.zshconfig/scripts:$PATH"
fi
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.zshconfig/.p10k.zsh ]] || source ~/.zshconfig/.p10k.zsh