Zsh – How to Create a Custom Auto_cd Command

cd-commandzsh

Instead of the normal cd I've created my own function

function cd {
    builtin cd "$@" && ls -F
}

This works great for when I use commands like cd someDir/subDir but not when I change directories via zsh's auto_cd someDir/subDir.

Is there a way to customize what command gets called for auto_cd?

Best Answer

The typical way to run-some-command-on-the-changing-of-the-directory is via the chpwd hook function (or list of functions named in the appropriately named chpwd_functions array):

% function chpwd () { pwd }  
% cd ~/tmp
/Users/jdoe/tmp
% pushd /etc
/etc ~/tmp
/etc
% chpwd_functions=( chpwd_do_ls )
% function chpwd_do_ls () { ls }
% cd /
/
Applications ...