Ubuntu – Find and replace while typing command

bashcommand line

I have typed out a long command in the BASH but just as I typed the command I see that I have misspelled the command or argument. I want the cursor to go to the argument or command I have misspelled using inplace search instead of hitting arrow keys to go to the particular command or argument.

E.g.

ls *.txt | grep -e 'foo' >> list_of_text_files_containing_foo.txt

and I want to change foo in grep to bar by find and replace foo to bar inside the command without hitting arrow keys. Is there a way to change all occurrences of foo to bar.

Best Answer

You probably are interested about bash quick substitution in the last command:

!!:gs/old/new

Example:

$ ls *.txt | grep -e 'foo' >> list_of_text_files_containing_foo.txt
$ !!:gs/foo/bar
ls *.txt | grep -e 'bar' >> list_of_text_files_containing_bar.txt

For more info about bash substitution, read man bash starting from somewhere from the line 3630.

Related Question