Substitute (s)

substitute text

Syntax:

[line_range]s/pattern/replacement_text/

[line_range]snumber/pattern/replacement_text/

Description:

This command replaces occurrences of pattern with the string replacement_text in the line range specified. If no line address is specified, substitutions are made on the current line. The pattern and replacement text are delimited by a single character, usually /. It is general practice to use the / character for this purpose, however, any other character not appearing in pattern or replacement_text may be used instead. When another character occurs in the place of / it will automatically be used as the delimiter character.

The first form replaces all occurrences of the given pattern on a line while the second form only replaces the numberth occurrence on the line.

The “&” character has a special meaning (with option m+) when used in the replacement text. It will be replaced by the text of the pattern matched. For example:

s/however/&,/

is equivalent to:

s/however/however,/

This can be extremely useful when matching complex patterns where you may not know the exact text matched. The “&” is not a special character in pattern, and all the pattern matching meta characters are not special in replacement_text.

The backslash \ character may be used in the definition of pattern and replacement_text to escape the meta characters, “&” and the delimiter character (usually /) so they lose their special meaning. For example:

s/myfile\*/\/yourfile\&/

will replace the string “myfile*” with “/yourfile&”. You can also match and replace your text with exact hexadecimal character values using a \hh sequence. One common use is splitting a line by inserting a record separator character. The command:

s/and further more/&\0a/

will split the current line after the string “and further more”. You can not match a linefeed character in you text buffer as they are not actually stored, but stripped and added when you read and write your buffer to a file.

The @(t) construct can be used to turn runs of spaces into tabs

Mark tab stops with a Ctrl-a:

s/@(t)/\01/

Replace runs of spaces ending on a tab stop with a Tab character:

s/  *\01/\09/

Current line:

Set to the address of the last line of the specified range.

Condition register:

Set TRUE if a successful substitution takes place, otherwise, it is set FALSE.