Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix commandline history substitution ^foo^bar (for multiple replacements)

Tags:

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. 
like image 536
Jesper Rønn-Jensen Avatar asked Feb 17 '10 13:02

Jesper Rønn-Jensen


People also ask

What does ${ foo bar do in Linux?

When you do foo= bar then you are assigning an empty string to the environment variable foo and then execute the command bar . It can be used to pass environment variables to a new execution.

What is foo bar in Unix?

The terms foobar (/ˈfuːbɑːr/), foo, bar, baz, and others are used as metasyntactic variables and placeholder names in computer programming or computer-related documentation.

What is history expansion in Linux?

9.3 History Expansion History expansions introduce words from the history list into the input stream, making it easy to repeat commands, insert the arguments to a previous command into the current input line, or fix errors in previous commands quickly.


2 Answers

man bash says ^old^new is equivalent to !!:s/old/new/. You want !!:gs/old/new/ to globally replace.

like image 82
dubiousjim Avatar answered Sep 23 '22 14:09

dubiousjim


An easy way of doing this would be:

fc -s checkbox=radio 

I have an alias r defined in my .bashrc:

alias r='fc -s' 

Then what you want to do becomes:

r checkbox=radio 

See my answer to "hidden features of bash" question for details.

like image 40
Alok Singhal Avatar answered Sep 21 '22 14:09

Alok Singhal