Skip to content

Instantly share code, notes, and snippets.

@hekt
Created September 20, 2012 17:25
Show Gist options
  • Select an option

  • Save hekt/3757203 to your computer and use it in GitHub Desktop.

Select an option

Save hekt/3757203 to your computer and use it in GitHub Desktop.
local alias
alias cd="mycd"
function mycd() {
    \cd $1
    if [ $? == 0 ]; then
        restore_aliases
        if [ -e ".bashrc.local" ]; then
            save_aliases
            source .bashrc.local
        fi
    fi
}       
function save_aliases() {
    IFS_=$IFS
    IFS=$'\n'
 
    OLD_ALIASES=()
    for i in `alias`; do OLD_ALIASES+=($i); done
     
    IFS=$IFS_
}
function restore_aliases() {
    IFS_=$IFS
    IFS=$'\n'
 
    if [ $OLD_ALIASES ]; then
        # unalias current aliases
        for i in `alias`; do
            unalias `echo $i | sed -e "s/alias \(.*\)=.*/\1/"`
        done
        # restore aliases
        for i in ${OLD_ALIASES[@]}; do
            eval $i
        done
    fi
 
    IFS=$IFS_
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment