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.
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.
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.