ZSH alias with parameter

87,030

Solution 1

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.

Solution 2

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.

Solution 3

I used this function in .zshrc file:

function gitall() {
    git add .
    if [ "$1" != "" ]
    then
        git commit -m "$1"
    else
        git commit -m update # default commit message is `update`
    fi # closing statement of if-else block
    git push origin HEAD
}

Here git push origin HEAD is responsible to push your current branch on remote.

From command prompt run this command: gitall "commit message goes here"

If we just run gitall without any commit message then the commit message will be update as the function said.

Solution 4

"git add ." and the other commands between " are just strings for bash, remove the "s.

You might want to use [ -n "$1" ] instead in your if body.

Solution 5

I tried the accepted answer (Kevin's) but was getting the following error

defining function based on alias `gitall'
parse error near `()'

Hence changed the syntax to this, based on the git issue and it worked.

    function gitall {
    git add .
    if [ "$1" != "" ]
    then
        git commit -m "$1"
    else
        git commit -m update
    fi
    git push
    }
Share:
87,030
albttx
Author by

albttx

Gratuated student in 42 School. Gopher and DevOps while (42) learn_everything_as_possible(); From France

Updated on February 21, 2022

Comments

  • albttx
    albttx about 2 years

    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"
    }
    
  • chepner
    chepner over 8 years
    csh does, but it doesn't have functions at all. (I don't know if there are no functions because aliases can take parameters, or if aliases take parameters because there are no functions, or what.)
  • archae0pteryx
    archae0pteryx over 6 years
    So you would call it (from the shell) like gitall "my commit message"? or would you call it gitall('my commit message')
  • Kevin
    Kevin over 6 years
    @archae0pteryx functions are called exactly like any other command, so gitall "my commit message".
  • rococo
    rococo over 6 years
    So hacky and yet so beautiful
  • tripleee
    tripleee over 6 years
    Why make an alias at all? Just call the function example.
  • Charles Duffy
    Charles Duffy over 5 years
    I'd suggest getall() { without the preceding function -- sure, it's legal either way in zsh, but that sole change will make this compatible with all POSIX-compliant shells.
  • Kevin
    Kevin over 5 years
    @CharlesDuffy makes sense, sure.
  • Charles Duffy
    Charles Duffy over 5 years
    BTW, if you used git commit -m "${1:-update}" (a parameter expansion with a default provided), then you wouldn't need the if statement at all.
  • tripleee
    tripleee over 5 years
    Also, belatedly, you need a semicolon before the closing brace.
  • MayTheSForceBeWithYou
    MayTheSForceBeWithYou over 4 years
    This was beautiful. With this I was able to make an alias that adds an alias to an rc file, then reloads said rc file. ❤️ alias addalias='f() { echo "alias" $1 >> ~/.zshrc && . ~/.zshrc };f'
  • maoizm
    maoizm about 4 years
    no need to add any names into global scope, just use anonymous function: alias example='(){ echo Your arg was $1. ;}'
  • gihanchanuka
    gihanchanuka over 3 years
    Tried using the function. Works, but autocomplete is not working as expected. In my case, I want an alias for Git, and after adding function, zsh auto-suggest folder names rather branch names.
  • Jimmy Adaro
    Jimmy Adaro almost 3 years
    @maoizm Great piece of code! I'm using it like this: alias dexec='(){docker exec -it $1 /bin/bash;}'to access a Docker container in 1 command. Thanks!
  • maoizm
    maoizm almost 3 years
    @triplee Quite surprisingly for me Zsh tolerates missing ; before }
  • tripleee
    tripleee almost 3 years
    @maoizm Ah thanks, it escaped me that this was a zsh question; it's weirdly also tagged bash.
  • maoizm
    maoizm almost 3 years
    @tripleee I have removed bash tag as it leads to misunderstanding + zsh definitely treats aliases differently from bash
  • Chris Perry
    Chris Perry almost 3 years
    Note that this must be written with single quotes (as it is in the original example). I was using double quotes at first and it was breaking functionality. Not sure why tho ¯\_(ツ)_/¯
  • Good Pen
    Good Pen over 2 years
    What a clear answer!
  • Good Pen
    Good Pen over 2 years
    +1: Why make an alias at all? Just call the function example. example_func() { echo Your arg was $1 ; }
  • nicolas
    nicolas over 2 years
    is there any benefit of having an alias, or is it just more syntax ?
  • jan
    jan almost 2 years
    @maoizm Your example works, but I don't understand why that anonymous function gets called at all
  • maoizm
    maoizm almost 2 years
    @jan this is by design: anonymous function is invoked immediately at its lexical scope. and there is no sense to store them in shell’s lookup table as they can’t be referred to later. they get discarded immediately after invocation