Wed, 30 Jan 2008

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.

::Read from here
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...

Comments

thanks - this is a very useful script

do you miss having a global history or is there some way of getting all history displayed in sequential order?

Actually I never got to really using it like this. I don't like switching to non-default setups, especially if they don't work exactly the way I want.


Name:


E-mail:


URL:


Comment:


What is the first name of the guy blogging here?


This comment form is pretty crude. Make sure mandatory fields are entered correctly.
Basic html tags (a,i,b, etc) are allowed, others are sanitized