Writing a whole file or parts of a file – HP NonStop G-Series User Manual

Page 163

Advertising
background image

The vi command : prompts for an ex command. This substitution command is applied to all lines
in the file by the % address. The / (slash) is used as a separator. (Any other character can be used.)
The g flag at the end of the command causes the substitution to occur on each instance of the
pattern within each line. Without the g flag, substitution occurs only once on each line.
You should be careful when making substitutions to ensure that you get what you want. Note that
the previous command line would change “this” to “thwas”.
You can add a c flag along with the g flag to prompt for confirmation before each substitution.
As an example of confirming a substitution, change the word “thwas” back to “this” by issuing the
following command:

:%s/thwas/this/gc<Return>

The following prompt appears at the bottom of the screen:

You can use thwas text file
^^^

Note that the “was” of “thwas” is emphasized as the text to substitute. As shown in the following
example, type y and press Return. You are then prompted for the second substitution:

You can use thwas text file
^^^y<Return>
You can use thwas text file
^^^

Type y and press Return. In response to the Hit return to continue prompt, press Return
once again as follows:

You can use thwas text file
4ª ^^^y
You can use thwas text file
4ª ^^^y<Return>
[Hit return to continue]<Return>

You will find that the two occurrences of the word “thwas” have been changed back to “this”. In
addition, you will also be back in command mode with your cursor at the place of the last
substitution.
Now try another substitution on our example file. Add three lines of new text to the file by using
the $ (go to beginning of last line), o (create new line), yy (yank), and p (paste) commands as
follows:

:$<Return>
o
Some new text with a mispelling. <Esc>
yy
p
p
p

You now should have four lines of new text, all containing the incorrectly spelled word “mispelling”.
To fix the spelling error, enter the following command:

:$-3,$s/mispelling/misspelling/<Return>

The address $-3 indicates the line that is three lines above the last line, and the second address $
indicates the last line. You do not need to use the g flag in this case, because the change is
necessary only once on each line.

Writing a Whole File or Parts of a File

The :wq command is a special ex command that writes the whole file. It combines the features of
the write command w and the quit command q. The only argument that the quit command can
take is the exclamation point (!). It forces the session to quit even if changes made to the file would
be lost by quitting.
The w command can also take addresses and a filename argument, which allow you to save part
of your text to another file. For example, to save the first three lines of your text to the new file
my.new.file

, use the following command:

Using the Underlying ex Commands 163

Advertising
This manual is related to the following products: