2010-02-17 67 views
23

偶爾我用的是bash命令在以前的命令來替換字符串:的Unix命令行歷史替換^ FOO ^條(多替換)

^foo^bar 

今天我想作以下行更換更換所有出現的checkbox'收音機:

$ 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. 

回答

32

man bash^old^new相當於!!:s/old/new/。你想要!!:gs/old/new/全局替換。

+0

非常感謝! 在這一點上,bash命令突然顯得非常難看,不可讀。但是你可以做的很棒! – 2010-02-17 13:56:42

+0

我同意!那絕對屬於最後! – dubiousjim 2010-02-17 13:58:44

+1

是的,我很確定你想!!:s/old/new/g,所以在原始提問者的情況下,你想!!:s/checkbox/radio/g – Jim 2010-02-17 17:29:52

13

這樣做的一個簡單的方法是:

fc -s checkbox=radio 

我在.bashrc定義的別名r

alias r='fc -s' 

然後,你想做什麼就變成了:

r checkbox=radio 

參見my answer至「hidden features of bash「問題的細節。

+0

這應該是**答案,因爲它很容易記住,否則每次我需要這樣做時都會有谷歌。 – xpt 2016-11-02 16:08:42