ZSH Function With Completion.

Today I wrote a little function in mit .zshrc. The goal was to have a very short command to switch into another project’s directory on the command line. For example typing p neckhair.ch would cd into the directory ~/src/neckhair.ch. Writing the function alone is very easy. In your ~/.zshrc add the following code:

function p() {
  cd ~/src/$1
}

This already worked. But the problem was: There was no autocompletion of the project directory. To achieve this I had to add another function to .zshrc:

function pcompl() {
  reply=( $(ls ~/src) )
}
compctl -K pcompl p

Now I can type p n<tab> and I get a list of projects starting with n. Like I was used to from typing cd ~/src/n<tab>.