Luis Abreu
Luis Abreu

Reputation: 4560

zsh: compinit and autocomplete function redefinition

I'm trying to learn how autocompletion works in zsh. I've got a simple script file (example.zsh) and I'm trying to create a simple autocomplete function that describes each of its parameters. In order to do that, I've started by creating a simple _example file which looks like this:

#compdef create_ca

_arguments \
    "--caKey[name of the file that will hold the keys used for generating the certificate (default: ca.key)]" \
    "--caCrt[name of the file that will hold the certificate with the public key (default: ca.crt)]" \
    "--cn[common name for the root certificate (default: root.GRM)]" \
    "--days[number of days that certificate is valid for (default: 10500)]" \
    "--size[key size (default: 4096)]"  \
    "--help[show this help screen]"

The file is on the same folder as the script and I've updated my .zshrc file so that it adds that folder to the $fpath:

fpath=(~/code/linux_certificates $fpath)

autoload -Uz compinit
compinit -D

I'm using the D option so that the .zcompdump isn't generated. At first sight, everything worked out, but when I tried to update the helper autocomplete function, I'm unable to see thosee changes (ex.: changing the description). I've tried re-running the compinit command and, when using the cache .zcompdump, deleting that file. However, it simply didn't work. The only way I've managed to get it working was by deleting the autocomplete helper function with:

unfunction _create_ca

Is this the expected behavior? I mean, should't running compinit -D be enough to reload my helper autocomplete function?

btw, any good tutorials on how to create autocomplete functions (besides the official docs)?

thanks.

Upvotes: 2

Views: 684

Answers (1)

Marlon Richert
Marlon Richert

Reputation: 7020

Once a function has been loaded, it will not be loaded again. That’s why you first have to unfunction your function, causing Zsh to unload it, so it can be loaded again.

Alternatively, you can just use exec zsh to restart your shell.

Upvotes: 0

Related Questions