z/OS UNIX System Services User's Guide
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Making substitutions

z/OS UNIX System Services User's Guide
SA23-2279-00

If the name of a data object or function has to be changed in a program (for whatever reason), it becomes necessary to change every occurrence of that name. This would be a tedious process using the vi features we have discussed up to this point, because you would have to search through each source file for the name and then type in the new name wherever the old one was found. To avoid much of this work, vi offers the substitute command.

The usual form of the substitute command is
:s/pattern/replacement/
where pattern is any of the patterns used in searches, and replacement is any string.

As soon as you type the colon (:), you see the cursor move to the bottom of the screen. Then type the rest of the command and press <Enter>. The command puts the given replacement string in the place of the first string that matches the given pattern.

What happens if a line has more than one string that matches the pattern? The s command replaces only the first occurrence of a given string on a line. The position of the cursor in the line does not matter.

If you want to change every occurrence of a string on a line, type a g (for global) after the last slash.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014