Python tip: zcd

Tim Wintle - December 16th, 2009

Another developer here suggested this to me (Thanks Rich!) – but every time I use it I think it’s really useful, so I thought I’d share it.

When you’re working with a python package like “product.section.part”, you end up with a directory tree like so:

product.section.part
    + product
        + section
            + part
               - module.py

Now, navigating through these directories is a pain – each time you want to edit module.py, you find yourself typing something like:

cd p[tab]p[tab]/s[tab]p[tab]
[editor] m[tab]
# (the "/" is required to get past the .egg-info file)
# i.e. this expands to
$ cd product.section.part/section/part/
$ [editor] module.py

Rich simply added a simple function to my .bashrc to do this automatically – so I now type

zcd p[tab]
editor m[tab]

#i.e. this expands to
$ zcd product.section.part/
$ [editor] module.py

- It’s much simpler, and makes moving between packages that little bit nicer !
Here’s the script to add to your ~/.bashrc:

function zcd() {
     X=`echo $1 | sed -e "s/\\./\\//g"`
     cd $1/$X
 }

3 Responses to “Python tip: zcd”

  1. Here is a slightly more advanced version that can deal with a src/ subfolder as well and falls back on the normal cd if neither directory can be found

    function zcd() {
    target=$(echo $1 | sed -e “s/\\./\\//g”)
    if [ -d "$1/${target}" ] ; then
    cd $1/$target
    elif [ -d "$1/src/${target}" ] ; then
    cd $1/src/$target
    else
    cd $1
    fi
    }

  2. [...] here to see the original: Python tip: zcd | We Are Team Rubber By admin | category: python | tags: been-involved, developer, few-personal, game-projects, [...]

  3. Autojump takes zcd, spanks it, and leaves it in a ditch:

    http://wiki.github.com/joelthelion/autojump

    Highly recommended!

Leave a Reply