Keeping history per working directory (cf. per shell session)

Not a neat answer but an alternative if you're using bash as your shell: you could createt some alias in your .bashrc.

For instance:

alias a='cd /tmp/A ; history -w ; history -c ; export HISTFILE=/home/user/.a_history ; history -r $HISTFILE'
alias b='cd /tmp/B ; history -w ; history -c ; export HISTFILE=/home/user/.b_history ; history -r $HISTFILE'

Then, if you type a:

  1. you will be moved in your project directory
  2. the current history will be saved (history -w)
  3. then the history kept in memory will be reset (history -c)
  4. the project history file will be set to /home/user/.a_history and read (history -r)

With zsh, you could do:

mkdir -p ~/.zsh/dirhist

And add to your ~/.zshrc:

HISTSIZE=1000
SAVEHIST=10000
setopt HIST_SAVE_NO_DUPS INC_APPEND_HISTORY
HISTFILE=~/.zsh/dirhist/${PWD//\//@}
chpwd() {
  [[ $PWD = $OLDPWD ]] || fc -Pp ~/.zsh/dirhist/${PWD//\//@}
}

chpwd() is called whenever the current directory changes. There, we reset the history file to something like ~/.zsh/dirhist/@foo@bar when you cd to /foo/bar.