Reformatting the directory stack

by Mike on January 9, 2008
in Tech

One day I decided to enable zsh’s directory stack and I have been enjoyed it ever since.  The page I linked to doesn’t say this, but I auto-enabled it by putting the following in my ~/.zshrc:

setopt auto_pushd pushdminus pushdsilent pushdtohome

For a long time I just typed “dirs” and got an output like this:

~ ~/downloads ~/Projects/jython-2.1 ~/Projects/svn ~/Projects ~

Then, to go to the fourth directory in my stack,  I do this:

cd -3

Why “-3″?  Because “-0″ is the top of the stack!  That’s too much thinking– heck, I even have to count.  If you do “dirs -v” you get:

0       ~
1       ~/downloads
2       ~/Projects/jython-2.1
3       ~/Projects/svn
4       ~/Projects
5       ~

Which is a lot better,  but I didn’t want to type three extra characters to get it.  Time for an alias!  Oh, wait, why not add the minuses to it? That way I can script it and always remember  So instead of an alias, I wrote this function:

 function ldirs() {
     (( count=0))
     for x in $(dirs); do
        echo "-$count\t$x"
        ((count+=1))
     done
}

And now it looks like this:

-0      ~
-1      ~/downloads
-2      ~/Projects/jython-2.1
-3      ~/Projects/svn
-4      ~/Projects
-5      ~

Now it’s simply displayed like how I would type it — “cd -3″.

Share Your Thoughts

You must be logged in to post a comment.