Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define oh my zsh aliases per folder

I have a different name for my heroku remote depending on the project, and I would like to have an alias for each of my projects to push to heroku, is there anyway to define an alias per folder, so that inside my project folder I can define the alias pointing to the right heroku remote?

like image 695
ryudice Avatar asked Dec 25 '22 20:12

ryudice


1 Answers

I would suggest using a function instead of aliases.

# paths to projects 
#  - must be absolute 
#  - must not contain symlinks
#  - must not end with "/"
# run `pwd -P` in the project root to get the correct value
PROJ_A="/path/to/project/a"
PROJ_B="/path/to/project/b"

pushit () {
    case $(pwd -P) in 
        ( $PROJ_A | $PROJ_A/* )
            # Put command(s) needed to push A here
        ;;
        ( $PROJ_B | $PROJ_B/* ) 
            # Put command(s) needed to push B here
        ;;
        ( * )
            echo "You are not in a project directory"
        ;;
    esac
}

Reasoning:

While it is possible to modify aliases every time the current working directory is changed by utilizing the chpwd hook, you only need to know which command to use when you are actually pushing to heroku. So most of the time the aliases would be modified for nothing.

Using a function, the appropriate decisions can be made when you actually need it. And as all of the logic used in the function would be needed anyway when going the chpwd route, so you actually reduce the lines of code needed when using the function directly.

like image 140
Adaephon Avatar answered Jan 05 '23 23:01

Adaephon