twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
12.85k stars 477 forks source link

How to enable completion for zsh #2556

Closed jm33-m0 closed 1 year ago

jm33-m0 commented 1 year ago

What exactly are you trying to do?

I am trying to enable auto completion for zsh shell using chezmoi completion zsh

What have you tried so far?

  1. Put the completion.zsh file in ~/zsh and add ~/zsh to $fpath
  2. Start a new zsh shell, chezmoi the press TAB, nothing but filename completion pops up

Tried to source the file, no luck either.

Where else have you checked for solutions?

There is no instructions in the guides, they just tell me to generate the scripts, which I think everyone knows. The important part is how to enable it, since you don't have packages for most distros other than Arch.

Output of any commands you've tried with --verbose flag

$ chezmoi --verbose $COMMAND

Output of chezmoi doctor

```console $ chezmoi doctor RESULT CHECK MESSAGE ok version v2.10.1, commit 6ed080bba2bd3f439832c2bd4b14ed2812c8ee9b, built at 2022-01-23T19:18:07Z, built by goreleaser ok os-arch linux/amd64 (Fedora Linux 36 (Workstation Edition)) ok go-version go1.17.6 (gc) ok executable ~/bin/chezmoi ok upgrade-method replace-executable ok config-file ~/.config/chezmoi/chezmoi.toml ok source-dir ~/.local/share/chezmoi is a directory ok suspicious-entries no suspicious entries ok working-tree ~/.local/share/chezmoi is a directory ok dest-dir ~ is a directory ok shell found /usr/bin/zsh ok edit-command found /usr/bin/nvim ok umask 022 ok git-command found /usr/bin/git, version 2.38.1 ok merge-command found /usr/bin/vimdiff info age-command age not found in $PATH ok gpg-command found /usr/bin/gpg, version 2.3.7 info pinentry-command not set info 1password-command op not found in $PATH info bitwarden-command bw not found in $PATH info gopass-command gopass not found in $PATH info keepassxc-command keepassxc-cli not found in $PATH info keepassxc-db not set info lastpass-command lpass not found in $PATH info pass-command pass not found in $PATH info vault-command vault not found in $PATH info secret-command not set ```

Additional context

Add any other context about the problem here.

jm33-m0 commented 1 year ago

I had to put it in /usr/share/zsh/site-functions to make it work, but ~/zsh is also in $fpath, I'm really confused.

jm33-m0 commented 1 year ago

I suspect it's a permission issue as zsh refuses to load anything from my home directory unless I source them.

twpayne commented 1 year ago

The completion file needs to be called _chezmoi. If the filename does not begin with an underscore then zsh will ignore it.

jm33-m0 commented 1 year ago

I did rename it to _chezmoi but it didn't work. zsh doesn't even load it when it's in my home directory.

I'm not sure if it's some selinux policy preventing zsh to load the functions. As you can see here, if there's a COPR repo for chezmoi it would be very useful, and users get to update chezmoi easily.

~ 
❯ ls ~/.local/share/zsh/site-functions/_chezmoi
/home/jm33/.local/share/zsh/site-functions/_chezmoi

~ 
❯ echo $fpath                                  
/usr/local/share/zsh/site-functions /usr/share/zsh/site-functions /usr/share/zsh/5.8.1/functions /home/jm33/.local/share/zsh/site-functions

~ 
❯ chezmoi 
Completing file
bin/                            kmod.log                        Templates/                  
twpayne commented 1 year ago

Adding chezmoi to COPR is tracked in #1800.

2560 adds a completion template function, which means that when chezmoi 2.27.0 is released you can make ~/.local/share/zsh/site-functions/_chezmoi a template managed by chezmoi with the contents:

{{- completions "zsh" -}}

zsh doesn't even load it when it's in my home directory ... I'm not sure if it's some selinux policy preventing zsh to load the functions

Are you able to reproduce this problem on a new machine or in a docker container?

jm33-m0 commented 1 year ago

I found the reason, zsh reads functions before my fpath+= statement.

fpath+=("$HOME/.local/share/zsh/site-functions") # here it should be
autoload -Uz compinit
# fpath+=("$HOME/.local/share/zsh/site-functions") # it was here

Basically my fpath changes after compinit so anything under the newly added path won't be loaded.

I used zsh -v 2>/tmp/zsh.log to debug this issue.

Thanks for the help, Hope we will soon have a COPR for chezmoi!

twpayne commented 1 year ago

Glad you found the solution, and thank you very much for sharing it here :)