I wanted to achieve the same as asked here Saving current directory to bash history but within Zsh shell. I haven't done any Zsh trickery before but so far I have:
function precmd {
hpwd=$history[$((HISTCMD-1))]
if [[ $hpwd == "cd" ]]; then
cwd=$OLDPWD
else
cwd=$PWD
fi
hpwd="${hpwd% ### *} ### $cwd"
echo "$hpwd" >>~/.hist_log
}
Right now I save the command annotated with the directory name to a log file. This works fine for me. Just thought there might be a way to make the same replacement in the history buffer itself.