If you want to replace a text throughout a file in the vi text editor, you can use the following command :
:1,$s/text/replacement/g
Here is the breaking down of this command:
- “:” : places you in “ex mode”
- 1,$ is a range specification, meaning from the first to the last line (this can be shortened as % in some versions of VI (vim does, for instance) — Thanks Brandon !
- s means “substitute”
- /text/replacement/ means to replace the “text” pattern by “replacement”
- g : means globally, which will replace all the occurrences of the pattern instead of the first of each lines
If you want to see how to script a text replacement, check out my previous post about text replacement with sed.
Most versions of vi will accept % as a synonym for 1,$.
Brandon,
Thanks for visiting and for this comment; I’ll add a note in the post.
Stephane
I should bookmark this post, I can never remember the syntax, even if it’s sed syntax !