vi Global Replacement
Linux Commands - Shells

The ex editor checks each line for a specific pattern.  Once it finds the pattern it will replace it with the new string of characters.  Global replacement uses two ex commands:
:g    (global)
:s    (substitute)

 

Lesson 5 | Lesson 7



The substitute command in this example is used to substitute “oldtext” with the “newtext” in the first occurrence of the line.

:s/oldtext/newtext/

The / is a delimiter between the oldtext and newtext.

If you wanted to change every occurrence of oldtext in the line you would use this command:

:s/oldtext/newtext/g

You can extend the range of lines to change the oldtext to newtext with a range of page addresses.  The example changes every instance of oldtext to newtext from lines 25 to line 175.

:25,175s/oldtext/newtext/g

If you wanted to change every instance in the entire file you could use either one of these commands.

:1,$s/oldtext/newtext/g