我的 ZSH 完成在开始时不起作用,但当我获取 .zshrc (Mac) 时它们会起作用

My ZSH completions won't work on start but they do when I source .zshrc (Mac)

标题中有一个简单的总结,但要进一步说明:

每当我打开我的终端 (iterm2) 时,我都会加载到 zsh 但完成似乎不起作用,然后当我手动 运行 source .zshrc 它会完全加载。我试过移动我的 .zshrc 文件中的内容以查看加载顺序是否不正确,但没有解决任何问题。

我的 .zshrc 文件:

# ZSH customization
export ZSH="/Users/user/.oh-my-zsh"
source $ZSH/oh-my-zsh.sh
plugins=(git docker asdf zsh-autosuggestions zsh-completions zsh-history-substring-search zsh-syntax-highlighting)
autoload -U compinit && compinit
# color
. "/Users/user/.bin/lscolors.sh" #big color file
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
# alias
alias ip="curl ifconfig.me"
alias ls="gls --color --group-directories-first -hp"
alias ydl="youtube-dl"
alias py="python"
alias code="codium"
# env
export PATH="/usr/local/opt/ncurses/bin:/usr/local/sbin:/usr/local/opt/openssl@1.1/bin:$PATH:/opt/metasploit-framework/bin:$HOME/.bin:$HOME/.cargo/bin"
export EDITOR=/usr/local/bin/codium
export DOTNET_CLI_TELEMETRY_OPTOUT=1
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib -L/usr/local/opt/ncurses/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include -I/usr/local/opt/ncurses/include"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/opt/ncurses/lib/pkgconfig"
# fzf
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
# inits
test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
eval "$(starship init zsh)"

如有任何帮助,我们将不胜感激,如果我需要提供更多信息,请告诉我。

您在 .zshrc 文件中犯了两个错误:

  1. 如果你做source $ZSH/oh-my-zsh.sh,那么你不应该也做autoload -U compinit && compinit,因为前者包括后者。
  2. plugins=( ... ) 应该在 之前 source $ZSH/oh-my-zsh.sh。前者本身不做任何事情。

因此,将 .zshrc 文件的顶部更改为:

# ZSH customization
export ZSH="/Users/user/.oh-my-zsh"
plugins=(git docker asdf zsh-autosuggestions zsh-completions 
    zsh-history-substring-search zsh-syntax-highlighting)
source $ZSH/oh-my-zsh.sh
# color
[...]