Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 4 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.

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

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.

share|improve this answer

You can add an entry directly to the named directory hash table:

hash -d s="${HOME}/projects"

Now you can simply use ~s to refer to your directory, and you can use Tab-completion in a cd command: cd ~s/Tab

If you have zsh's autocd option set, you can leave out the cd.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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