In the vi text editor, you can replace text in the following ways:
- Replace a single instance of a word:
- Position the cursor on the word you want to replace.
- Type
r
followed by the replacement text.
- Replace all instances of a word in a single line:
- Press the
Esc
key to enter normal mode. - Type
:s/old-word/new-word/g
to replace all instances of “old-word” with “new-word” in the current line.
- Replace all instances of a word in the entire file:
- Press the
Esc
key to enter normal mode. - Type
:%s/old-word/new-word/g
to replace all instances of “old-word” with “new-word” in the entire file.
Note: In the above commands, the g
at the end of the s
command stands for “global”, which means that all instances of the word in the specified range will be replaced, rather than just the first instance. You can omit the g
to only replace the first instance.