-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash.bashrc
More file actions
1576 lines (1278 loc) · 47.9 KB
/
Copy pathbash.bashrc
File metadata and controls
1576 lines (1278 loc) · 47.9 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
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# =============================================================================
# 🌟 Bash 配置文件
# ✨ 支持 RedHat / Debian / Arch Linux 全系列发行版
# =============================================================================
export TERM=xterm
bind '"\t": menu-complete'
bind '"\e[Z": menu-complete-backward'
[[ $- != *i* ]] && return
# =============================================================================
# 🏠 环境检测与初始化 - 人家会自动识别你的系统哦~
# =============================================================================
if [[ -n "$TERMUX_VERSION" ]]; then
SYSROOT="/data/data/com.termux/files"
elif [[ -d "/data/data/com.termux" ]]; then
SYSROOT="/data/data/com.termux/files"
else
SYSROOT=""
fi
_detect_distro() {
if [[ -f /etc/redhat-release ]]; then
echo "redhat"
elif [[ -f /etc/debian_version ]]; then
echo "debian"
elif [[ -f /etc/arch-release ]]; then
echo "arch"
else
echo "unknown"
fi
}
DISTRO_TYPE=$(_detect_distro)
# =============================================================================
# ⚙️ Shell 选项与历史记录 - 让人家变得更聪明吧~
# =============================================================================
HISTFILE="${HOME}/.bash_history"
HISTSIZE=100000
HISTFILESIZE=200000
HISTCONTROL="ignorespace:ignoredups"
shopt -s histappend
shopt -s autocd
shopt -s cdspell
shopt -s histverify
shopt -s checkwinsize
shopt -s globstar
shopt -s cdable_vars
shopt -s extglob
shopt -s dirspell
shopt -s dotglob
shopt -s no_empty_cmd_completion
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
export LESS="-R"
# =============================================================================
# 🔍 智能文件查找函数 - 帮主人快速找到想要的东西~
# =============================================================================
_parse_find_args() {
local cmd_name="$1"
shift
local user_args=("$@")
local search_path="."
local pattern=""
case ${#user_args[@]} in
1)
pattern="${user_args[0]}"
;;
2)
search_path="${user_args[0]}"
pattern="${user_args[1]}"
;;
*)
echo "😿 哎呀~参数太多啦!用法: $cmd_name [路径] 模式" >&2
return 1
;;
esac
if [[ "$search_path" == "~/"* || "$search_path" == "~" ]]; then
search_path="${search_path/#\~/$HOME}"
fi
if [[ ! -e "$search_path" ]]; then
echo "😢 呜呜~路径不存在呢: $search_path" >&2
return 2
fi
if [[ ! -d "$search_path" ]]; then
echo "🤔 咦?这不是目录哦: $search_path" >&2
return 2
fi
if [[ -z "$pattern" ]]; then
echo "💭 人家需要搜索模式嘛~不能为空哦" >&2
return 3
fi
echo "$search_path"
echo "$pattern"
return 0
}
findfile() {
local parsed
parsed=$(_parse_find_args findfile "$@") || return $?
local search_path
search_path=$(echo "$parsed" | head -1)
local pattern
pattern=$(echo "$parsed" | tail -1)
echo "🔍 正在查找文件: $pattern ..."
find "$search_path" -type f -iname "*$pattern*" 2>/dev/null | head -1000
}
finddir() {
local parsed
parsed=$(_parse_find_args finddir "$@") || return $?
local search_path
search_path=$(echo "$parsed" | head -1)
local pattern
pattern=$(echo "$parsed" | tail -1)
echo "📁 正在查找目录: $pattern ..."
find "$search_path" -type d -iname "*$pattern*" 2>/dev/null | head -500
}
findtext() {
local parsed
parsed=$(_parse_find_args findtext "$@") || return $?
local search_path
search_path=$(echo "$parsed" | head -1)
local pattern
pattern=$(echo "$parsed" | tail -1)
if command -v rg &>/dev/null; then
echo "⚡ 使用 ripgrep 快速搜索: $pattern ..."
rg --smart-case --hidden --no-ignore \
--max-depth 8 --max-filesize 10M \
--max-columns=150 \
--glob='!{.git,.cargo,.lingma,.local,.java,.svn,.hg,node_modules,build,target,dist,.cache,__pycache__,.DS_Store}' \
-- "$pattern" "$search_path" 2>/dev/null | head -500
else
echo "😅 抱歉呢~请先安装 ripgrep (rg) 才能使用这个功能哦" >&2
echo "💡 安装方法:"
echo " sudo pacman -S ripgrep"
return 127
fi
}
# =============================================================================
# 🧹 空目录清理函数 - 帮主人打扫干净的房间~
# =============================================================================
rmemptydir() {
local target="${1:-.}"
if [[ ! -e "$target" ]]; then
echo "😢 路径不存在呢: $target" >&2
return 1
fi
if [[ ! -d "$target" ]]; then
echo "🤔 这不是目录哦: $target" >&2
return 1
fi
if [[ -L "$target" ]]; then
echo "🛡️ 为了安全起见,人家不会操作符号链接的啦~" >&2
return 1
fi
local abs_target
abs_target=$(realpath "$target")
local protected_dirs=("/" "/home" "/root" "/etc" "/var" "/usr" "/bin" "/sbin" "/lib" "/lib64" "/tmp" "/dev" "/proc" "/sys")
for protected in "${protected_dirs[@]}"; do
if [[ "$abs_target" == "$protected" ]]; then
echo "🛡️ 危险!这是系统关键目录,人家不敢删啦~" >&2
return 1
fi
done
echo "🧹 开始清理空目录: $target"
local total_count=0
local iteration=0
local max_iterations=100
while [[ $iteration -lt $max_iterations ]]; do
((iteration++))
local tmpfile
tmpfile=$(mktemp)
local round_count=0
find "$target" -mindepth 1 -depth -type d -empty \
! -type l ! -fstype proc ! -fstype sysfs ! -fstype devfs \
-print0 2>/dev/null > "$tmpfile"
if [[ ! -s "$tmpfile" ]]; then
rm -f "$tmpfile"
break
fi
while IFS= read -r -d '' dir; do
if [[ -n "$dir" && -d "$dir" ]]; then
if [[ -w "$dir" ]]; then
if rmdir "$dir" 2>/dev/null; then
local display="${dir//$'\n'/␊}"
echo "🗑️ 已删除: $display"
((round_count++))
((total_count++))
fi
fi
fi
done < "$tmpfile"
rm -f "$tmpfile"
[[ $round_count -eq 0 ]] && break
done
if [[ $iteration -ge $max_iterations ]]; then
echo "⚠️ 达到最大迭代次数,可能需要手动检查哦~" >&2
fi
if [[ $total_count -eq 0 ]]; then
echo "✨ 太棒啦~没有发现空目录呢!"
else
echo "✅ 完成!共删除 $total_count 个空目录(迭代 $iteration 轮)~人家厉害吧~"
fi
}
# =============================================================================
# 🌐 HTTP 请求封装函数 - 帮主人上网获取信息~
# =============================================================================
get() {
# get 'https://uapis.cn/api/v1/convert/unixtime' 'time=2023-10-27 10:30:00' "
local url="$1"
if [[ ! "$url" =~ ^https?:// ]]; then
echo "😿 URL 必须以 http:// 或 https:// 开头哦~" >&2
return 1
fi
if ! command -v curl &>/dev/null; then
echo "😅 请先安装 curl 才能使用这个功能呢~" >&2
case "$DISTRO_TYPE" in
redhat) echo " sudo dnf install curl" ;;
debian) echo " sudo apt install curl" ;;
arch) echo " sudo pacman -S curl" ;;
esac
return 127
fi
echo "🌐 正在请求: $url ..."
local cmd=(curl -s -f -m 10 -X GET \
-H "User-Agent: http_get/1.0" \
-H "Accept: application/json, */*" \
-H "Accept-Encoding: gzip, deflate" \
-w "\n%{http_code}" \
-G)
shift
local param
for param in "$@"; do
if [[ "$param" =~ ^[^=]+= ]]; then
cmd+=("--data-urlencode" "$param")
else
echo "⚠️ 跳过格式错误的参数: '$param'" >&2
fi
done
cmd+=("$url")
local response
response=$( "${cmd[@]}" 2>&1 ) || {
echo "❌ 请求失败了呢~可能是网络问题或者服务器出错啦" >&2
return 1
}
local lines_array=()
while IFS= read -r line; do
lines_array+=("$line")
done <<< "$response"
local lines_count=${#lines_array[@]}
if [[ $lines_count -lt 2 ]]; then
echo "😵 服务器返回了奇怪的响应呢~" >&2
return 1
fi
local status_code="${lines_array[$((lines_count-1))]}"
local body_lines=("${lines_array[@]:0:$((lines_count-1))}")
if [[ ! "$status_code" =~ ^2[0-9][0-9]$ ]]; then
echo "🔴 HTTP $status_code - 请求出问题啦~" >&2
if [[ ${#body_lines[@]} -gt 0 ]]; then
printf '%s\n' "${body_lines[@]}" >&2
fi
return 1
fi
if [[ ${#body_lines[@]} -eq 0 ]]; then
echo "✨ 请求成功!不过没有返回内容呢~"
return 0
fi
local body_str
body_str=$(printf '%s\n' "${body_lines[@]}")
if command -v jq >/dev/null 2>&1; then
echo "$body_str" | jq -e . 2>/dev/null || echo "$body_str"
else
echo "$body_str"
fi
echo "✅ 请求成功啦~"
}
post() {
# post 'https://uapis.cn/api/v1/search/aggregate' '{"query":"archlinux","fetch_full":false}' "
local url="$1"
local json="$2"
if [[ -z "$url" ]]; then
echo "😿 URL 不能为空哦~" >&2
return 1
fi
if [[ -z "$json" ]]; then
echo "😿 JSON 数据不能为空呢~" >&2
return 1
fi
if ! command -v curl &>/dev/null; then
echo "😅 请先安装 curl 呢~" >&2
return 127
fi
echo "📤 正在发送 POST 请求: $url ..."
local response
response=$(curl -s -X POST \
-H "Content-Type: application/json" \
-d "$json" \
"$url")
if command -v jq &>/dev/null; then
echo "$response" | jq . 2>/dev/null || echo "$response"
else
echo "$response"
fi
echo "✅ POST 请求完成~"
}
# =============================================================================
# 🛠️ 开发工具函数 - 主人编程的好帮手~
# =============================================================================
genkey() {
if ! command -v openssl &>/dev/null; then
echo "😅 请先安装 openssl 呢~" >&2
case "$DISTRO_TYPE" in
redhat) echo " sudo dnf install openssl" ;;
debian) echo " sudo apt install openssl" ;;
arch) echo " sudo pacman -S openssl" ;;
esac
return 127
fi
echo "🔑 正在生成随机密钥..."
openssl rand -base64 32 | tr -d '\n=' | tr '/+' '_-'
echo
echo "✨ 密钥生成完成~记得好好保存哦~"
}
mine() {
if [[ $# -eq 0 ]]; then
echo "😿 请指定要修改所有权的文件或目录呢~" >&2
echo "💡 用法: mine file1 file2 dir1 ..."
return 1
fi
local user
user=$(whoami)
echo "🔧 正在修改所有权为 $user:$user ..."
for item in "$@"; do
if [[ ! -e "$item" ]]; then
echo "⚠️ 跳过不存在的: $item"
continue
fi
if [[ -d "$item" ]]; then
sudo chown -R "$user:$user" "$item" 2>/dev/null && \
echo "✅ $item/* → $user:$user" || \
echo "❌ 修改失败: $item(可能需要权限)"
else
sudo chown "$user:$user" "$item" 2>/dev/null && \
echo "✅ $item → $user:$user" || \
echo "❌ 修改失败: $item(可能需要权限)"
fi
done
echo "✨ 所有权修改完成~"
}
# =============================================================================
# ✏️ 批量重命名函数 - 帮主人整理文件名~
# =============================================================================
batchrename() {
if [[ $# -lt 3 ]]; then
echo "😿 参数不足呢~" >&2
echo "💡 用法: batchrename '正则表达式' '替换文本' 文件1 文件2 ..."
return 1
fi
local regex="$1"
local replacement="$2"
shift 2
local files=("$@")
if [[ -z "$regex" ]]; then
echo "😿 正则表达式不能为空哦~" >&2
return 1
fi
echo "✏️ 开始批量重命名..."
for file in "${files[@]}"; do
if [[ ! -e "$file" ]]; then
echo "⚠️ 跳过不存在的: $file"
continue
fi
local new_name
new_name=$(echo "$file" | sed -E "s|${regex}|${replacement}|g")
if [[ -z "$new_name" || "$file" == "$new_name" ]]; then
continue
fi
if [[ "$new_name" == *..* ]]; then
echo "⚠️ 跳过不安全的重命名: $file -> $new_name" >&2
continue
fi
if [[ "$file" != /* && "$new_name" == /* ]]; then
echo "⚠️ 跳过不安全的路径转换: $file -> $new_name" >&2
continue
fi
mv -i "$file" "$new_name" && \
echo "📝 已重命名: $file -> $new_name" || \
echo "❌ 重命名失败: $file"
done
echo "✨ 批量重命名完成~"
}
multirename() {
if [[ $# -eq 0 ]]; then
echo "😿 请指定要重命名的文件呢~" >&2
echo "💡 用法: multirename file1 file2 ..."
return 1
fi
echo "✏️ 进入交互重命名模式~"
for file in "$@"; do
if [[ ! -e "$file" ]]; then
echo "⚠️ 跳过不存在的: $file"
continue
fi
echo "📄 当前文件: $file"
read -rp "💭 新文件名 (留空跳过,输入 'q' 退出): " new_name
if [[ -z "$new_name" ]]; then
echo "⏭️ 跳过: $file"
elif [[ "$new_name" == "q" ]]; then
echo "🚪 退出重命名~"
return
else
mv "$file" "$new_name" && \
echo "✅ 已重命名: $file -> $new_name" || \
echo "❌ 重命名失败: $file"
fi
echo "---"
done
echo "✨ 交互重命名完成~"
}
# =============================================================================
# 🔄 文本替换函数 - 帮主人快速修改文件内容~
# =============================================================================
replacetext() {
if [[ $# -ne 3 ]]; then
echo "😿 参数不对呢~" >&2
echo "💡 用法: replacetext '文件模式' '旧字符串' '新字符串'"
return 1
fi
local file_pattern="$1"
local old_string="$2"
local new_string="$3"
if [[ -z "$old_string" ]]; then
echo "😿 旧字符串不能为空哦~" >&2
return 1
fi
if ! command -v perl &>/dev/null; then
echo "😅 请先安装 perl 呢~" >&2
case "$DISTRO_TYPE" in
redhat) echo " sudo dnf install perl" ;;
debian) echo " sudo apt install perl" ;;
arch) echo " sudo pacman -S perl" ;;
esac
return 127
fi
echo "🔄 开始替换: '$old_string' -> '$new_string' (匹配: $file_pattern)"
export REPLACE_OLD="$old_string"
export REPLACE_NEW="$new_string"
find . -type f -name "$file_pattern" -exec perl -pi -e '
use strict;
use warnings;
my $old = $ENV{REPLACE_OLD};
my $new = $ENV{REPLACE_NEW};
s/\Q$old\E/$new/g;
' {} +
unset REPLACE_OLD REPLACE_NEW
echo "✅ 替换完成~人家厉不厉害~"
}
# =============================================================================
# 📦 压缩与目录工具 - 帮主人打包和创建目录~
# =============================================================================
mktar() {
if [[ $# -eq 0 ]]; then
echo "😿 请指定要压缩的文件或目录呢~" >&2
echo "💡 用法: mktar file1 dir1 ..."
return 1
fi
for target in "$@"; do
if [[ ! -e "$target" ]]; then
echo "⚠️ 跳过不存在的: '$target'"
continue
fi
local base_name
base_name=$(basename "$target")
local tar_name="$base_name.tar"
if [[ -e "$tar_name" ]]; then
read -rp "⚠️ '$tar_name' 已存在,要覆盖吗?(y/N): " confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
echo "⏭️ 跳过: $target"
continue
fi
fi
echo "📦 正在创建: $tar_name"
if tar -cvf "$tar_name" "$target" 2>/dev/null; then
echo "✅ 创建成功: $tar_name"
else
echo "❌ 创建失败: $tar_name"
fi
done
echo "✨ 压缩任务完成~"
}
mkcd() {
if [[ $# -ne 1 ]]; then
echo "😿 请指定目录名称呢~" >&2
echo "💡 用法: mkcd 'My Directory'"
return 1
fi
echo "📁 正在创建目录: $1"
if mkdir -p "$1" && cd "$1"; then
echo "✅ 目录创建成功并已进入~ ($(pwd))"
else
echo "❌ 创建目录失败了呢: $1" >&2
return 1
fi
}
# =============================================================================
# 🎨 智能路径显示 - 让路径变得漂漂亮亮~
# =============================================================================
__smart_pwd() {
local home_tilde="\033[1;35m~\033[m"
local root_slash="\033[1;34m/\033[m"
local dir_color="\033[0;32m"
local link_color="\033[1;36m"
local error_color="\033[1;31m"
if [[ "$PWD" == "$HOME" ]]; then
echo -e "$home_tilde"
return
elif [[ "$PWD" == "/" ]]; then
echo -e "$root_slash"
return
fi
local display_path="${PWD/#$HOME/\~}"
local result=""
local accumulated_path=""
local -a parts
IFS='/' read -ra parts <<< "$display_path"
for i in "${!parts[@]}"; do
local part="${parts[$i]}"
[[ -z "$part" ]] && continue
if [[ "$i" -eq 0 && "${parts[0]}" == "~" ]]; then
accumulated_path="$HOME"
result+="${home_tilde}/"
continue
else
accumulated_path+="/${part}"
fi
local color="$dir_color"
if [[ -L "$accumulated_path" ]]; then
color="$link_color"
elif [[ ! -e "$accumulated_path" ]]; then
color="$error_color"
fi
result+="${color}${part}\033[m/"
done
echo -e "${result%/}"
}
# =============================================================================
# 🌿 Git 版本控制 - 主人的代码管理小助手~
# =============================================================================
if command -v git &>/dev/null; then
alias g='git'
alias ginit='git init'
alias gclone='git clone'
alias gadd='git add'
alias gcommit='git commit'
alias gamend='git commit --amend -m'
alias gpush='git push'
alias gpull='git pull'
alias gfetch='git fetch'
alias gmerge='git merge'
alias grebase='git rebase'
alias gstatus='git status'
alias gbranch='git branch'
alias gcheckout='git checkout'
alias gswitch='git switch'
alias glog='git log'
alias gloggraph='git log --graph --oneline --all'
alias gdiff='git diff'
alias gdiffstaged='git diff --staged'
alias gupdate='git add . && git commit -m "fix bugs and add new features"'
alias gc1='git clone --recursive --depth=1'
alias greset='git reset'
alias gresethard='git reset --hard HEAD~1'
alias gresetsoft='git reset --soft HEAD~1'
alias grestore='git restore'
alias grestorestaged='git restore --staged'
alias gclean='git clean -fd'
alias gtag='git tag'
alias gstash='git stash'
alias gstashpop='git stash pop'
alias gstashlist='git stash list'
alias gremote='git remote'
alias gremoteadd='git remote add'
alias gremoteseturl='git remote set-url'
gwhatchange() {
echo "📋 最近 20 条提交记录:"
git log --oneline | head -20
read -rp "💭 输入哈希值查看详细变更: " hash
if [[ -n "$hash" ]]; then
echo "🔍 显示提交: $hash"
git show "$hash"
else
echo "⚠️ 未输入哈希值~"
fi
}
pushremote() {
local branch
branch=$(git symbolic-ref --short HEAD 2>/dev/null) || branch="master"
git add . && git commit -m "fix bugs and add new features" && git pull origin "$branch" && git push origin "$branch"
}
fi
# =============================================================================
# 🪟 Tmux 会话管理器 - 帮主人管理多个工作空间~
# =============================================================================
tmuxmgr() {
if ! command -v tmux &>/dev/null; then
echo "😅 请先安装 tmux 呢~" >&2
case "$DISTRO_TYPE" in
redhat) echo " sudo dnf install tmux" ;;
debian) echo " sudo apt install tmux" ;;
arch) echo " sudo pacman -S tmux" ;;
esac
return 1
fi
if ! command -v fzf &>/dev/null; then
echo "😅 请先安装 fzf 呢~" >&2
case "$DISTRO_TYPE" in
redhat) echo " sudo dnf install fzf" ;;
debian) echo " sudo apt install fzf" ;;
arch) echo " sudo pacman -S fzf" ;;
esac
return 1
fi
if [[ -n "$TMUX" ]]; then
echo "⚠️ 人家已经在 tmux 会话中啦~" >&2
return 1
fi
while true; do
local action
action=$(printf "🔗 进入会话\n➕ 创建新会话\n🗑️ 删除会话\n❌ 退出" | fzf --height=40% --border --prompt="Tmux Manager > " --header="按 ESC 取消")
if [[ -z "$action" ]]; then
break
fi
case "$action" in
"🔗 进入会话")
local sessions
sessions=$(tmux ls 2>/dev/null | cut -d: -f1)
if [[ -z "$sessions" ]]; then
echo "📭 没有活跃的 tmux 会话呢~" >&2
read -rp "按任意键继续..." -n 1 -s
continue
fi
local target_session
target_session=$(echo "$sessions" | fzf --prompt="选择要进入的会话 > " --header="按 ESC 返回")
if [[ -n "$target_session" ]]; then
echo "✨ 正在进入会话: $target_session"
tmux attach-session -t "$target_session"
fi
;;
"➕ 创建新会话")
read -rp "💭 输入会话名称: " session_name
if [[ -z "$session_name" ]]; then
echo "😿 会话名称不能为空哦~" >&2
read -rp "按任意键继续..." -n 1 -s
continue
fi
if tmux has-session -t="$session_name" 2>/dev/null; then
echo "⚠️ 会话 '$session_name' 已经存在啦~" >&2
read -rp "按任意键继续..." -n 1 -s
else
tmux new-session -d -s "$session_name"
echo "✅ 已创建并连接到会话 '$session_name' ~" >&2
tmux attach-session -t "$session_name"
fi
;;
"🗑️ 删除会话")
local sessions
sessions=$(tmux ls 2>/dev/null | cut -d: -f1)
if [[ -z "$sessions" ]]; then
echo "📭 没有活跃的 tmux 会话呢~" >&2
read -rp "按任意键继续..." -n 1 -s
continue
fi
local target_session
target_session=$(echo "$sessions" | fzf --prompt="选择要删除的会话 > " --header="按 ESC 返回")
if [[ -n "$target_session" ]]; then
tmux kill-session -t "$target_session"
echo "✅ 已删除会话 '$target_session' ~" >&2
read -rp "按任意键继续..." -n 1 -s
fi
;;
"❌ 退出")
echo "👋 拜拜~"
break
;;
*)
break
;;
esac
done
}
# =============================================================================
# 💻 提示符配置 - 让命令行界面变得漂漂亮亮~
# =============================================================================
__git_branch() {
local branch
branch=$(git rev-parse --short HEAD 2>/dev/null) || return 0
local branch_name
branch_name=$(git symbolic-ref --short HEAD 2>/dev/null) || branch_name="$branch"
if [[ -n "$branch_name" ]]; then
printf " \033[1;31m[\033[m%s\033[1;31m]\033[m" "$branch_name"
fi
}
__set_prompt() {
local exit_code=$?
local full_time
full_time=$(date +%T 2>/dev/null) || full_time="??:??:??"
local time1="${full_time%:*}"
local time2="${full_time##*:}"
local user_color="\[\033[1;31m\]\[\033[4m\]\[\033[5m\]"
local user_host="${user_color}$(whoami)\[\033[m\]"
local smart_path
smart_path="$(__smart_pwd)"
local git_info
git_info="$(__git_branch)"
local status_color="\[\033[1;32m\]"
local status_text=""
if [[ $exit_code -ne 0 ]]; then
status_color="\[\033[1;31m\]"
status_text="${exit_code}"
fi
PS1="\[\033[m\]┌─\[\033[1;31m\][\[\033[m\]$0-$$ ${time1}\[\033[25m\]:\[\033[m\]${time2} ${user_host}\[\033[1;31m\]@\[\033[34m\]\h ${smart_path}\[\033[1;31m\]]\[\033[m\]${git_info}
└─${status_color}${status_text}\$>>_\[\033[m\] "
PS2='\[\033[1;33m\][Line $LINENO]>\[\033[m\]'
PS3='\[\033[1;35m\][[$0]Select > \[\033[m\]'
PS4='\[\033[1;35m\][[$0] Line $LINENO:> \[\033[m\]'
}
PROMPT_COMMAND='__set_prompt'
# =============================================================================
# 📋 文件操作别名 - 让文件列表变得漂漂亮亮~
# =============================================================================
if command -v eza &>/dev/null; then
alias ls='eza --color=auto --icons --group-directories-first'
alias l='eza -lbah --icons'
alias ll='eza -lbg --icons'
alias la='eza -labgh --icons'
alias lsa='eza -lbagR --icons'
alias lst='eza -lTabgh --icons'
alias sl='eza --icons'
elif command -v exa &>/dev/null; then
alias ls='exa --color=auto --icons --group-directories-first'
alias l='exa -lbah --icons'
alias ll='exa -lbg --icons'
alias la='exa -labgh --icons'
alias sl='exa --icons'
else
alias ls='ls --color=auto -v -p --group-directories-first'
alias l='ls -lh'
alias ll='ls -la'
alias la='ls -lah'
alias sl='ls'
fi
alias grep='grep --color=auto'
alias diff='diff --color=auto'
# =============================================================================
# 🧭 快速导航别名 - 让主人快速跳转到想去的地方~
# =============================================================================
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias home='cd $HOME 2>/dev/null || cd ~'
alias cache='cd $HOME/.cache 2>/dev/null || cd ~'
alias config='cd $HOME/.config 2>/dev/null || cd ~'
alias localshare='cd $HOME/.local/share 2>/dev/null || cd ~'
alias localstate='cd $HOME/.local/state 2>/dev/null || cd ~'
alias docs='cd ~/Documents 2>/dev/null || cd ~'
alias downs='cd ~/Downloads 2>/dev/null || cd ~'
# =============================================================================
# ⚙️ 系统管理别名 - 帮主人管理系统服务~
# =============================================================================
if command -v systemctl &>/dev/null; then
if [[ -f /etc/arch-release ]] && command -v mkinitcpio &>/dev/null; then
alias mkinitcpio='sudo mkinitcpio'
fi
alias sc-start='sudo systemctl start'
alias sc-stop='sudo systemctl stop'
alias sc-restart='sudo systemctl restart'
alias sc-reload='sudo systemctl reload'
alias sc-enable='sudo systemctl enable --now'
alias sc-disable='sudo systemctl disable --now'
alias sc-mask='sudo systemctl mask'
alias sc-unmask='sudo systemctl unmask'
alias sc-status='sudo systemctl status'
alias sc-failed='systemctl --failed'
alias sc-cat='systemctl cat'
alias sc-ls='systemctl list-units --type=service'
alias sc-lsfiles='systemctl list-unit-files --type=service'
alias sc-reloadall='sudo systemctl daemon-reload'
alias boottime='sudo systemd-analyze'
alias sshd='sudo systemctl start sshd'
alias dockerd='sudo systemctl start docker'
alias mysqld='sudo systemctl start mariadb'
alias tomcatd='sudo systemctl start tomcat10'
fi
# ==========================================
# 🗄️ MySQL 数据库管理 - 数据库连接和备份命令呀~
# ==========================================
if command -v mariadb >/dev/null 2>&1; then
alias my='mariadb --defaults-extra-file=$HOME/.my.cnf'
alias my-ls='mariadb --defaults-extra-file=$HOME/.my.cnf -e "SHOW DATABASES;"'
alias my-tables='mariadb --defaults-extra-file=$HOME/.my.cnf -e "SHOW TABLES;"'
alias my-conn='mariadb --defaults-extra-file=$HOME/.my.cnf -e "SHOW PROCESSLIST;"'
alias my-vars='mariadb --defaults-extra-file=$HOME/.my.cnf -e "SHOW VARIABLES LIKE \"%max_connections%\";"'
my-backup() {
local db="$1"
if [ -z "$db" ]; then
echo "USAGE: my-backup <database_name>" >&2
return 1
fi
local date=$(date +%F_%H%M%S)
local out="${db}_${date}.sql.gz"
mariadb-dump --defaults-extra-file=$HOME/.my.cnf --single-transaction --routines --triggers --events --quick --lock-tables=false "$db" | gzip > "$out"
echo "$out"
}