Per-directory bash/zsh history log files
In case you haven't figure this out yet: what you are looking for is the excellent virtualenvwrapper package. It is a wrapper around python's virtualenv (go figure) and, while it is commonly referred to when taking about python environments, it is actually a very generic tool that satisfies your use case.
Installation
pip install virtualenvwrapper
or
easy_install virtualenvwrapper
and add initialisation stuff into your shell config (~/.zshrc, ~/.bashrc
)
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$WORKON_HOME/projects
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true
source /usr/local/bin/virtualenvwrapper.sh
Usage
# create your env
mkvirtualenv my_project
# edit per project hooks
vim ~/.virtualenvs/my_project/bin/{postactivate,preactivate,predeactivate,etc}
# work in this env
workon my_project
You also have generic hooks in ~/.virtualenvs/{postactivate,postdeactivate,etc}
that are called every time you workon any_project
.
So, for example, having the line export HISTFILE="$VIRTUAL_ENV/bash_history"
in the ~/virtualenvs/postactivate
hook means that the $HISTFILE
variable will be expanded every time to a different project.
I need this too and I have come up with a version that uses Bash’s PROMPT_COMMAND variable:
The value of the variable PROMPT_COMMAND is examined just before Bash prints each primary prompt. If PROMPT_COMMAND is set and has a non-null value, then the value is executed just as if it had been typed on the command line.
So I say PROMPT_COMMAND="check_for_local_history" in ~/.bashrc.my, where check_for_local_history
is a function of mine that checks if the last executed command was a directory change, and when that’s true, it checks the new current directory for the .bash_history file. If it’s there, use it as the history file.
Here is the complete gist: https://gist.github.com/gurdiga/dac8d2e7eb3056d6b839