有一些事情,重复干两次以上,就不应该想办法让它自动化,或者被记录。 Chat+Note的方式可能需要继续迭代,先满足自己的需求,后面继续迭代。
Linux的习惯性配置
沿用之前的模式,Ohmyzsh方案:p10k+自动高亮和补充这种。 以下复制使用(网络不好的话,手动下载或者直接局域网梯子🚀)
# Ohmyzsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# p10k
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k"
# 然后主题位置替换
"powerlevel10k/powerlevel10k"
# 高亮和自动补全插件
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# ~/.zshrc配置修改
plugins=(
zsh-syntax-highlighting
zsh-autosuggestions
)
然后也写了一个脚本工具:
#!/usr/bin/env bash
**set** -euo pipefail
**echo** "===> 1) 检查依赖:curl / git"
**command** -v curl >/dev/null 2>&1 || { **echo** "需要 curl"; **exit** 1; }
**command** -v git >/dev/null 2>&1 || { **echo** "需要 git"; **exit** 1; }
# 可重复执行:备份一次 zshrc(若存在)
ZSHRC="$HOME/.zshrc"
TS=$(date +%Y%m%d-%H%M%S)
**if** [ -f "$ZSHRC" ] && ! grep -q "backup-by-setup-ohmyzsh" "$ZSHRC"; **then**
cp "$ZSHRC" "$ZSHRC.bak.$TS.backup-by-setup-ohmyzsh"
**echo** "已备份 $ZSHRC -> $ZSHRC.bak.$TS.backup-by-setup-ohmyzsh"
**fi**
**echo** "===> 2) 安装/更新 Oh My Zsh(非交互,不改默认 shell)"
**export** RUNZSH=no
**export** CHSH=no
**export** KEEP_ZSHRC=yes
# 官方安装脚本是幂等的,多次运行安全
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# 计算 ZSH_CUSTOM 路径
ZSH_CUSTOM="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}"
mkdir -p "$ZSH_CUSTOM/themes" "$ZSH_CUSTOM/plugins"
**echo** "===> 3) 安装/更新 powerlevel10k 主题"
P10K_DIR="$ZSH_CUSTOM/themes/powerlevel10k"
**if** [ -d "$P10K_DIR/.git" ]; **then**
git -C "$P10K_DIR" fetch --depth=1 origin && git -C "$P10K_DIR" reset --hard origin/master
**else**
rm -rf "$P10K_DIR"
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git "$P10K_DIR"
**fi**
**echo** "===> 4) 安装/更新 zsh-syntax-highlighting 插件"
ZSH_SH_DIR="$ZSH_CUSTOM/plugins/zsh-syntax-highlighting"
**if** [ -d "$ZSH_SH_DIR/.git" ]; **then**
git -C "$ZSH_SH_DIR" fetch --depth=1 origin && git -C "$ZSH_SH_DIR" reset --hard origin/master
**else**
rm -rf "$ZSH_SH_DIR"
git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting.git "$ZSH_SH_DIR"
**fi**
**echo** "===> 5) 安装/更新 zsh-autosuggestions 插件"
ZSH_AS_DIR="$ZSH_CUSTOM/plugins/zsh-autosuggestions"
**if** [ -d "$ZSH_AS_DIR/.git" ]; **then**
git -C "$ZSH_AS_DIR" fetch --depth=1 origin && git -C "$ZSH_AS_DIR" reset --hard origin/master
**else**
rm -rf "$ZSH_AS_DIR"
git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions "$ZSH_AS_DIR"
**fi**
**echo** "===> 6) 更新 ~/.zshrc:设置主题为 powerlevel10k"
# 检查 OS 类型,处理 sed 命令的兼容性
**if** [[ "$OSTYPE" == "darwin"* ]]; **then**
# macOS 使用 sed 时需要指定空字符串作为备份后缀
sed -i '' 's|^ZSH_THEME=.*|ZSH_THEME="powerlevel10k/powerlevel10k"|' "$ZSHRC"
**else**
# Linux 系统直接使用 -i 参数
sed -i 's|^ZSH_THEME=.*|ZSH_THEME="powerlevel10k/powerlevel10k"|' "$ZSHRC"
**fi**
**echo** "===> 7) 更新 ~/.zshrc:合并 plugins 列表并确保顺序"
# 读取现有 plugins 块(兼容单/多行)
TMP="$ZSHRC.tmp.$TS"
awk '
BEGIN{inplug=0; buf=""}
function flush(){
if(buf!=""){
print buf;
buf="";
}
}
/^ *plugins *= *\(/{
inplug=1;
buf=$0;
next;
}
inplug{
buf=buf "\n" $0;
if($0 ~ /\)/){
inplug=0;
# 处理 buf 中的插件名
gsub(/\r/,"",buf);
# 抽取括号内容
content=buf;
sub(/^.*plugins[[:space:]]*=\s*\(/,"",content);
sub(/\).*/,"",content);
# 使用空白拆分
n=split(content, arr, /[[:space:]]+/);
delete seen;
outc=0;
for(i=1;i<=n;i++){
if(arr[i]=="" ) continue;
if(arr[i] ~ /^[#)]/ ) continue;
if(!(arr[i] in seen)){
seen[arr[i]]=1; list[++outc]=arr[i];
}
}
# 确保 autosuggestions / highlighting 存在
want1="zsh-autosuggestions";
want2="zsh-syntax-highlighting";
if(!(want1 in seen)){ list[++outc]=want1; }
if(!(want2 in seen)){ list[++outc]=want2; }
# 构造新的单行 plugins
line="plugins=(";
for(i=1;i<=outc;i++){
line=line (i==1? "" : " ") list[i];
}
line=line ")";
print line;
next;
}
next;
}
{ print }
' "$ZSHRC" > "$TMP" && mv "$TMP" "$ZSHRC"
**echo** "===> 8) 确保 p10k 配置自动加载(若存在)"
**if** ! grep -q '\[\[ ! -f ~/.p10k.zsh \]\] \|\| source ~/.p10k.zsh' "$ZSHRC"; **then**
**printf** '\n# powerlevel10k config\n[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh\n' >> "$ZSHRC"
**fi**
**echo**
**echo** "✅ 完成!建议做两件事:"
**echo** " 1) 重新打开终端,或执行: source ~/.zshrc"
**echo** " 2) 若首次使用 powerlevel10k,可运行: p10k configure"
**echo**
**echo** "Tips:"
**echo** "- 若看到乱码/图标异常,安装任意 Nerd Font(如 MesloLGS NF)并在终端里选择它。"
**echo** "- 本脚本可重复执行;如需回退,查看 ~/.zshrc.bak.*"
服务器网络问题
简单粗暴的方式就是将局域网的梯子打开。以前经常干,用少了就忘了。
比如下面这种:
export HTTP_PROXY=http://10.0.1.211:10809
export HTTPS_PROXY=http://10.0.1.211:10809
SSH密钥登陆Git
主要的目的就是为了Git快速访问。
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
然后将公钥放到Git的设置中。
Linux的端口占用情况
一般情况下可以用:
lsof -i :5000
Linux服务器后台静默运行
一般情况下使用这种:
nohup python main.py > output.log 2>&1 &