Live data from Hacker News

Ask HN: Is there a way to make bash history dir specific?

news.ycombinator.com

1–6 of 6 posts

Re: Ask HN: Is there a way to make bash history dir specific?

#2
Unfortunately I don't remember exactly how I did it at past but I'll try to help:

On my previous machine, with zsh, I could define my .zsh_history to be created per dir. That way I could see the history per dir.

The downside is multiple .zsh_history files per dir plus no universal .zsh_history (the default)

EDIT: I think I have found how I have done it. I have this currently on my .zsrch HISTFILE=~/.zhistory

removing the ~, will force zsh to create a .zhistory file on your current dir.

Hope this helps.

Re: Ask HN: Is there a way to make bash history dir specific?

#3
Bear in mind I think this is pretty crazy and I haven't tested the following, but something along these lines added to your .bashrc should work:

    set_per_dir_history(){
        history -a  # append new history lines to the existing history file
        history -c  # clear history list in memory
        export HISTFILE=$PWD/.bash_history  # set new history file (uses .bash_history in the current directory)
        history -r  # read the new history file into memory
    }

    export PROMPT_COMMAND="set_per_dir_history;$PROMPT_COMMAND"
Personally I'd add some additional logic to the set_per_dir_history function, defaulting to ~/.bash_history and only whitelisting specific directories.

Re: Ask HN: Is there a way to make bash history dir specific?

#5
post #3

Bear in mind I think this is pretty crazy and I haven't tested the following, but something along these lines added to your .bashrc should work: set_per_dir_history(){ history -a # append new history lines to the existing history file history -c # clear history list in memory export HISTFILE=$PWD/.bash_history # set new history file (uses .bash_history in the current directory) history -r # read the new history file…

i wonder why this isn't default bash history behavior? Wouldn't it be way more useful this way?

Re: Ask HN: Is there a way to make bash history dir specific?

#6
There isn't an builtin way to do this in bash but what you are asking for is 2 things:

* Trigger an action on changing a directory

* That action should be to change your bash history file

The second part is pretty easy to do -- you just change the $HISTFILE environment variable and export it. For the first part though, you can either use a naive little function that replaces bash's builtin `cd`[1] or you can use any one of the many tools specifically created for this sort of a thing:

  http://swapoff.org/ondir.html
  https://github.com/kennethreitz/autoenv
  http://direnv.net/
  https://github.com/cxreg/smartcd
Note however, that most unix tools are angnostic to the current working dir so...

  $ /path/to/cmd /some/other/path/to/argument
  $ cd /path/to && ./cmd /some/other/path/to/argument
  $ cd /some/other/path/to && /path/to/cmd argument
...might all do the same thing but will be saved in different history files if you implement what you need.

[1] http://superuser.com/a/296554/267793