When I use a terminal like iTerm or Hyper, right as I boot it up, the first message I get is:

complete:13: command not found: compdef

I don't know what this is from, and how to get rid of it. This message does not show up on the default terminal app though.

I am using zsh.


8 Answers 8


This is the same issue I got on my mac OS. I am using zsh shell.

Zsh Compdef error

Compdef is basically a function used by zsh for load the auto-completions. The completion system needs to be activated. If you’re using something like oh-my-zsh then this is already taken care of, otherwise you’ll need to add the following to your ~/.zshrc

autoload -Uz compinit

Completion functions can be registered manually by using the compdef function directly like this compdef . But compinit need to be autoloaded in context before using compdef.

  • This worked for me. On my system, it was caused by adding the completion for the "flux" cli. Adding these lines before the "flux" commands fixed the issue. It does seem this should be the accepted answer.
    – Steve Wall
    Nov 16, 2021 at 22:03
  • 5
    FYI, this happened to me after installing terraform auto-complete. It looks like it put in a line for bash autocomplete instead of zsh auto-complete: autoload -U +X bashcompinit && bashcompinit Sep 13, 2022 at 20:17
  • 1
    This should be the accepted answer!
    – ivandov
    Nov 23, 2022 at 17:14
  • 2
    I needed both: autoload -U +X compinit && compinit and autoload -U +X bashcompinit && bashcompinit to resolve this.
    – colm.anseo
    Apr 14, 2023 at 20:39

After more research, I have found my answer. There was a block in my .zprofile:

export NVM_DIR="/Users/Aaron/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

The last line loads compdef. But when doing this it caused some sort of confliction. All I needed to do was comment out:

# [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

And the annoying error went away. And the best part: I still have zsh tab completion.

  • That was exactly my problem as well, thank you. Jan 14, 2021 at 10:08
  • This should not be the accepted answer! This solution will only disable shell completion for nvm. Any other scripts trying to load shell completion will continue to pop the original error. Use the solution provided by @Gopesh.
    – ivandov
    Nov 23, 2022 at 17:10

Update: The issue is already fixed in newer versions of nvm. Update nvm using the install script and the error should go away (tested with v0.37.2)

I had the same problem, after some research I found that the root of the issue was a nvm code snippet in the .zshrc (could also be .zprofile) file.

export NVM_DIR="/Users/Aaron/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

Like suggested in another answer, commenting the third line does solve the issue, but, surprise surprise, bash completion for nvm commands will not be available anymore.

The real problem is at the end of the bash_completion script:

# complete is a bash builtin, but recent versions of ZSH come with a function
# called bashcompinit that will create a complete in ZSH. If the user is in
# ZSH, load and run bashcompinit before calling the complete function.
if [[ -n ${ZSH_VERSION-} ]]; then
  autoload -U +X bashcompinit && bashcompinit

But to call bashcompinit, compinit must have been called before and at this time it isn‘t. (I think this should/could be fixed in the nvm script, I will create an issue on github.)

If you want to have nvm completion, you can just call compinit yourself before the script runs.

autoload -Uz compinit
compinit -i

export NVM_DIR="/Users/Aaron/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

The -i option tells compinit to silently ignore all insecure files and directories, which might prevent further errors in the future.

  • Weirdly I didn't have this issue until I moved to M1 Mac. Now I have it and this solves it, but it's totally unexpected...
    – milosmns
    Jun 1, 2022 at 18:02
  • @milosmns Did you solve it by updating nvm or by calling compinit? Jun 12, 2022 at 13:00
  • It was a clean nvm installation on my side, so super modern and latest versions, still having the problem. When I added the compinit stuff it started working again
    – milosmns
    Jun 12, 2022 at 13:03

For Angular 14 users on Mac M1

This happens when you run:

ng completion

Check the following file:

sudo nano .zshrc

Make sure it looks like this:

# Add RVM to PATH for scripting. Make sure this is the last PATH variable chang$
export PATH="$PATH:$HOME/.rvm/bin"

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_HOMEBREW" ] && \. "$NVM_HOMEBREW"

autoload -Uz compinit

# Load Angular CLI autocompletion.
source <(ng completion script)

If not then modify it and make sure you have installed:

  1. Homebrew
  2. NVM by homebrew
  3. Node by NVM
  4. The order is important: compinit should be before ng completion script
  • Same thing happens with intel Mac's. This solve the problem. Thanks. Feb 16, 2023 at 7:44

I also receieved this error, but I had a different solution.

My .zshrc file had this:

# Setup Kubectl completion
if [ /usr/local/bin/kubectl ]; then source <(kubectl completion zsh); fi

# The next line enables shell command completion for gcloud.
if [ -f '/Users/my-user-name/google-cloud-sdk/completion.zsh.inc' ]; then . '/Users/my-user-name/google-cloud-sdk/completion.zsh.inc'; fi

I needed to place the kubectl completion part AFTER executing the completion.zsh.inc script:

# The next line enables shell command completion for gcloud.
if [ -f '/Users/my-user-name/google-cloud-sdk/completion.zsh.inc' ]; then . '/Users/my-user-name/google-cloud-sdk/completion.zsh.inc'; fi

# Setup Kubectl completion
if [ /usr/local/bin/kubectl ]; then source <(kubectl completion zsh); fi
  • These are not getting stuck if I initialize a new terminal... Is there any reason? Feb 22, 2021 at 22:49

It depends on what you set as your default (login-)shell for the system, for Terminal, for iTerm. Updated bash installed and set to execute in one of them gives differing results.

This error comes from a so called dot-file you installed. These are files with a dot in front (hence the name) like .bashrc. That means they are invisible to the Finder and to a simple ls. The dot-files in question are read by your shell when it starts up. They contain instructions, settings, customisations; maybe little snippets of program code.

Not all dot-files in your users home directory are read by the shell. Those read by bash may include: .bash_history .bash_profile .bash_prompt .bash_sessions .bashrc .editorconfig .exports .functions .inputrc .profile (This list is not exhaustive)

Those files may have been created by you, manipulated by you or by certain programs like fink, homebrew or ports. You may have just copied those from the net to "upgrade"/personalize your shell.

Regardless of how you arrived at the current situation. Open your Terminal/iTerm from another user and the symptoms will probably not be observed.

That means: all those files are in a sense 'safe to delete': in the sense that they are not essential for running your Terminal or your shell. Make a backup of those in case they indeed contain personalised settings you may depend on in your daily work. Examples for that include additions to your $PATH variables. Without those you may find programs like fink no longer working.

However the actual error message you posted indicates two things:

  • The combined content of your dot-files did not work to accomplish what they were supposed to do. More exact: an instruction (compdef) was called and that is not available.

  • That you very probably sourced a bunch of dot-files from a source on the net like holman-dotfiles. These included a bug that had the exact same error message.

So you should look through all of the dot-files for your current user and keep only the stuff you recognise as necessary (remember the backups). Then you might use the updated files from your source again. But keep in mind that these are quite 'comprehensive'. It would be prudent to not use them all blindly and actually test and pick a subset of all those options.

Using them all blindly or indiscriminately may not only lead to issues like the one prompting this question but also when following other how-tos or troubleshooting advice.

Update: This answer was given to a previous version of the question that did not indicate zsh as the shell used. -> Substitute zsh where you read bash above.

  1. Go to your Home folder
  2. Press Cmd+Shift+. to reveal the hidden files
  3. Locate the .zshrc file & open it in a text editor
  4. Add the clear command to the end of the file
  5. Save it and reopen your Terminal

Screenshot of the .zshrc file open in a text editor with the clear command at the end


Reinstall Brew

by deleting ~/.zprofile and install afresh

  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Thinkr
    May 8, 2023 at 12:00
  • No to this. Your .zprofile contains much more than just what Homebrew added. Deleting this can have unintended consequences from your customizations (minor) to PATH and ENVIRONMENT variables (major)
    – Allan
    May 8, 2023 at 13:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .