Zsh 函数使用替换重复最后一个命令

Zsh 函数使用替换重复最后一个命令,zsh,Zsh,我通常使用 !!:gs/Change/ChangeTo 为了简化,我创建了一个函数 funciton re() { !!:gs/$1/$2 } 现在输出如下 [~/Desktop]$ print -P '\033[34mThis is the same color as in your solarized palette\033[0m' This is the same color as in your solarized palette [~/Desktop]$ !!:gs/34/

我通常使用

!!:gs/Change/ChangeTo
为了简化,我创建了一个函数

funciton re() {
    !!:gs/$1/$2
}
现在输出如下

[~/Desktop]$ print -P '\033[34mThis is the same color as in your solarized palette\033[0m'
This is the same color as in your solarized palette
[~/Desktop]$ !!:gs/34/35                                                                
[~/Desktop]$ print -P '\033[35mThis is the same color as in your solarized palette\033[0m'
This is the same color as in your solarized palette
[~/Desktop]$ re 35 36                                                                   
re:1: no such file or directory: !!:gs/35/36
[~/Desktop]✕127$
因此,它给出了错误
re:1:没有这样的文件或目录:!!:gs/35/36
当我调用函数时

我也试过了

funciton re() {
    ^$1^$2^:G
}
它说找不到命令

[~/Desktop]$ re 35 36
^35^36^:G: command not found
[~/Desktop]✕127$ 

这里的解决方案是什么?

您需要使用
fc
命令。经过一些试验:

re() { fc -e - "$1=$2"; }
然后:

$echo foo bar
富吧
$re bar qux
回音福曲
福曲

如果这对您很重要,我不确定如何禁止打印已编辑的命令。

历史引用(如果适用)将在输入包含它们的行后立即展开。如果在命令提示下定义函数,那么在您完成函数定义之前,您应该会在输入该行时立即收到错误的替换错误(或某些影响)。如果您是从一个文件中进行寻源,那么历史扩展似乎被抑制(尽管我找不到该断言的良好参考)。