vi : replacing text globally (search and replace)
stephane
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.