You can do this with a zsh plugin: https://github.com/jimhester/per-directory-history
function set_histfile {
export HISTFILE="$HOME/.bash_history_$(pwd | tr '/' '_')"
}
PROMPT_COMMAND="set_histfile; $PROMPT_COMMAND"
There may be other ways, but probably can't do it without PROMPT_COMMAND.Someone else mentioned "direnv", too.