Skip to content

Instantly share code, notes, and snippets.

@mdshw5
Last active November 24, 2017 10:31
Show Gist options
  • Save mdshw5/8710121 to your computer and use it in GitHub Desktop.
Save mdshw5/8710121 to your computer and use it in GitHub Desktop.
bash history in each writeable working directory
#!/bin/bash
# per-directory history
shopt -s histappend # always append to history, don't replace
export PROMPT_COMMAND="builtin history -a;$PROMPT_COMMAND" # write to history file at every new prompt
export HISTTIMEFORMAT="%m/%d/%y %T "
alias cd='cd_with_local_history'
alias history='cat $HOME/.bash_history'
export HISTFILE="$PWD/.bash_cwd_history_$USER"
function cd_with_local_history()
{
if [ -w "$@" ]
then
export HISTFILE="$PWD/.bash_cwd_history_$USER"
builtin history -w # write the local history file
cat $HOME/.bash_history .bash_cwd_history_$USER > .bash_cat_history
# preserve the global history file
mv .bash_cat_history $HOME/.bash_history
builtin cd "$@" # do actual cd
export HISTFILE="$PWD/.bash_cwd_history_$USER"
builtin history -c # clear memory
builtin history -r #read from current histfile
else
export HISTFILE="$HOME/.bash_history"
builtin cd "$@" # do actual cd
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment