Per-directory bash/zsh history log files

bashhistoryshellzsh

I find I do a lot of work on a project in a set directory. Sometimes — a few months down the track — I need to redo something on that project but I can't remember what I did. I use mercurial or git to track changes to files but I want to be able to remember what commands I issued in that directory.

Searching through my shell history is not very informative. I already log everything to my .*_history files, but I want a list of things I did in ~/foo/bar, and not all the other (million) things that I did that week. I probably can't even remember what month I last worked on that particular project.

Does anyone have any ideas how a project directory log file of all shell commands I've used? I'm envisioning a command something like:

workon myproject

… which would set the shell log file to ~/myproject/.history.log, load the previous history from that logfile, and maybe update my prompt to tell me what directory I'm working on (like e.g. vcprompt to provide version control info).

Is there anything out there like this?

Best Answer

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.

Related Question