Why can't zsh execute command compdef?
Asked Answered
A

9

21

I have installed zsh and oh my zsh on Ubuntu 18.04. I would like to use autocompletion for kubectl. Therefore I have added source <(kubectl completion zsh) to my ~/.zshrc file.

On execution of this script zsh gets the following error:

complete:13: command not found: compdef

The kubectl documentation states that when one gets the error above, you should put the following on top of the .zshrc file:

autoload -Uz compinit
compinit

After doing this and restarting the terminal, I get the same error.

In a git-issue I found the following helped people with a common issue:

#This will perform chmod g-w for each file returned by compaudit to remove write access for group
compaudit | xargs -I % chmod g-w "%"
#This will perform chown to current user (Windows and Linux) for each file returned by compaudit
compaudit | xargs -I % chown $USER "%"
#Remove all dump files (which normally speed up initialization)
rm ~/.zcompdump*
#Regenerate completions file
compinit

zsh logs the following while running the script:

kubescript:12457: command not found: _bash_comp

Unfortunately this did not solve my problem. What else can I do to fix my issue? Or even still: what can I do to find out what is causing it?

Apollo answered 8/10, 2019 at 9:32 Comment(6)
Try this one unix.stackexchange.com/questions/339954/… Some users suggest also that this error may occur if you have two different versions of zsh installed.Grisham
Unfortunately the suggested link does not help. And I am pretty sure I have only one version of zsh installed.Apollo
Did you try solutions from both answers ?Grisham
Yes I did. Both without a result.Apollo
It looks like this issue github.com/kubernetes/kubectl/issues/125Grisham
It's exactly that. It seems there is no solution for now...Apollo
D
46

I fixed the error by using the following code in .zshrc:

# K8s auto-complete
autoload -U +X compinit && compinit
source <(kubectl completion zsh)

You may also do it using oh-my-zsh plugin if you use oh-my-zsh.

Documentary answered 24/2, 2020 at 20:2 Comment(0)
W
9

For my OSX 10.15.7 I did something similar

vi ~/.zshrc

alias k=kubectl
autoload -U +X compinit && compinit
[[ /usr/local/bin/kubectl ]] && source <(kubectl completion zsh)

Works like a charm!

Womankind answered 12/10, 2020 at 7:34 Comment(0)
N
7

I had the same issue and it was solved updating nvm

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Nitrogenous answered 11/4, 2020 at 20:14 Comment(2)
No difference here, though.Apollo
What is the relation to nvm here?Eriha
H
7

I encountered this after installing the Angular CLI. Turns that Angular adds something into your .zshrc file.

If you recently installed Angular CLI, open ~/.zshrc and remove the lines added by Angular CLI.

Holusbolus answered 20/6, 2022 at 21:12 Comment(2)
My ~/.zshrc has this line source <(ng completion script). It has a left angle bracket but no right angle bracket. Yesterday my terminal was blocked for hours from running commands because of this script. I commented out the line and now my terminal works. Thanks!Grommet
@ThomasDavidKehoe this gave me the following error .zshrc:8: parse error near \n'`Basseterre
E
3

After trying lot of options and going through diff treads. It worked for me after running below command

autoload -Uz compinstall && compinstall

This configures the ~/.zshrc and initialize the compinit by adding these 2 below lines:

autoload -Uz compinit
compinit

and then run below sudo commands which mentioned in link

$ sudo chmod -R 755 /usr/local/share/zsh
$ sudo chown -R root:staff /usr/local/share/zsh
$ compaudit | xargs chmod g-w

at last restart the terminal.

Hope this helps for someone.

Elnaelnar answered 9/6, 2021 at 23:50 Comment(1)
I ran your suggested autoload -Uz compinstall && compinstall (following the bare minimum prompts until my .zshrc file had the 2 lines you mention), then restarted my terminal (I did not attempt running the sudo commands), and my problem was fixed. Interestingly, after restarting my terminal, I tried commenting out the autoload -Uz compinit and compinit lines in my .zshrc file and everything still worked as expected (even after terminal restarts)... so that initial autoload -Uz compinstall && compinstall command fixed my problem without needing the ~/.zshrc changes.Jameljamerson
Z
0

In my case the issue was fixed after re-installing oh-my-zsh:

  1. Deleted my old version rm -rf ~/.oh-my-zsh
  2. Installed from https://github.com/ohmyzsh/ohmyzsh
Zwolle answered 11/10, 2020 at 11:41 Comment(0)
W
0

I really tried every answer here, but nothing worked.

So, I tried this tutorial paying attention to the last observation:

"Note: Make sure you add this snippet before any call to compdef else you will still see the error"

I had a complete call from Terraform installation in my ~/.zshrc and ~/.bash_profile files.

https://thysmichels.com/2020/07/16/mac-solved-command-not-found-compdef/

Wendt answered 8/12, 2020 at 2:52 Comment(0)
H
0

For me I had this line:

source ~/.oh-my-zsh/plugins/git/git.plugin.zsh

In my ~/.zshrc file. Which was trying to source the plugin before it is loaded by plugins=(git)

Removing that line fixed it for me.

If you have added the plugins and everything works well, One of the reasons is you may be trying to use the plugin before it is loaded. As in my case.

Hark answered 7/6, 2022 at 12:20 Comment(0)
W
0

This issue is (also) faced after installing Angular CLI (Angular Version 14.x) and accepting addition of autocompletion for the cli commands. Snippet from .zshrc:

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

Commenting out the source line resolved the issue for me (macOS 12.6 (21G115)).

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

Still need to work out the root cause.

Windbroken answered 22/9, 2022 at 6:15 Comment(2)
Duplicate of Norbu Sonam's answer -- showing the impacting lines of .zshrc.Windbroken
One feels so welcome here.Windbroken

© 2022 - 2024 — McMap. All rights reserved.