I have a directory where I put all my projects in, let's say it's ~/projects as an example. I've made a command called s which takes one argument, and moves me into that directory. E.g.: s foo moves me to ~/projects/foo.

What I'd like is to have a completion command of some sorts, which would act like cd so I could do keep hitting tab to go further into the ~/projects/... directories.

Basically, cd with a prefix which is always present.

I've looked into zstyle completion in man zshcompsys, but realized I just don't know enough about it to understand it properly.

link|improve this question
feedback

2 Answers

up vote 2 down vote accepted

Here's an example from my own .zshrc that I keep as a copy/paste snippet:

# Try using the below template to set up zsh functions that act
# as aliases to cd and allow you to get autocomplete nicely.

project() { cd /path/to/project/$1; }
compctl -W /path/to/project/ -/ project

Just edit the /path/to/project section in both lines above then you are good to go.

link|improve this answer
This works great, thank you! A lot simpler than I would have thought. – nifty Jan 23 at 9:54
feedback

A different way of achieving a similar effect is to define aliases for directories:

setopt auto_cd
alias -d s=~/projects

Type ~s/ Tab to change to a subdirectory of ~/projects; you can use ~s in a command argument, too.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.