結果

結果

我在用著奧梅茲什。我正在嘗試加載差異主題基於目前目錄路徑 ( pwd)

邏輯學

如果密碼在或包含 sustring /Sites/work/load af-magic,否則 load robbyrussell

.zshrc

我試過了

STR=$(pwd)
SUB='/Users/john/Sites/work'

if [[ "$STR" =~ .*"$SUB".* ]]; then
  echo "It's there."
  ZSH_THEME="af-magic"
else
    ZSH_THEME="robbyrussell"
fi

結果

它一直在加載robbyrussell

前任。

在此輸入影像描述

答案1

如果你看oh-my-zsh 代碼ZSH_THEME, oh-my-zsh 初始化程式碼使用該變數來取得每個主題的檔案。

因此,如果您希望每當當前工作目錄進入某個目錄時主題就會更改,您需要:

  1. 每當當前目錄更改時更改該變數
  2. 當變數發生變化時,重現主題文件的相同來源。

所以像這樣:

load-omz-theme() {
  # copied and improved from oh-my-zsh
  if (( $# > 0 )) ZSH_THEME=$1
  if [[ -n $ZSH_THEME ]]; then
    if [[ -f $ZSH_CUSTOM/$ZSH_THEME.zsh-theme ]]; then
      source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme"
    elif [[ -f $ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme ]]; then
      source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme"
    else
      source "$ZSH/themes/$ZSH_THEME.zsh-theme"
    fi
  fi
}
adapt-theme() {
  local previous_theme=$ZSH_THEME
  case $PWD in
    ($SUB*) ZSH_THEME=af-magic;;
    (*)     ZSH_THEME=robbyrussell;;
  esac
  [[ $ZSH_THEME = $previous_theme ]] || load-omz-theme
}

chpwd_functions+=(adapt-theme)

順便說一句,[[ "$STR" =~ .*"$SUB".* ]]是 bash 語法,而不是 zsh 語法。在 中zsh,在正規表示式中引用變數不會停用其中的正規表示式運算子。

相關內容