Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global text insertion using vi 4

Status
Not open for further replies.

rik1551

IS-IT--Management
Sep 14, 2001
10
GB
Hello people!
Can anybody please tell me how you can globally insert words into a text file using vi??
I need to be able to insert the words 'echo dummy' between 2 words on many lines of a text file.
Any help would be very much appreciated.
Thanks
 
How about

:%s/\(word1\) \(word2\)/\1 echo dummy \2/g

Hope this helps. If it is not exactly what you want, post some of your input data and what you want the output to be.

CaKiwi
 
this is what I use

shift /
1,$ s/expression1/expression2/g


-Danny
techie@snoboarder.net






 
The % is a shortcut for 1,$ for the range.

CaKiwi's response shows how the words from expression1 are retained with the additional text. In this case, you can just include them in expression2, but the other method -- using the \( and \) -- allows you to preserve selections found with wildcards. (In case you don't know, the \n in the replacement text indicates the nth block from the find text.) Ex:

%s/\(word[1-3]\) \(word[2468]\)/\1 echo dummy \2/g

This would make the following changes...

From "word2 word2" to "word2 echo dummy word2"
and "word3 word8" to "word3 echo dummy word8"

Like CaKiwi, I'm assuming that you want to insert the new text between two words that are constant throughout the file, but if not, the above may help. If this is not enough, please post a more detailed description of the sort of thing you're trying to do.

Good luck!

- Steve StevieW85@go.com
 
You're right, Steve. I started to write a solution like your improvement, but decided that it was not what rik1551 asked, but then left it overly complicated. Nice description of how buffers and [] work.

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top