Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZSH alias with parameter

I am trying to make an alias with parameter for my simple git add/commit/push.

I've seen that a function could be used as an alias, so I tried but I didn't make it.

Before I had:

alias gitall="git add . ; git commit -m 'update' ; git push"

But I want to be able to modify my commits:

function gitall() {
    "git add ."
    if [$1 != ""]
        "git commit -m $1"
    else
        "git commit -m 'update'"
    fi
    "git push"
}
like image 688
albttx Avatar asked Oct 09 '22 23:10

albttx


2 Answers

If you really need to use an alias with a parameter for some reason, you can hack it by embedding a function in your alias and immediately executing it:

alias example='f() { echo Your arg was $1. };f'

I see this approach used a lot in .gitconfig aliases.

like image 192
joelpt Avatar answered Oct 16 '22 08:10

joelpt


You can't make an alias with arguments*, it has to be a function. Your function is close, you just need to quote certain arguments instead of the entire commands, and add spaces inside the [].

gitall() {
    git add .
    if [ "$1" != "" ] # or better, if [ -n "$1" ]
    then
        git commit -m "$1"
    else
        git commit -m update
    fi
    git push
}

*: Most shells don't allow arguments in aliases, I believe csh and derivatives do, but you shouldn't be using them anyway.

like image 179
Kevin Avatar answered Oct 16 '22 10:10

Kevin