OhMyZsh:使用自定义多行别名/函数覆盖Git插件别名 [英] OhMyZsh: override Git plugin aliases with custom multi-line aliases / functions

查看:332
本文介绍了OhMyZsh:使用自定义多行别名/函数覆盖Git插件别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl; dr-我想用多行别名/功能覆盖 OhMyZsh 的Git别名。

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

我正在尝试从 bash 切换到 zsh 并迁移我的别名。我可以通过以下示例从OhMyZsh覆盖Git别名:

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

但是,当我尝试使用 zsh 函数(对于OhMyZsh中已经存在的别名):

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
}

它将导致此错误:

/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 `()'

当我尝试 bash 样式的函数时:

When I try bash-style functions:

function grs() {

他们只是被忽略了。


  • 当我导出或导出OhMyZsh和 .zshrc 中的别名时,我已经走动了(引用此处),但没有区别。

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

我尝试使用 ZSH_CUSTOM = / Users / StevenChoi / .aliases / .zsh_aliases 并创建插件文件夹(我想我遵循了这些指示正确),但结果相同。

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.

我在每个文件的顶部添加了#!/ bin / zsh ,并在 .zsh 每个文件名的末尾,只是为了透彻(不是我期望太多)。

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

注意:我也想避免触摸 .oh-my-zsh / plugins / git / git.plugin.zsh .oh-my-zsh

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 :将命令压缩为一行会牺牲参数处理。我一直在使用自己为自己提供的解决方案,但是它无法处理参数,只能将它们作为空字符串返回。为了获得更好的解决方案,它必须是多行别名/函数,该函数既可以替换OhMyZsh中的别名,也可以接受并处理参数。

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.

推荐答案

最终编辑:实际解决方案由@simont提供,作为指向此问题。请忽略水平法线以下的所有内容,我将留作记录。

FINAL 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
}






编辑:此解决方案无法正确处理参数!虽然此解决方案足以使我不受阻碍,但现在的问题是找到一个既可以替换OhMyZsh中的别名又可以接受并处理参数的解决方案。

在详细说明我的问题的同时,从此处找到了解决方案。

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;"

这是自迁移到 zsh以来我见过的丑陋事物之一(通常看起来更好),但我认为这是值得的。如果有人有更好的解决方案,我很乐意听到。

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.

这篇关于OhMyZsh:使用自定义多行别名/函数覆盖Git插件别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆