Skip to content

Instantly share code, notes, and snippets.

@slowkow
Forked from leipzig/mycd.sh
Last active September 8, 2015 15:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save slowkow/11133999 to your computer and use it in GitHub Desktop.
Save slowkow/11133999 to your computer and use it in GitHub Desktop.
Record folder-specific history in `.dir_bash_history`
function cd_dir_history()
{
OLDPWD="$PWD"
echo "# $(date) $USER -> $@" >> "$HISTFILE"
command cd "$@"
# If this directory is writable then write to directory-based history file
# otherwise write history in the usual home-based history file.
touch "$PWD/.dir_bash_history" 2>/dev/null \
&& export HISTFILE="$PWD/.dir_bash_history" \
|| export HISTFILE="$HOME/.bash_history";
echo "# $(date) $USER <- $OLDPWD" >> "$HISTFILE"
}
# Replace the regular `cd` command.
alias cd="cd_dir_history"
# Initial shell opened.
export HISTFILE="$PWD/.dir_bash_history"
# Timestamp all history entries.
export HISTTIMEFORMAT="%h/%d - %H:%M:%S "
export HISTCONTROL=ignoredups:erasedups
export HISTSIZE=1000000
export HISTFILESIZE=1000000
shopt -s histappend # Append, no clearouts.
shopt -s histverify # Edit a recalled history line before executing.
shopt -s histreedit # Reedit a history substitution line if it failed.
# Save the history after each command finishes.
# (and keep any existing PROMPT_COMMAND settings).
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment