If you want to replace a text throughout a file in the vi text editor, you can use the following command :
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.
3 thoughts on “vi : replacing text globally (search and replace)”
Most versions of vi will accept % as a synonym for 1,$.
Thanks for visiting and for this comment; I’ll add a note in the post.
I should bookmark this post, I can never remember the syntax, even if it’s sed syntax !
Comments are closed.