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

Viewed 2928

a simple summary is in the title but to further explain:

Whenever i open my terminal (iterm2) i load into zsh but completions don't seem to work, then when i manually run source .zshrc it does fully load. I've tried moving stuff around in my .zshrc file to see if the order of loading was incorrect but it didn't fix anything.

My .zshrc file:

# 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)"

Any help would be appreciated, if i should provide more info please tell me.

1 Answers

You're making two mistakes in your .zshrc file:

  • If you do source $ZSH/oh-my-zsh.sh, then you shouldn't also do autoload -U compinit && compinit, because the former includes the latter.
    • Running compinit more than once can cause problems and slows down your shell’s startup. Each time you run it, it reinitializes the completion system. This will erase any completion entries that were added programmatically (rather than picked up automatically from your $fpath) after the previous compinit was run.
  • plugins=( ... ) should be done before doing source $ZSH/oh-my-zsh.sh.
    • $plugins is not special to your shell in any way. Rather, it’s a parameter that’s read by OMZ.

So, change the top of your .zshrc file to this:

# 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
[...]
Related