String Substitution
The Substitute command is a combination of a
search command and a
change command. The operator for the
Substitute command is [s]. It searches for a string, and when it finds
it, it replaces it with the string you specify. The syntax of the
Substitute command is:
- :[address]s/search-string/replace-string[/g]
If you do not specify an address, the Substitute command will only
search the current line. The "g" indicates that you wish to make this
substitution globally; i.e., each occurrence of the search-string on a
line will be replaced by the replace-string (as opposed to just the
first occurence).
The Substitute Address
The following characters are used to compose an address:
- n
- Line number n will be searched
- n; m
- specifies a range of line numbers (n through m) to be used in the search. The range is inclusive.
- .
- represents the current line
- $
- represents the last line of the work buffer
- 1,$
- represents the entire work buffer
Examples of String Substitution
- :s/larger/largest
- replaces the string "larger" on the current line with the string "largest".
- :1,.s/Section/Chapter/g
- replaces every occurrence of the string "Section" with the string "Chapter" from line 1 in the file through the current line. The "/g" indicates that "Section" should be replaced by "Chapter" every time on a line.
Click here to go to the top of
the chapter.