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:

  1. #
  2. # Usage: mycd <path>
  3. #
  4. # Replacement for builtin 'cd', wh ich keeps a separate bash-history
  5. # for every directory.
  6. function mycd()
  7. {
  8. history -w # write current history file
  9. builtin cd "$@" # do actual c d
  10. local HISTDIR="$HOME/.dir_bash_history$PWD" # use& nbsp;nested folders for history
  11. if [ ! -d "$HISTDIR" ]&n bsp;; then # create folder if neede d
  12. mkdir -p "$HISTDIR"
  13. fi
  14. export HISTFILE="$HISTDIR/bash_history.txt" # set& nbsp;new history file
  15. history -c # clear memory
  16. history -r #read from current histfile
  17. }

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

  1. shopt -s histappend
  2. alias cd="mycd"
  3. 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...

Trackback URL for this post:

http://dieter.plaetinck.be/trackback/36
Submitted by Dieter_be on Wed, 01/30/2008 - 22:37. categories [ ]

thanks - this is a very

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

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.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Security question, designed to stop automated spam bots