Skip to content

Instantly share code, notes, and snippets.

@philm
Created March 12, 2015 15:56
Show Gist options
  • Save philm/b3ca8fae6241f49fd959 to your computer and use it in GitHub Desktop.
Save philm/b3ca8fae6241f49fd959 to your computer and use it in GitHub Desktop.
Creating directory specific bash aliases
# For use case and usage notes, see https://coderwall.com/p/wvsndw/directory-specific-bash-aliases
function on_leave_dir() {
if [ -e .aliases ]; then
export OLD_ALIAS_DIR="$PWD"
fi
}
function on_enter_dir() {
if [ -n "$OLD_ALIAS_DIR" ] && ! is_subdirectory "$PWD" "$OLD_ALIAS_DIR" ; then
aliases="$OLD_ALIAS_DIR/.aliases"
while IFS=':' read -r key value || [ -n "$key" ]; do
unalias "$key"
done < $aliases
unset OLD_ALIAS_DIR
echo "Unloaded local aliases"
fi
if [ -e .aliases ]; then
while IFS=':' read -r key value || [ -n "$key" ]; do
alias "$key"="${value##*( )}"
done < ".aliases"
echo "Loaded local aliases"
fi
}
function is_subdirectory() {
local child="$1"
local parent="$2"
if [[ "${child##${parent}}" != "$child" ]]; then
return 0
else
return 1
fi
}
function cd() {
on_leave_dir
builtin cd "$@"
on_enter_dir
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment