News for hackers. Stuff that matters.
Re:Ask HN: Is there a way to make bash history dir specific? by devnonymous on Wednesday May 25, 2016 @05:40AM (#11768645)
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

Re:Ask HN: Is there a way to make bash history dir specific? by zzenon on Monday May 23, 2016 @12:53PM (#11755045)
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? by brbsix on Monday May 23, 2016 @08:49PM (#11758172)
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? by andrewfromx on Tuesday May 24, 2016 @01:09PM (#11763337)
i wonder why this isn't default bash history behavior? Wouldn't it be way more useful this way?