Skip to content

Instantly share code, notes, and snippets.

@axel-angel
Created January 21, 2017 13:55
Show Gist options
  • Save axel-angel/293e7d9ad1cbb9c08af7ee43bb08a1b5 to your computer and use it in GitHub Desktop.
Save axel-angel/293e7d9ad1cbb9c08af7ee43bb08a1b5 to your computer and use it in GitHub Desktop.
#!/bin/zsh
# relative history
relative_histfile() {
searchdir="$(readlink -f $PWD)";
# default values
if [ -z "$HISTDIR" ]; then
HISTDIR="$HOME/.histfiles/";
fi
if [ -z "$HISTFILE_DEFAULT" ]; then
HISTFILE_DEFAULT="$HOME/.histfile"
fi;
# find first histfile starting from pwd up to /
while true; do
histtarget="${HISTDIR}/${searchdir//\//%}"
if [ -e "$histtarget" ] || [ "$searchdir" = "/" ]; then
break;
fi;
searchdir="$(readlink -f "$searchdir/..")";
done;
# load relative history or default one
if [ "$searchdir" = "/" ]; then
if [ "$HISTFILE" != "$HISTFILE_DEFAULT" ]; then
echo "> load default history";
fc -Pp "$HISTFILE_DEFAULT";
fi;
else
echo "> load history for $searchdir";
fc -Pp "$histtarget";
fi;
}
chpwd_functions+=(relative_histfile);
relative_histfile;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment