bash Unix 命令行历史替换 ^foo^bar(用于多次替换)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2280778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unix commandline history substitution ^foo^bar (for multiple replacements)
提问by Jesper R?nn-Jensen
Occasionally I use the bash command to replace string in previous command:
有时我会使用 bash 命令替换之前命令中的字符串:
^foo^bar
Today I wanted to make the replacement in the following line for replacing all occurrences of checkboxwith `radio:
今天,我想在以下行中进行替换,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
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.
回答by dubiousjim
man bashsays ^old^newis equivalent to !!:s/old/new/. You want !!:gs/old/new/to globally replace.
man bash说^old^new相当于!!:s/old/new/。你想!!:gs/old/new/全局替换。
回答by Alok Singhal
An easy way of doing this would be:
一个简单的方法是:
fc -s checkbox=radio
I have an alias rdefined in my .bashrc:
我r在我的中定义了一个别名.bashrc:
alias r='fc -s'
Then what you want to do becomes:
那么你想做的就变成了:
r checkbox=radio
See my answerto "hidden features of bash" question for details.
有关详细信息,请参阅我对“ bash 的隐藏功能”问题的回答。

