terminaloh-my-zshzshrc

ZSH alias to checkout branch with branch prefix already included


I'm trying to create a ZSH alias so that I can type this:

check 123

And have it run this command:

git checkout APP-123

However this alias:

alias check="git checkout APP-"

It doesn't work. If I run check 123 it says check is not a branch and 123 is not a branch. If I run check123 it says command not found, which does makes sense.

Is there a way to create an alias to checkout a branch with the prefix already filled in?


Solution

  • If you have an alias

     alias foo="bar"
    

    and write

     foo 123
    

    this is expanded to

     bar 123
    

    Similarily if you have an alias

    alias check="git checkout APP-"
    

    and write

    check 123
    

    it is expanded to

    git checkout APP- 123
    

    which obviously doesn't do what you want. If you write check123, there is nothing which would trigger an expansion, and the shell simply searches for a command of this name in your PATH.

    You can write a shell function or a shell script to achieve the goal:

    #!/bin/zsh
    git checkout APP-${1?parameter missing}