I've decided to try out zsh for a while, and while I already get completions for most everything I want, out of the box, I am missing completions for Capistrano tasks.

I had been using brynary's Bash Capistrano completion script. I was able to find a mailing list post about setting up Capistrano task completions for zsh, but it didn't quite work for me. (show_tasks isn't a valid task.) I also didn't like throwing the .cap_tasks file in the top-level of the project. I already had a ~/.zsh_cache/ directory for caching zsh's completions, so I decided to modify the script I found to put the cache file there, instead.

~/.zsh.d/S50_capistrano

_cap_does_task_list_need_generating () {
  if [ ! -f cap_tasks ]; then return 0;
  else
    accurate=$(stat -f%m ~/.zsh_cache/cap_tasks-$(echo $PWD | sha512sum))
    changed=$(stat -f%m config/deploy.rb)
    return $(expr $accurate '>=' $changed)
  fi
}

_cap () {
  if [ -f config/deploy.rb ]; then
    if _cap_does_task_list_need_generating; then
      cap -T | grep '^cap' | cut -d' ' -f2 >! ~/.zsh_cache/cap_tasks-$(echo $PWD | sha512sum)
    fi
    compadd `cat ~/.zsh_cache/cap_tasks-$(echo $PWD | sha512sum)`
  fi
}

compdef _cap cap

I have the above in ~/.zsh.d/S50_capistrano, which automatically gets loaded on startup.

~/.zshrc

for zshrc_snipplet in ~/.zsh.d/S[0-9][0-9]*[^~] ; do
    source $zshrc_snipplet
done

Update (2010-09-29): Use archive.org link to brynary's blog post.