在沒有「嵌套」會話的情況下重新啟動 zsh 進程

在沒有「嵌套」會話的情況下重新啟動 zsh 進程

我想重新啟動我的 zsh 會話,因為我保留一個持久的 tmux 會話並~/.zshrc經常更改並執行source ~/.zshrc.然而我意識到,隨著時間的推移,這會變得更慢(例如,for i inseq 50; do source ~/.zshrc; echo "a"; done開始快速列印 'a' 並很快變得更慢)。

我在這裡閱讀了重新啟動 zsh 的建議,建議是簡單地運行zshzsh -l。但是,如果我這樣做,我會建立一個「嵌套」zsh 會話(如果我理解正確的話)。我的意思是:

# Simulate slowed zsh session
for i in `seq 50`; do source ~/.zshrc; echo "a"; done
# use zsh to make it faster "child" zsh
zsh
# confirm fast
source ~/.zshrc; # fast
# revert back to "parent" zsh
exit
# confirm old slow session is still there
source ~/.zshrc; # slow

我有一個包含多個視窗的 tmux 會話和一個我希望保持持久的命令歷史記錄。這就是為什麼我正在尋找可持續的解決方案。

獎金問題:有人知道為什麼source ~/.zshrc會放慢速度?

# Path to your oh-my-zsh installation.
export ZSH="/Users/username/.oh-my-zsh"

ZSH_THEME="themename"

# Which plugins would you like to load?
# Standard plugins can be found in ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
  git
)

source $ZSH/oh-my-zsh.sh


# activate zsh-syntax-highlighting (brew install zsh-syntax-highlighting)
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

function proxyON() {
...redacted
}

function proxyOFF(){
 http_proxy=
 https_proxy=
 HTTP_PROXY=
 HTTPS_PROXY=
 export http_proxy https_proxy HTTP_PROXY HTTPS_PROXY
}

function nukeDS_Store(){
 find ~/Projects/mys/ -name '.DS_Store' -delete
}

function reload-ssh() {
   ssh-add -e /Library/OpenSC/lib/opensc-pkcs11.so >> /dev/null
   if [ $? -gt 0 ]; then
       echo "Failed to remove previous card"
   fi
   ssh-add -s /Library/OpenSC/lib/opensc-pkcs11.so 
}

alias fastBuild='mvn install --offline -DskipTests=true'

## History Settings
# set history size
export HISTSIZE=1000000
#save history after logout
export SAVEHIST=1000000
##history file
export HISTFILE=~/.zhistory
##save only one command if 2 common are same and consistent
setopt HIST_IGNORE_DUPS
##add timestamp for each entry
setopt EXTENDED_HISTORY   
##have seperate history for each
setopt nosharehistory
##dont append into history file
setopt NOINC_APPEND_HISTORY


# Set java version
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_191`


# Maven
export M3_HOME="/Applications/apache-maven-3.6.0" # replace n.n.n with appropriate version
export M3=$M3_HOME/bin
export PATH=$M3:$PATH

## set node version
export PATH="/usr/local/opt/node@8/bin:$PATH"

## pic-tools
source /Projects/pic-tools/scripts/*.env

答案1

只需將您正在運行的 zsh 實例替換為新實例即可:

exec zsh

exec是一個外殼內置命令的目的是(參見zshbuiltins手冊頁):

使用命令而不是 fork 取代目前 shell。

為什麼它變得越來越慢......我的第一個猜測是,你在你的 PATH 中重新定義 PATH zshrc,也許在一個相當慢的驅動器上有一個目錄。因此,每次您獲取 後zshrc,您的搜尋路徑就會變得越來越長。而且每次都zsh必須重新哈希越來越多的目錄...

請閱讀我的回答另一個問題如何改善這種情況。

相關內容