From b93d9ab879524e971a27b0765dc9783570a0e669 Mon Sep 17 00:00:00 2001 From: Konano Date: Tue, 8 Sep 2026 12:03:26 +0800 Subject: [PATCH] feat(zsh): load user config after zshrc Load .zsh_user once before the first prompt and remove legacy direct source lines during updates. Disable Clash proxy environment variables in the default user configuration. --- configure_priv.sh | 11 ----------- files/zsh/.zsh_user | 2 ++ files/zsh/00-init.zsh | 12 ++++++++++++ update.sh | 29 +++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/configure_priv.sh b/configure_priv.sh index 1115f0b..f75a299 100755 --- a/configure_priv.sh +++ b/configure_priv.sh @@ -187,17 +187,6 @@ configure_zsh() { fi fi - # 如果 ~/.zshrc 里面没有 source ~/.zsh_user 则添加 - if ! grep -qx "source ~/.zsh_user" ~/.zshrc; then - if [ "$(uname)" = "Darwin" ]; then - # macOS - printf "\nsource ~/.zsh_user\n" >> ~/.zshrc - else - # Linux - sed -i '$a source ~/.zsh_user' ~/.zshrc - fi - fi - # 如果 ~/.zshrc 里面没有 conda initialize 则运行 mamba init zsh if ! grep -q "conda initialize" ~/.zshrc; then if [ -d ~/.miniconda3 ]; then diff --git a/files/zsh/.zsh_user b/files/zsh/.zsh_user index 081d414..8698c62 100644 --- a/files/zsh/.zsh_user +++ b/files/zsh/.zsh_user @@ -1,3 +1,5 @@ +__zsh_clash_env_unset + # HOST="VM-ubuntu" # ZSH_THEME="robbyrussell" diff --git a/files/zsh/00-init.zsh b/files/zsh/00-init.zsh index d4816a5..129c4c7 100644 --- a/files/zsh/00-init.zsh +++ b/files/zsh/00-init.zsh @@ -6,6 +6,18 @@ __ZSH_CUSTOM_INIT_INCLUDED="yes" # ==================================================================================================== +autoload -Uz add-zsh-hook + +__zsh_load_user_config() { + add-zsh-hook -d precmd __zsh_load_user_config + unfunction __zsh_load_user_config + [ -r "$HOME/.zsh_user" ] && source "$HOME/.zsh_user" +} + +add-zsh-hook precmd __zsh_load_user_config + +# ==================================================================================================== + zstyle ':omz:update' mode reminder # just remind me to update when it's time zstyle ':omz:update' frequency 13 diff --git a/update.sh b/update.sh index 80b0c8b..c407782 100755 --- a/update.sh +++ b/update.sh @@ -37,6 +37,34 @@ update_authorized_keys() { done <<< "$public_keys" } +remove_legacy_zsh_user_source() { + local zshrc="$HOME/.zshrc" + local init_file="$HOME/.oh-my-zsh/custom/00-init.zsh" + [ -f "$zshrc" ] || return + grep -q '^add-zsh-hook precmd __zsh_load_user_config$' "$init_file" 2>/dev/null || return + + local temp_file + temp_file=$(mktemp "${zshrc}.XXXXXX") || return 1 + + awk ' + { + line = $0 + sub(/^[[:space:]]+/, "", line) + sub(/[[:space:]]+$/, "", line) + if (line != "source ~/.zsh_user" && line != ". ~/.zsh_user") print + } + ' "$zshrc" > "$temp_file" + + if cmp -s "$zshrc" "$temp_file"; then + rm "$temp_file" + return + fi + + cp "$temp_file" "$zshrc" + rm "$temp_file" + echo "Legacy source ~/.zsh_user line has been removed from $zshrc." +} + # ============================================================ update_file $scriptdir/files/.nanorc ~/.nanorc @@ -51,6 +79,7 @@ update_file $scriptdir/files/zsh/51-alias.zsh ~/.oh-my-zsh/custom/51-a update_file $scriptdir/files/zsh/80-proxy.zsh ~/.oh-my-zsh/custom/80-proxy.zsh update_file $scriptdir/files/zsh/90-other.zsh ~/.oh-my-zsh/custom/90-other.zsh update_file $scriptdir/files/zsh/ys-simple.zsh-theme ~/.oh-my-zsh/custom/themes/ys-simple.zsh-theme +remove_legacy_zsh_user_source update_authorized_keys # sudo_id=$(sudo id -u 2>/dev/null)