/ quick tip

cd to Source

On my computer, I have all of my source code in a single directory in my home directory, ~/src. I think that this is a fairly common thing both in the concept and the location. Usually this means that I can navigate to a different project by simply cd ../<project name> but, sometimes this doesnʼt work. This is usually a problem when Iʼm somewhere in a projectʼs source tree besides the root directory. This then requires me to either keep track of how deep in a project I am or to use cd ~/src/<project name>. This is what Iʼve typically been using but, Iʼm a software developer and so I like to spend hours removing even these tiny little inefficiencies. That is a bit of an exaggeration in this case but, Iʼm sure that I spend far longer on this then it will ever save me.

So for the simple part, the function to handle changing directories.

cds() {
    cd ~/src/$1
}

Now, that works just fine but, it is slightly inefficient. It is shorter but it now requires you to type out the full directory name. This results in this method being longer for directory names longer than 4 characters which is most of them for me. The solution is to add tab completion. Luckily, this is really easy in zsh. All together it looks like this:

cds() {
    cd ~/src/$1
}

compctl -/ -W ~/src cds

Pretty simple, huh? You just have to throw that in one of zsh startup directories, such as ~/.zshrc.