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:
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.
* 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:
Note however, that most unix tools are angnostic to the current working dir so... ...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
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.