bashgitaliaszshoh-my-zsh

OhMyZsh: override Git plugin aliases with custom multi-line aliases / functions


tl;dr - I want to override OhMyZsh's Git aliases with multi-line aliases / functions.

I'm trying to make the switch over from bash to zsh and migrate my aliases. I'm able to override Git aliases from OhMyZsh via this (example):

alias grs="git restore --staged ."

However, when I try to use zsh functions (for aliases that already exist in OhMyZsh):

grs() {
  if [ $# -eq 0 ]
  then
    git restore --staged .
  else
    git restore --staged "$@"
  fi
}

it results in this error:

/Users/StevenChoi/.aliases/.zsh_aliases/.g_aliases.zsh:102: defining function based on alias `grs'
/Users/StevenChoi/.aliases/.zsh_aliases/.g_aliases.zsh:102: parse error near `()'

When I try bash-style functions:

function grs() {

They just get ignored.


Things I've tried


Solution

  • FINAL EDIT: The actual solution is provided by @simont as a link to this question. Please ignore everything below the horizontal rule, which I'm leaving in for the record.

    Simply unset the alias and set it again right after.

    unalias grs
    
    grs() {
      if [ $# -eq 0 ]
      then
        git restore --staged .
      else
        git restore --staged "$@"
      fi
    }
    

    EDIT: This solution DOES NOT handle parameters correctly! While this solution is good enough to get me unblocked, the question is now to find a solution that can both replace an alias in OhMyZsh and also accept and handle parameters.

    Found a solution from here while detailing my question.

    I simply converted all my multi-line functions to single-line aliases (example):

    alias grs="if [ $# -eq 0 ];then;git restore --staged .;else;git restore --staged \"$@\";fi;"
    

    This is one of the uglier things I've seen since migrating to zsh (which is usually nicer-looking), but I think it's worth it. If anyone has a more elegant solution, I'd be happy to hear it.