The Computer Oracle

Bash: Replace all occurrences of a word in the last command

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping

--

Chapters
00:00 Bash: Replace All Occurrences Of A Word In The Last Command
00:35 Accepted Answer Score 22
01:02 Answer 2 Score 9
01:31 Thank you

--

Full question
https://superuser.com/questions/255281/b...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #commandline #bash

#avk47



ACCEPTED ANSWER

Score 22


I think ^data^index is equivalent to !!:s/data/index, so it will only substitute the first word. If you want the whole line substituted, I think you'll have to use !!:gs/data/index/




ANSWER 2

Score 9


You can do it by adding ^:& to the end.
^:& will replace two occurrences
^:g& will replace all

$ cp foo.data bar.data
$ ^data^index^:&
$ cp foo.index bar.index
$ cp foo.data bar.data joe.data doe.data
$ ^data^index^:g&
$ cp foo.index bar.index joe.index doe.index

sidenote: in the book 'command line kungfu'
it says that ^:& will replace all