5. Directories — Zsh 5.9.999.3-test documentation

# 5. Directories

One nice feature of `zsh` is the way it prints directories. For example, if we set the prompt like this:

Example 5.1: Tilde in the prompt

    phoenix% PROMPT='%~> '
    ~> cd src
    ~/src>

the shell will print the current directory in the prompt, using the `~` character. However, `zsh` is smarter than most other shells in this respect:

Example 5.2: Named directory shortcuts

    ~/src> cd ~subbarao
    ~subbarao> cd ~maruchck
    ~maruchck> cd lib
    ~maruchck/lib> cd fun
    ~maruchck/lib/fun> foo=/usr/princeton/common/src
    ~maruchck/lib/fun> cd ~foo
    ~foo> cd ..
    /usr/princeton/common> cd src
    ~foo> cd news/nntp
    ~foo/news/nntp> cd inews
    ~foo/news/nntp/inews>

Note that `zsh` prints other users’ directories in the form `~user`. Also note that you can set a parameter and use it as a directory name; `zsh` will act as if `foo` is a user with the login directory `/usr/princeton/common/src`. This is convenient, especially if you’re sick of seeing prompts like this:

Example 5.3: Long path in prompt

    phoenix:/usr/princeton/common/src/X.V11R4/contrib/clients/xv/docs>

If you get stuck in this position, you can give the current directory a short name, like this:

Example 5.4: Shorten path via parameter

    /usr/princeton/common/src/news/nntp/inews> inews=$PWD
    /usr/princeton/common/src/news/nntp/inews> echo ~inews
    /usr/princeton/common/src/news/nntp/inews
    ~inews>

When you reference a directory in the form `~inews`, the shell assumes that you want the directory displayed in this form; thus simply typing `echo ~inews` or `cd ~inews` causes the prompt to be shortened. You can define a shell function for this purpose:

Example 5.5: namedir helper function

    ~inews> namedir () { $1=$PWD ;  : ~$1 }
    ~inews> cd /usr/princeton/bin
    /usr/princeton/bin> namedir pbin
    ~pbin> cd /var/spool/mail
    /var/spool/mail> namedir spool
    ~spool> cd .msgs
    ~spool/.msgs>

You may want to add this one-line function to your `.zshrc`. `zsh` can also put the current directory in your title bar, if you are using a windowing system. One way to do this is with the `chpwd` function, which is automatically executed by the shell whenever you change directory. If you are using xterm, this will work:

Example 5.6: chpwd for xterm title

    chpwd () { print -Pn '^[]2;%~^G' }

The `-P` option tells `print` to treat its arguments like a prompt string; otherwise the `%~` would not be expanded. The `-n` option suppresses the terminating newline, as with `echo`.

If you are using an IRIS `wsh`, do this:

Example 5.7: chpwd for IRIS wsh

    chpwd () { print -Pn '\2201.y%~\234' }

The `print -D` command has other uses. For example, to print the current directory to standard output in short form, you can do this:

Example 5.8: print -D shortens PWD

    % print -D $PWD
    ~subbarao/src

and to print each component of the path in short form:

Example 5.9: print -D shortens path

    % print -D $path
    /bin /usr/bin ~locbin ~locbin/X11 ~/bin