cd, ls, cd, ls, cd, ls

During one of his presentation, I thought I had noticed that Damian Conway had his shell automatically list the contents of a directory after changing to it. A pretty good idea, as my history file confirms that I follow a cd with an ls more than 50% of the time.

The problem? In both bash & zsh, this does what you expect but not what you want:

function cd() { cd $1; ls; }

Instead, you have to use builtin so that the shell knows if you mean the user-defined or builtin version of cd:

function cd() { builtin cd $1; ls; }

If you want extra style points in zsh, you can define the chpwd function:

function chpwd() { ls; }

5 Responses to “cd, ls, cd, ls, cd, ls”

  1. ben Says:

    I wouldn’t like the cd ls combination.
    Maybe if it were ls -lrt in some directories and ls -l in others

  2. Bill Says:

    I GPL’ed the above code, so feel free to modify it.

  3. ben Says:

    If this “cd; ls” innovation shows up in SCO Unix, we are fucked.

  4. khayman Says:

    dork.

  5. Bill Says:

    Khayman’s just bitter I didn’t submit this for his QA approval.

Leave a Reply