Skip to content

Instantly share code, notes, and snippets.

@maligree
Forked from Paczesiowa/autoload aliases
Last active August 29, 2015 13:56
Show Gist options
  • Save maligree/9087438 to your computer and use it in GitHub Desktop.
Save maligree/9087438 to your computer and use it in GitHub Desktop.
MY_OLD_PWD=""
MY_ALIASES=""
function load_aliases() {
if [ "${PWD}" != "${MY_OLD_PWD}" ]; then
echo "PWD differs"
MY_OLD_PWD="${PWD}"
for NAME in ${MY_ALIASES}; do
unalias "${NAME}"
done
MY_ALIASES=""
ALIAS_FILE=".aliases"
# Traverse the tree upwards to find a suitable .aliases file.
d=`pwd`;
while [ "$d" != "/" ]; do
if [ -f "$d/$ALIAS_FILE" ]; then
# Found it, read it in.
echo "Found in $d";
while read LINE; do
echo "Setting $LINE";
NAME=$(echo ${LINE} | perl -pe 's|=.*||')
VALUE=$(echo ${LINE} | perl -pe 's|.*?=||')
if [ -n "$MY_ALIASES" ]; then
MY_ALIASES="${MY_ALIASES} ${NAME}"
else
MY_ALIASES="$NAME"
fi
alias "${NAME}=${VALUE}"
done < "$d/${ALIAS_FILE}"
break
fi;
d=`dirname "$d"`
done
fi
}
if [ -n "$BASH_VERSION" ]; then
export PROMPT_COMMAND=load_aliases
elif [ -n "$ZSH_VERSION" ]; then
if [ -z "$chpwd_functions" ]; then
chpwd_functions=($(chpwd_functions[@]) "load_aliases")
else
chpwd_functions=("load_aliases")
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment