使用替换重复上一个命令的函数

Function To Repeat The Last Command With a Substitution

我通常会使用

更改上一个命令的一部分
!!:gs/Change/ChangeTo

为了简化我创建了一个函数

funciton re() {
    !!:gs//
}

现在输出如下

[~/Desktop]$ print -P '3[34mThis is the same color as in your solarized palette3[0m'
This is the same color as in your solarized palette
[~/Desktop]$ !!:gs/34/35                                                                
[~/Desktop]$ print -P '3[35mThis is the same color as in your solarized palette3[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: no such file or directory: !!:gs/35/36

我也试过了

funciton re() {
    ^^^:G
}

它说找不到命令

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

这里的解决方案可能是什么?

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

re() { fc -e - "="; }

然后:

$ echo foo bar
foo bar
$ re bar qux
echo foo qux
foo qux

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