Mas*_*son 119 command-line shell bash command-history
我最喜欢的 Unix 技巧之一是^x^y,它将采用最后一个命令并将“x”的第一个实例替换为“y”。但是,我想知道是否有类似的技巧可以在最后一个命令中用“y”替换“x”的所有实例?
slm*_*slm 128
您可以使用该!!:gs/search/replace/符号来做您想做的事。这利用了全局搜索和替换 ( :gs):
前
$ echo "harm warm swarm barm"
harm warm swarm barm
Run Code Online (Sandbox Code Playgroud)
后
$ !!:gs/arm/orn/
echo "horn worn sworn born"
horn worn sworn born
Run Code Online (Sandbox Code Playgroud)
Mik*_*kel 60
我不相信有一个简单的方法来添加的东西^string1^string2,使bash更换所有出现。正如 slm 指出的那样,您必须编写!!:gs/string1/string1.
但是zsh,您可以添加:G:
$ echo foo foo
foo foo
$ ^foo^bar^:G
echo bar bar
bar bar
Run Code Online (Sandbox Code Playgroud)
在bash和 中zsh,您还可以fc -s这样使用:
$ echo foo foo
foo foo
$ fc -s foo=bar
echo bar bar
bar bar
Run Code Online (Sandbox Code Playgroud)
这通常被称为别名,r因此您可以这样做:
$ echo foo foo
foo foo
$ r foo=bar
echo bar bar
bar bar
Run Code Online (Sandbox Code Playgroud)
我相信最好的选择是使用“:&”
\n$ echo "dog cat dog"\n$ ^dog^cat^:&\necho "cat cat cat"\ncat cat cat\nRun Code Online (Sandbox Code Playgroud)\n但就像下面 St\xc3\xa9phane Chazelas 评论的那样,这仅替换了 2 次出现。如果您有更多,则需要添加更多:&
\n| 归档时间: |
|
| 查看次数: |
26574 次 |
| 最近记录: |