Search code examples
zshzshrcoh-my-zsh

ZSH Completions based on a directory


I have a function set up in ZSH to quickly navigate to a folder within my ~/Sites directory.

site() { cd ~/Sites/"$1"/website/; }

So if I type site clientsite it will cd into ~/Sites/clientsite/website/, nothing too complex going on here.

I am wondering how I get this to autocomplete based on the folders that I have inside ~/Sites/.

So when I type site cli[HIT TAB] it will autocomplete to site clientsite.


Solution

  • In addition to the two lines you have:

    alias sites='cd ~/Sites'
    site() { cd ~/Sites/"$1"/website/; }
    

    Add these two lines:

    _site() { _files -W ~/Sites; }
    compdef _site site
    

    That should give you the tab completion you're looking for.