Per-directory bash history

I've been thinking about how a specific bash history for each directory could improve productivity, and unlike what I feared it was actually pretty easy to find a solution on the net.
Quote from an anonymous reply on debian-administration.org


So I use the following bash function:
#
# Usage: mycd <path>
#
#  Replacement for builtin 'cd', which keeps a separate bash-history
#   for every directory.
function mycd()
{
   history -w # write current history file
   builtin cd "$@" # do actual cd
   local HISTDIR="$HOME/.dir_bash_history$PWD" # use nested folders for history
   if  [ ! -d "$HISTDIR" ]; then # create folder if needed
      mkdir -p "$HISTDIR"
   fi
   export HISTFILE="$HISTDIR/bash_history.txt" # set new history file
   history -c  # clear memory
   history -r # read from current histfile
}

and then set it up with the following in my bashrc:

shopt -s histappend
alias cd="mycd"
export HISTFILE="$HOME/.dir_bash_history$PWD/bash_history.txt"

Great stuff. It would be nice to be able to use both the global and the directory-specific history by combining with an extra modifier key .
( eg arrowup/pageup/c^r for global and alt+arrowup/alt+pageup/alt+c^r for the directory-specific one )

If I ever come up with / find something to do this I'll let you know...

Add comment