Unix 命令行历史替换 ^foo^bar (用于多个替换)
偶尔我会使用 bash 命令来替换上一个命令中的字符串:
^foo^bar
今天我想在以下行中进行替换,将所有出现的 checkbox
替换为 `radio:
$ git mv _product_checkbox_buttons.html.erb _product_checkbox_button.html.erb
$ ^checkbox^radio
git mv _product_radio_buttons.html.erb _product_checkbox_button.html.erb
所以它只替换第一次出现的情况。如何让它替换所有出现的情况?
bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
Copyright (C) 2007 Free Software Foundation, Inc.
Occasionally I use the bash command to replace string in previous command:
^foo^bar
Today I wanted to make the replacement in the following line for replacing all occurrences of checkbox
with `radio:
$ git mv _product_checkbox_buttons.html.erb _product_checkbox_button.html.erb
$ ^checkbox^radio
git mv _product_radio_buttons.html.erb _product_checkbox_button.html.erb
So it only replaces the first occurrence. How do I make it replace all occurrences?
bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
Copyright (C) 2007 Free Software Foundation, Inc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
man bash
说^old^new
相当于!!:s/old/new/
。您希望!!:gs/old/new/
进行全局替换。man bash
says^old^new
is equivalent to!!:s/old/new/
. You want!!:gs/old/new/
to globally replace.一个简单的方法是:
我在
.bashrc
中定义了一个别名r
:然后您想要做的就是:
参见 我对“隐藏的答案 bash 的功能”问题以了解详细信息。
An easy way of doing this would be:
I have an alias
r
defined in my.bashrc
:Then what you want to do becomes:
See my answer to "hidden features of bash" question for details.