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

2

4

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

  • I've moved around when I export or source OhMyZsh and my aliases within .zshrc (citation here), but no difference.

  • I've tried using ZSH_CUSTOM=/Users/StevenChoi/.aliases/.zsh_aliases and creating a plugins folder (I think I followed these directions correctly), but same result.

  • I've added #!/bin/zsh at the top of each file and .zsh at the end of each filename, just to be thorough (not that I expected much).

  • NOTE: I also want to avoid touching .oh-my-zsh/plugins/git/git.plugin.zsh and the .oh-my-zsh directory, unless it's the only way.

  • EDIT: Condensing the commands into one line sacrifices parameter handling. I've been using the solution I've provided for myself for now, but it cannot handle parameters, returning them only as empty string. For a better solution, it must be a multi-line alias / function that both replaces an alias in OhMyZsh and also accepts and handles parameters.

Buskined answered 8/4, 2020 at 20:41 Comment(2)
If I understand correctly, you're trying to override the behaviour of an already-defined alias with a ZSH function doing something different [specifically, handling parameters]. If that's the case, why not just unset the existing alias prior to creating a function with the same name in your ~/.zshrc? Load oh-my-zsh, unset things you want to replace, replace them? See thisDewan
@simont, Thanks! Works perfectly!Buskined
B
5

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.

Buskined answered 8/4, 2020 at 20:41 Comment(1)
Worked, thank you!Woad
F
0

As mentioned in the wiki partially-overriding-an-existing-plugin you can add a custom plugin in the ~/.oh-my-zsh/custom/plugins directory and with the method @simont said, first use unalias then assign the function. Then add the custom plugin in the $plugins variable after the plugin you want to override. For example, add following in the file ~/.oh-my-zsh/custom/plugins/git-custom/git-custom.plugin.zsh:

unalias grs

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

Then add the custom plugin in the .zshrc file: plugins=(git git-custom)

Fretful answered 17/7 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.