zsh: autocomplete relative paths

I'm extensively using binstubs in my projects. So each project has a bin/ folder with commands. I've added ./bin to my $PATH, so I can call them easily (I always call the commands from the root of the project, no need to figure out the project root). That all works fine.

However, zsh won't auto-complete the commands with my current settings. How can I let zsh auto-complete commands in ./bin? Obviously the commands should only be auto-completed when I'm in the project root. When I change to another directory, they shouldn't be auto-completed any more.


Solution 1:

Add this to your ~/.zshrc file:

autoload -Uz compinit && compinit
bindkey '^I' complete-word
zstyle -e ':completion:*' command-path 'reply=( "$PWD/bin" "$path[@]" )'

The -e flag tells zstyle to eval the given string each time the completion system requests the value of command-path.