Don't understand german? Read or subscribe to my english-only feed.

mika’s advent calendar – day 1: hashes in the zsh

Someone asked me to provide Zsh tips as kind of an advent calendar in my blog. I like the idea but don’t want to restrict it to Zsh only but instead provide hints that might be interesting in general for the readers of my blog. So there we go…

Of course let’s start with the best interactive shell: Zsh. If you don’t use Zsh yet please consider giving it a try, seriously. If you don’t know grml’s Zsh configuration check out "Make console work comfortable".

One nice feature of Zsh is the concept of hashes. Instead of typing /var/cache/apt/archives manually we specify a hash entry named deb running "hash -d deb=/var/cache/apt/archives". Then we can use for example "cd ~deb" instead of "cd /var/cache/apt/archives". Some further examples for inspiration (extracted from grml’s zsh configuration of course):

hash -d deb=/var/cache/apt/archives
hash -d doc=/usr/share/doc
hash -d linux=/lib/modules/$(command uname -r)/build/
hash -d log=/var/log
hash -d slog=/var/log/syslog
hash -d src=/usr/src
hash -d www=/var/www

So you don’t have to type "ls -la /usr/share/doc/zsh/" but just use "ls -la ~doc/zsh/" and instead of "cd /var/www" you can use "cd ~www". Got the idea?

4 Responses to “mika’s advent calendar – day 1: hashes in the zsh”

  1. novoid Says:

    As a matter of fact, you do not even have to type in “cd ~hashname”!

    You can simply stick to “~hashname” (without the “cd “) whenever you want to jump into a
    “hashed” directory ;-)

  2. mika Says:

    @novoid: but only if you use ‘setopt autocd’ :)

    regards,
    -mika-

  3. daniel Says:

    why is that better than environment variables?

  4. mika Says:

    @daniel: environment variables can be overriden too easily by accident and they might trigger unwanted behaviour in programs. And if you think that aren’t enough arguments for you :) – check out:

    mika@grml ~ % cd ~deb
    mika@grml ~deb % pwd
    /var/cache/apt/archives
    mika@grml ~deb % cd ~www
    mika@grml ~www %

    regards,
    -mika-