DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Editing files

Substituting text

To substitute one sequence of characters for another on the current line, use the :s/old/new/ command, where old is the sequence to find, and new is the sequence to replace it with. For example:

:s/needle/bodkin

This changes the first occurrence of ``needle'' when applied to the following line:

With a bare needle? Who would needles bear,

After the substitution it reads as follows:

With a bare bodkin? Who would needles bear,

Pressing & repeats the substitution on the current line:

With a bare bodkin? Who would bodkins bear,

Entering :s/bodkins/fardels/ amends the line to give the correct quotation:

With a bare bodkin? Who would fardels bear,

If you wish to keep the old string as part of the new, you can refer to it as ``&'' within the new string. For example:

Tomorrow and tomorrow,

Applying :s/tomorrow/& and &/ to the above line transforms it into the following:

Tomorrow and tomorrow and tomorrow,

Performing global substitutions

To substitute all occurrences of a sequence of characters within a file, type:

:g/old/s/old/new/g

The first element of this command, :g/old/ is the address. g/ (short for global) indicates that the following action will be applied globally (throughout the file) to all lines containing the string old.

The second element of the command, s/old/new/g, is the action. In this case, new will be substituted for old, globally, on the selected lines.

This command can also be written as follows:

:1,$s/old/new/g

In this form, the address is the range of lines 1,$, where $ is an abbreviation for the last line in the file.

To restrict the change to a line or range of lines, replace the search command /old with the line number or range of line numbers to apply the command to. For example, to carry out the command on lines 5 to 20 of the file, type the following:

:5,20 s/old/new/g

vi remembers the last string it searched for; the empty search command // matches all occurrences of the previous search string. For example, in the following command, the change is applied to lines containing old:

:g/old/s//new/g

Because the target of the substitution is the empty search field, all occurrences of old are replaced (because old was the last item searched for).

You can search for regular expressions as defined in ``Editor regular expressions'', but should replace them with a string of ordinary text. For example, to search for any single word beginning with ``cent'' (such as center, centered, or central) and replace it with ``middle'':

:g/cent[a-z].*/s//middle/g

(Note that the ``.*'', representing a sequences of zero or more characters, is necessary to match the rest of the word. Otherwise the string matched by ``cent[a-z]'' may be replaced by ``middle'' but the suffix of the original word will not be removed by the substitution.)

Using the soliloquy example introduced earlier in this chapter, we can replace all instances of ``Which'' that start a line:

:g/^Which\>/s//That/

The use of the ``\>'' notation ensures that the substitution is applied only to single words. The final g was left out as there can only be one beginning to a line. After performing this command, vi leaves you at the last line on which it performed a substitution:

That patient merit of th' unworthy takes,

You can apply a global substitution to all lines that do not match the search string, by starting the command with :g!/ instead of :g/.

For example, suppose you have a file containing paragraphs of text separated by blank lines, and want to indent each line of text by one tab space. You do not want to add tabs to the blank lines. To do this, you can use the command:

:g!/^$/s/^/<Tab>/g

(<Tab> is a tab character). The regular expression ^$ matches an empty line; that is, a start-of-line metacharacter ``^'' followed immediately by an end-of-line metacharacter ``$''. The :g!/^$/ command matches all lines that don't match this regular expression (that is, all nonempty lines). vi then executes the substitution command s/^/<Tab>/g which searches for the beginning of the line and inserts a tab character.

You might also wish to restrict the lines that are substituted. For example, applying the command :1,$-1 s/^.*$/<Tab>&/g to the file soliloquy indents every nonempty line except the last:

   .
   .
   .
   	And enterprises of great pitch and moment,
   	With this regard their currents turn awry,
   	And lose the name of action. - Soft you now,
   	The fair Ophelia! - Nymph, in thy orisons
   	Be all my sins remembered.
   
   by William Shakespeare
   

When the command finishes, your position in the file is at the last line that was changed.

Specifying addresses

The following types of address are recognized:


.
The current line. (This is the default address for most vi commands.)

57
The fifty-seventh line in the current file.

57,4711
Lines 57 through 4711.

$
The last line in the file.

1,$
Every line in the file.

±5
The fifth line after the current one. (Negative offsets are also permitted; for example, -8 refers to the eighth line before the current one.)

.,+8
The current line to the eighth line after it. (Negative offsets are not permitted in this case.)

/pattern/
Matches the first line after the current that contains the regular expression pattern. See ``Editor regular expressions'' for details of the regular expressions vi recognizes.

To match any single word beginning with some prefix prefix, you should search for the regular expression \<prefix[a-z]*, which matches prefix followed by any sequence of lowercase letters. If you search for \<prefix.* it will match from prefix to the end of the line. This may cause unpredictable results when substituting text.


`marker
Matches the line containing the marker placed using the m command. See ``Placing markers'' for more details.

Confirming substitutions

To make vi prompt you for confirmation before it changes each instance of a string, use the gc command. You might want to do this if you only want to replace some occurrences of a word in a file, rather than all of them. For example, to substitute ``hawk'' for ``handsaw'' throughout a text, prompting for confirmation before each change, enter:

:g/hawk/s//handsaw/gc

The command is broken down as follows:


g/hawk/
Applies the change to every line containing the string ``hawk''.

s//
Makes a substitution; the empty search target // means that vi will carry out substitutions on the previous search string (``hawk'').

handsaw/gc
Replaces the search string with ``handsaw'' everywhere (g), subject to confirmation of each change (c).
In response to this command, vi prompts you at each occurrence of the word ``hawk'', printing the line it occurs on. Enter ``y'' to make the substitution; enter ``n'' to ignore the occurrence and continue the search. (If you enter something which vi does not recognize, it plays it safe and does not carry out the substitution.)

Next topic: Repeating and undoing commands
Previous topic: Replacing and modifying text

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003