This appendix only provides an introduction to the features of
vi
.
If you want to learn more, see the
vi
(1)
reference page.
You may also read one of the many books on the market that describe
the advanced features of
vi
.
This appendix is divided into three sections.
The first section gets
you started with
vi
.
The second section shows you some
advanced techniques for speeding up your work.
The third section shows you
how to take advantage of the power of the underlying
ex
commands.
Whether you are writing memos or modifying C programs, editing text
files is one of the most common uses of any computer system.
The
vi
text editor (hereafter known as
vi
) is particularly
well-suited for the day-to-day text editing tasks of most computer users.
You quickly and easily can open a file, edit it, and save the results using
vi
.
The
vi
text editor is a full-featured text editor
with the following major features:
Fast processing, especially on startup and global operations
Full screen editing and scrolling capability
Separate text entry and edit modes
Global substitution and complex editing commands using the
underlying
ex
editor
commands
Access to operating system level commands
Ability to customize system parameters and keyboard mappings
This appendix shows you how to use the basic features of
vi
.
After completing the exercises in this appendix, you will be able
to:
Create and save a new file
Access (open) an existing file
Move the cursor within the file
Enter new text
Change existing text
Search for strings
Move and copy text
Make global substitutions
Write all or part of the text to a file
Delete, move, or copy blocks of text
Customize your editing environment
This section shows you how to create a file with
vi
,
save the file, move the cursor around in the file, add text, delete text,
and modify text.
A.1.1 Creating a File
To create the file,
my.file
, that will be used in
the examples throughout this appendix, enter the
vi
command
as shown below:
$
vi my.file [Return]
Your screen will look like this:
~ ~ ~ ~ ~ ~ ~ ~ ~ "my.file" [New file]
The lines beginning with tildes ( ~ ) represent the blank
lines in the file.
Because
my.file
is empty, all lines
in the file begin with a tilde ( ~ ).
The
vi
editor has two modes:
command mode
and
input mode.
Command mode is the mode
vi
is in when it is started.
In command mode, the characters you
enter are interpreted as commands for manipulating the text.
When
vi
is in input mode, the characters you enter are interpreted as
text.
When you create a new file with the
vi
command, the
vi
editor is in the command mode.
That is,
vi
is waiting for you to enter a command.
However, at this point you want
vi
to be in the input mode so you can insert text into
my.file
, which is empty.
Put
vi
into the input mode by typing:
i
The
i
command will not be displayed on the screen.
The
vi
editor is now in the input mode and
vi
will interpret all characters that you type to be text.
In the sample text below, notice the use of the Escape key on the last
line of input and the use of the
:wq
command to save the
file and exit the
vi
editor.
Type the sample text exactly
as shown.
If you make a mistake, use the Backspace key to correct it; press
the Return key where indicated to move to the next line of text:
You can use this text file [Return]
to experiment with vi. [Return]
The examples shown here [Return]
will teach you the basics of vi. [Escape]
~
~
~
~
~
:wq
"my.file" 4 lines, 108 characters
$
Note
Depending upon how your terminal or workstation is set up, the Escape key may be programmed to perform a different function. It is possible that one of the function keys on your keyboard (possibly F11) may have been set up to perform the escape function. See your system administrator if your Escape key does not operate properly.
Pressing the Escape key while
vi
is in the input
mode puts
vi
back into the command mode; once in the command
mode
vi
interprets anything you type to be a command.
The
:wq
command writes (saves) the file with the name
my.file
into your current directory and quits the
vi
editor.
The format of the
:wq
command is much different
from other
vi
commands because
:wq
is
not a
vi
command; it is an
ex
command.
When you press a colon ( :
) when
vi
is in the command mode, notice that it appears at the bottom of the screen.
The colon ( :
) begins all
ex
commands from within
vi
.
All
ex
commands are executed when
vi
is in the command mode.
You must press the Return key after the
command to signify to
ex
that you are finished entering
the command.
See
Section A.3
to learn more about
ex
commands.
If you lose track of which mode
vi
is in, press the
Escape key a few times to make sure
vi
is in the command
mode.
If your system is so configured, you will hear a bell when you press
the Escape key that signals that
vi
is indeed in the command
mode.
The Escape key and its use in
vi
and exiting
vi
using several different methods are described in more detail
later in this appendix.
The text you just entered in
my.file
will be used
in the remaining examples in this appendix.
A.1.2 Opening an Existing File
Whether you are creating a new file or opening an existing file, the
syntax for using
vi
is the same:
vi
filename
To open the
my.file
file, enter the
vi
command as follows:
$
vi my.file
Your screen should look like this:
You can use this text file to experiment with vi. The examples shown here will teach you the basics of vi. ~ ~ ~ ~ ~ ~ "my.file" 4 lines, 108 characters
The text you entered in the file will be displayed at the top of the screen. The lines beginning with tildes ( ~ ) represent the blank lines in your file. The text at the bottom of the screen shows the name of the file, the number of lines in the file, and the number of characters in the file.
A.1.3 Saving a File and Quitting vi
In the previous example, you learned that the
:wq
command saved the file and quit the
vi
editor.
However,
there are several other options available to save and quit a file:
Save a file and continue working in it
Save the file and quit (exit)
vi
Quit
vi
without saving the changes made
to the file
If you are working on a large text file and have been adding, changing,
and deleting a lot of information, it is suggested to save the file often
(perhaps every 10 minutes) to protect against potential data loss.
The
write
command is used to save an entire file to the current directory.
The format of the
write
command is:
:w
filename
The entry of
filename
is optional
and is used only when you want to save a file under a different file name.
Omitting
filename
from the command automatically
saves a file to it's current file name.
When you enter the
:w
command, the current file name, number of lines, and number of characters
is displayed at the bottom of your screen.
If you entered a new file name,
the new file name will be displayed.
Note
If you specify a new file name with the
:w
command, you will have two files saved in your directory: the new file name you just entered and the original file name.
If you are finished making changes to a file, you can save the file
and quit
vi
at the same time.
The format of the
write
and
quit
command is:
:wq
The
:wq
command saves a file to the same file name,
quits
vi
, and brings you back to your shell prompt.
You also have the option to quit a file and
vi
simultaneously
without saving the changes you may have made.
This option is useful if, for
example, you have deleted many lines of information by mistake and you want
to start all over again.
Quitting
vi
will restore your
file to its original state.
However, quitting
vi
to restore
a file to its original state will only work if you have not saved the file
previously during the current editing session.
To quit your file and
vi
without saving your changes enter:
:q!
Quitting a file with the
:q!
command will not delete
the file from your directory.
Your file will still reside in the directory,
but it will not contain any of the changes you may have made.
Table A-1
summarizes the commands used to save files and
quit the
vi
editor.
Table A-1: Write and Quit Command Summary
Command | Result |
:w |
Saves the entire file to the current file
name; does not exit the
vi
editor. |
:w
filename |
Saves the entire file to the new file name;
does not exit the
vi
editor.
The new file name and original
file name reside in the directory. |
:wq |
Saves the entire file to the current file
name and exits the
vi
editor simultaneously. |
:q! |
Quits the file; exits the
vi
editor; does not save any changes made to the file since the last time the
file was saved. |
If you have closed
my.file
, reopen it by using the
command:
$
vi my.file
The cursor should be on the first character
in the file: the
Y
in the word
You
.
As
mentioned previously in this appendix,
vi
is in command
mode at start up.
In command mode, the characters you enter are treated as
commands rather than as text input to the file.
A.1.4.1 Moving the Cursor Up, Down, Left, and Right
Certain keys on the keyboard have been designated to be
movement
keys when
vi
is in the command mode.
The following letters on the keyboard control cursor movement:
h
(move the cursor one character to the
right)
j
(move the cursor down one line staying
in the same position)
k
(move the cursor up one line staying
in the same position)
l
(move the cursor one character to the
left)
Using the movement keys, move the cursor to the first letter
of the word
experiment
by typing:
lllj
If your keyboard is equipped with arrow keys, you may be able to use
the arrow keys to move left, right, up, or down as well.
However, using the
h
,
j
,
k
, and
l
keys lets you keep your fingers on the main section of the keyboard for faster
typing.
On some keyboards, the
h
,
j
,
k
, and
l
keys are repetitive keys.
That is, holding
the key down will repeat the key action until you release the key.
For instance,
holding down the
j
key will scroll rapidly through the
lines in a file.
In the command mode, the Return key acts as a cursor movement key.
Pressing
the Return key moves the cursor to the first character of the next line.
This
movement differs from the
j
movement key because the Return
key positions the cursor at the first character of the next line whereas the
j
moves the cursor to the same character position on the next line.
In the command mode, the hyphen ( - ) moves the cursor to the first character of the previous
line.
This feature is useful to move backward through files.
This movement
differs from the
k
movement key because the hyphen ( - )
positions the cursor at the first character of the previous line whereas the
k
moves the cursor to the same character position on the previous
line.
If you tested any of the cursor movement keys described above, make
sure your cursor is positioned at the first letter of the word
experiment
before continuing to the next section.
A.1.4.2 Moving the Cursor by Word, Line, Sentence, and Paragraph
You can use the
w
command to move the cursor by whole
word boundaries.
The
w
command moves the cursor forward
to the beginning of the next word.
Move the cursor to the beginning of the
word
with
by typing:
w
You can use the
b
command
to move backward to the beginning of the previous word.
For example, move
to the beginning of the word
experiment
by typing:
b
Now see what happens when you
do not use the
b
command from the beginning of a word by
typing:
llllb
The cursor returns to the beginning of the word
experiment
.
The word motion commands will wrap to the next or previous text line
when appropriate.
Move the cursor to the beginning of the word
text
by typing:
bbb
Notice how the cursor moved backward and wrapped around to the previous line.
There are a few other interesting movement commands you should know about. The zero (0) moves the cursor to the beginning of the current line, and the dollar sign, ($) moves the cursor to the end of the current line.
The close parenthesis [ ) ] moves the cursor to the beginning of the next sentence, and the open parenthesis [ ( ] moves the cursor to the beginning of the previous sentence.
The right brace ( } ) moves
the cursor to the beginning of the
next paragraph, and
the left brace ( { ) moves the cursor to the beginning of the
previous
paragraph.
A.1.4.3 Moving and Scrolling the Cursor Forward and Backward Through a File
In larger files, you can move the cursor by whole screenfuls by pressing certain control keys:
Ctrl/F
moves the cursor one full
screen forward
Ctrl/B
moves the cursor one full
screen backward
Ctrl/D
moves the cursor and scrolls
down (forward) a half screen
Ctrl/U
moves the cursor and scrolls
up (backward) a half screen
The following uppercase letters also designate cursor movement over large boundaries of text:
The
H
command moves
the cursor
Home; that is, to the first character in the
file
The
G
command instructs
the cursor to
Go
to the last line in the file
A.1.4.4 Movement Command Summary
The
vi
text editor has many more cursor movement
commands.
When you have learned the basics documented in this appendix, refer
to the
vi
(1)
reference page for more information.
Table A-2
summarizes the cursor movement commands.
The cursor movement keys are in effect only when
vi
is
in the command mode.
Table A-2: Cursor Movement Command Summary
Command | Result |
h |
Moves the cursor one character to the right. |
j |
Moves the cursor down one line in the same position. |
k |
Moves the cursor up one line in the same position. |
l |
Moves the cursor one character to the left. |
Return key |
Moves the cursor to the beginning of the next line. |
- |
Moves the cursor to the beginning of the previous line. |
w |
Moves the cursor forward to the beginning of the next word. |
b |
Moves the cursor backward to the beginning of the previous word. |
0 |
Moves the cursor to the beginning of the current line. |
$ |
Moves the cursor to the end of the current line. |
) |
Moves the cursor to the beginning of the next sentence. |
( |
Moves the cursor to the beginning of the previous sentence. |
} |
Moves the cursor to the beginning of the next paragraph. |
{ |
Moves the cursor to the beginning of the previous paragraph. |
Ctrl/D |
Scroll down (forward) a half screen. |
Ctrl/F |
Moves the cursor forward one screen. |
Ctrl/B |
Moves the cursor backward one screen. |
Ctrl/U |
Scroll up (backward) a half screen. |
H |
Moves the cursor home (to the first character in the file). |
G |
Moves the cursor to the last line of the file. |
To enter new text into a file,
vi
must be in the
input mode.
In input mode, the characters you enter are inserted as text directly
into the file.
Remember that when
vi
is in the input mode,
you can return
vi
to the command mode by pressing the Escape
key once.
There are several different commands used to insert text, and all of
the commands that are used to insert text automatically place
vi
in the input mode as soon as the command is typed.
To begin this exercise, open
my.file
and make sure
the cursor is positioned at the word
text
in the first
line of the file.
As
you did initially to insert text into
my.file
, you will
use the insert command to insert the word
new
just before
the word
text
.
With the cursor positioned on the first
t
in the word
text
, put
vi
into the input mode command by typing the insert command:
i
Next, enter the word
new
and press the space bar
once:
new [Space]
Exit the input mode by pressing the Escape key:
[Escape]
The cursor should now be positioned on the space between the words
new
and
text
.
The
i
command starts inserting text at the character
just before the cursor.
That is why you have to remember to press the Space
bar to insert a space between words if the cursor was positioned at the first
character in a word when you started to insert text.
Another
command that is used to insert text is the append (a
) command.
In contrast to the insert command, the
a
command adds (or
appends) the characters you type just after the cursor position.
To see how
the
a
command works, use the cursor movement keys to move
to the letter
u
in the word
You
, and
type:
a
, too, [Escape]
The
vi
text editor appended the text you typed to the end of the word
You
.
The cursor should now be positioned on the second comma.
The
o
command opens a new line below
the line with the cursor and lets you insert text at the start of that new
line.
To add a sentence to the end of this file, move the cursor to the last
line of the file by pressing the Return key three times:
[Return] [Return] [Return]
The cursor should be positioned at the word
will
.
To open a new line below the current line and automatically put
vi
into the input mode, type:
o
Enter the sample text shown below (including pressing the Return key where indicated), and press the Escape key to return to command mode when you are finished.
New text can be easily entered [Return]
while in input mode. [Escape]
Your screen should now look like this:
You, too, can use this new text file to experiment with vi. The examples shown here will teach you the basics of vi. New text can be easily entered while in input mode. ~ ~ ~ ~ ~ ~
The
O
command opens
a new line above the current line and starts inserting text at the start of
the new line.
This command is most useful for adding new text to the top of
an existing file, but can be used anywhere in a file.
To practice using this
command to open a line and insert text, move the cursor to the first line
in the file (using the cursor movement command
H
perhaps)
and type:
O
Opening a new line is easy. [Escape]
The
vi
text editor is back in the command mode once
the Escape key is pressed.
There are two other commands that put
vi
in
the input mode: the
I
and
A
commands.
The
I
command inserts text at the beginning of the current
line.
The
A
command appends text after the last character
at the end of the current line.
Practice inserting text to the beginning of a line, by typing:
I
Inserting text is easy. [Space] [Escape]
Practice appending text to the end of a line by typing:
A
Really! [Escape]
Your screen should now look like this:
Inserting a line is easy. Opening a new line is easy. Really! You, too, can use this new text file to experiment with vi. The examples shown here will teach you the basics of vi. New text can be easily entered while in input mode. ~ ~ ~ ~ ~ ~
Table A-3
summarizes the commands used to insert and
append text to a file.
These commands are executed from the command mode and
automatically put
vi
into the input mode.
Table A-3: Text Insertion Command Summary
Command | Result |
i |
Inserts text immediately before the current cursor position. |
a |
Appends text immediately after the current cursor position. |
I |
Inserts text at the beginning of the current line. |
A |
Appends text to the end of the current line. |
o |
Opens a new line directly below the current line. |
O |
Opens a new line directly above the current line. |
Up to this point you only have learned how to add new text to the file,
but what if you need to change some text? The
vi
text editor
provides commands for deleting and changing text.
For example, to remove the
word
easily
, from the sixth line in
my.file
,
move the cursor to the first character of the word and enter:
dw
This command is a combination of the delete command
d
,
and the motion command
w
.
In fact, many
vi
commands can be combined with motion commands to specify the duration of the
action.
The general form of a
vi
command follows:
[number][command]motion
The command entry represents an action command, motion represents a motion command, and number optionally represents the number of times to perform the command. You also can use this general form to move the cursor in larger steps.
To illustrate this concept, move the cursor to the beginning of
my.file
by typing
H
.
Now, to move the cursor
forward four words, enter:
4w
The cursor has moved four entire words and is positioned at the first
letter of the fifth word,
easy
.
Using the general form of commands, you can delete the last five words of this text file. Move the cursor to the beginning of the last line by pressing the Return key several times and enter:
5dw
:w
It takes five words to delete the whole line rather than four because
the period at the end of the line counts as a word.
All punctuation counts
as one word when you are using the delete word command.
As a reminder that
you should save a file often, this example also had you
write
the file (save it) using the
:w
command.
Suppose you only want to delete a portion of a word? The
x
command deletes one character at a time.
To see how this command
works, move the cursor to the letter
s
in the word
examples
.
Press the
x
key once to delete the
letter
s
.
A.1.6.2 Deleting Lines
The
dd
command is a shortcut for deleting whole lines
at a time.
The
dd
command can be used with a number to
delete multiple lines as well.
For example, position the cursor at the sixth
line in the file (at the line beginning with the word
New
)
and type:
2dd
The sixth
and seventh lines (even though the seventh line is empty) of the file are
deleted simultaneously.
The
dd
command can be used without
specifying a number to delete one line at a time.
The
D
command clears the current line of text from the current cursor position to
the end of the line but does not delete the line itself.
If the cursor is
positioned at the beginning of the line, the entire line is cleared.
This
command speeds up your work because you do not have to know how many words
are in the line to be able to delete them (as you would, for example, if you
were using the
dw
command).
This command is useful if you
want to rewrite an entire line.
With the cursor positioned at the beginning
of the line, the
D
command followed by one of the text
insertion commands (i
,
I
,
a
, or
A
) lets you clear the current line of text
and reenter new text with a minimum of keystrokes.
A.1.6.3 Changing Text
The command for changing text,
c
, can be used to
combine the actions of deleting and returning to input mode.
It follows the
same general form as the
d
command.
To change the text
new text
to
almost new demo
, move the cursor
to the first character in the word
new
, and enter the command:
2cw
The text will not disappear immediately.
Instead, a dollar sign ($
) is placed at the end of the change range (the last
t
in
text
), and
vi
is placed
in input mode automatically.
The text you enter will overwrite the existing
text up to the dollar sign and then extend the text range as needed.
Enter
the new text by typing:
almost new demo [Escape]
A.1.6.4 Text Editing Command Summary
As shown in the previous sections, the text editing commands can be
used together with the motion commands to give you more editing power.
The
text editing commands can be combined with a number to change or delete large
blocks of words or lines simultaneously.
Table A-4
summarizes
the commands used to edit text.
Table A-4: Text Editing Command Summary
Command | Result |
cw |
Changes the current word to the new text you type. You may change the word with as much new text as necessary. The Escape key signals the end of the change. |
ncw |
Changes n number of words to the new text you type. The new text is not limited to just n words. You may change n words with as much new text as necessary. The Escape key signals the end of the change. |
D |
Clears the text from the current cursor position to the end of the line. Does not delete the space used by the line thereby letting you add more text. |
dd |
Deletes the current line. |
ndd |
Deletes n number of lines beginning with the current line. |
dw |
Deletes the current word. |
ndw |
Deletes n number of words beginning with the current word. |
x |
Deletes the current character. |
If you make a change and then realize it was in error, you still may
be able to correct it if you have not executed another command.
The
u
command undoes the last command entered.
Undo the last command,
2cw
, by typing:
u
The text string
almost new demo
will be changed back to
new text
if you did not execute any other commands since you executed
the
2cw
command.
The uppercase
U
command undoes all changes to the
current line and restores the line to its original state.
The
U
command works only if you have not moved the cursor to another line.
A.1.8 Finishing Your Edit Session
After you finish the exercises in this appendix, you should save the
file and quit
vi
.
To save your changes and quit
vi
, enter:
:wq [Return]
If you want to quit
vi
without saving your changes,
you can do so by entering:
:q! [Return]
You have now learned enough about
vi
to edit any
file.
The following sections show you some advanced techniques that can improve
your productivity and let you customize your environment.
A.2 Using Advanced Techniques
This section shows you how to search for text strings, move text, and
copy and paste text.
As you work with larger documents, all these tasks increase
your ability to work efficiently.
A.2.1 Searching for Strings
In a large document, searching for a particular text string can be very
time consuming.
The slash ( / ) command is used to search for a string.
When you enter
the slash ( / ),
you are prompted for a text string as the target of the search.
When you press
the Return key,
vi
searches the file for the first occurrence
of the text string you entered.
If you do not have it open, reopen the
my.file
file.
Move to the top of the document using one of the cursor movement keys you
learned earlier in this appendix.
To search for the text string
th
, enter the following:
/th [Return]
As soon as you enter the slash ( / ) command, the slash ( / ) is displayed at the bottom of the screen (similar to the
way in which the colon ( : ) works).
When you entered the text string
th
,
it was echoed (displayed) at the bottom of the screen.
You can use the Backspace
key to fix mistakes when you enter the search string.
After you press the Return key, the cursor moves to
the first occurrence of the string (the
th
in the word
this
).
The
n
(next) command continues the search
for the next occurrence of the last string you searched for.
Try it now by
entering:
n
The cursor should move to the next occurrence of the string, which is
the
th
in the word
with
.
Similarly, the
N
command searches for the next occurrence
of the search string, but it searches in the opposite direction of the
n
command.
The
N
finds the previous occurence
of the string.
The question mark ( ?
) command is also used to initiate
a search for text strings, but the question mark ( ?
) initiates a backward search
through the file.
When you search backward, the
n
command
moves the cursor backward to the previous occurrence of the string, and the
N
command moves the cursor forward (exactly the opposite of the
way in which they work with a slash ( / ) search).
A.2.2 Deleting and Moving Text
To move a block of text, you must first select the text to move.
You
already know how to do this.
The delete (d
) command not
only deletes a line of text but also copies it to a paste buffer.
Once in
the paste buffer, the text can be moved (or pasted) by repositioning the cursor
and then using the lowercase
p
command to paste the text
on the line after the current cursor position.
Move the cursor to the first line in the file and type:
dd
The line is deleted and copied into the paste buffer, and the cursor is located on the next line in the file. To paste the line in the buffer back into the file, after the line on which the cursor is positioned, enter:
p
The uppercase letter
P
(Paste) command is used to
paste text on the line above the cursor rather than below it.
If you delete a letter or block of words, the deleted text will be pasted
into the new position within the current line.
For example, to move the word
can
to just before the word
with
, use the following
command sequence (remember to use an uppercase
P
):
/ can [Return]
dw
/ with [Return]
P
You copy text in the same manner as you move it, except that instead
of using the delete text command
d
, you use the yank text
command,
y
.
The
y
command copies (or
yanks) the specified text into the paste buffer without deleting it from the
text.
It follows the same syntax as the
d
command.
You
can also use the
yy
command to yank an entire text line
into the paste buffer, in the same way as
dd
.
For example, to copy the first two lines of the file to a position immediately underneath them, enter the following command sequence from the first line of the file:
2yy
j
p
You must move the cursor down one line using
j
or
the two lines will be pasted after the first line rather than after the second.
A.2.4 Other vi Features
You may want to try some of the other features of
vi
.
The
vi
(1)
reference page lists all of the available commands.
You may
want to pay particular attention to the following:
J
Joins the next line of text to the current line of text.
.
Repeats the last command.
s
Substitutes the current character with the following entered text.
x
Deletes the current character.
~
Changes the alphabetic case of the current character.
!
Executes an operating system command on the current line of text and replaces the text with the output.
Ctrl/L
Refreshes the screen when problems with the screen display occur. Any time your screen is displaying confusing output, press Ctrl/L.
A.3 Using the Underlying ex Commands
The
vi
text editor is based upon the
ex
line editor.
The underlying
ex
line editor can
bring the power of global changes to your entire text file or any large piece
of it.
You can access
ex
commands from within
vi
by using the colon ( :
) command.
You were introduced
to
ex
commands earlier in this appendix with the
:wq
and
:q!
commands for writing and quitting
an editing session.
The colon ( :
) command causes
ex
to prompt for a command
line at the bottom of the editor screen with a colon ( :
).
Each
ex
command is ended by pressing the Return key.
You
can also enter
ex
more permanently with the
vi
command
Q
.
This command turns processing over
to
ex
until you explicitly return to
vi
.
This often happens accidentally.
If it should happen to you, you can return
to
vi
by typing
vi
at the colon ( :
)
prompt followed by the Return key as follows:
:vi [Return]
An
ex
command acts on a block of lines in your text
file according to the following general syntax:
:
[ address
[,address
]
]
command
The command, along with any of its arguments, acts on the lines between and including the first and second address. If one address is specified, the command acts only on the specified line. If no address is specified, the command acts only on the current line. Addresses can be specified in a number of ways. Some of the more common address specifications are the following:
line number
Address by absolute line number.
pattern
/Next line that contains the pattern.
Line that the cursor is on.
Last line of the file.
address
±lines
Relative offset from the addressed line.
All the lines in the file, and is used once in place of both addresses.
The following sections show some of the most generally useful
ex
commands, and some of the customization features offered by
ex
.
You should read the
ex
(1)
reference page for a more detailed
list of commands.
A.3.1 Making Substitutions
The most common substitution task, possibly the most common
ex
task, is a global substitution of one word or phrase for another.
You can do this with the
s
command.
If you have closed
the
my.file
file, reopen it.
To change every occurrence
of "is" to "was", use the following command:
:%s/is/was/g [Return]
This substitution command is applied to all lines in the file by the
%
address.
The slash ( / ) is used as a separator.
The
g
argument
at the end of the command causes the substitution to occur globally, that
is, on each instance of the pattern within each line.
Without the
g
argument, substitution occurs only once on each line.
You should be careful when making substitutions to ensure that you get
what you want.
In the previous command line, the word
this
has changed to
thwas
because every occurrence of
is
was changed to
was
.
You can add a
c
argument along with the
g
argument to prompt for confirmation before each substitution.
The format of the confirmation is a bit complex; however, it is well worth
using when you want to be scrupulous about making global changes.
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, too, use thwas new text file ^^^^^
As shown in the next example, type
y
and press the
Return key.
You are then prompted for the second substitution:
You, too, use thwas new text file
^^^^^
y [Return]
You, too, use thwas new text file
^^^^^
Type
y
and press the Return key, and in response to the
Hit
return to continue
prompt, press the Return key once again as follows:
You, too, use thwas new text file
^^^^^
y
You, too, use thwas new text file
^^^^^
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,
vi
is back in the command mode
with the cursor positioned at the first character of the line with the last
substitution.
Now try another substitution on your 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]
oSome new text with a mispelling. [Escape]
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 one of the following commands:
:1,$s/mispelling/misspelling/ [Return]
or
:5,8s/mispelling/misspelling/ [Return]
In the first example, the address
1,$
indicates that
the substitution should begin on line one (1) and end at the last line of
the file ($).
In the second example,
5,8
indicates that
the substitution should being on line five and end on line eight.
You do not
need to use the
g
operator in either case because the change
is only necessary once on each line.
A.3.2 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 file
name argument, which lets you 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:
:1,3w my.new.file [Return]
"my.new.file" [New file] 3 lines, 130 characters
A.3.3 Deleting a Block of Text
The delete command in
ex
is
d
,
just as in
vi
.
To delete from the current line to the end
of the file, use the following command:
:.,$d [Return]
A.3.4 Customizing Your Environment
The
ex
editor provides two mechanisms for customizing
your
vi
environment.
You can use the
:set
command to set environment variables, and the
:map
command
to map a key sequence to a
vi
command key.
Environment variables are set either by assigning them as
option
or
no option
for Boolean variables,
or by assigning them as
option=
value.
The full set of environment variables is described in
the
ex
(1)
reference page.
Table A-5
lists some common
variables.
Table A-5: Selected vi Environment Variables
Variable | Description |
errorbells |
Specifies that when an error is made, a bell sounds. This is the default setting. |
ignorecase |
Specifies that when performing searches,
the case of characters should be ignored.
The default variable setting is
noignorecase . |
number |
Specifies that line numbers are to be displayed
at the left margin.
The default variable setting is
nonumber . |
showmatch |
Specifies that when you enter a matching
parenthesis or brace, the cursor moves to the matching character and then
returns.
The default variable setting is
noshowmatch . |
tabstop |
Specifies the amount of space between tab
stops.
The default setting is
tabstop=8 . |
wrapscan |
Specifies that searches should wrap around
the beginning or end of the file.
The default variable setting is
wrapscan . |
wrapmargin |
Creates an automatic right margin located
a specified number of characters from the right side of your screen.
Whenever
your cursor reaches the specified right margin, an automatic new line is generated,
and the word you are typing is brought to the next line.
The default setting
is
You should set the
|
To display the line numbers of your example file enter the following command:
:set number [Return]
To remove the line numbers, enter the following command:
:set nonumber [Return]
The
:map
command sets a single
vi
command key to
a
vi
command sequence.
The syntax for the
:map
command follows:
:map
key sequence [Return]
This command sequence replaces any existing command for
that key.
The command sequence should be identical to the keystrokes you want
to map, except that special keys such as the Return key, the Escape key, and
keys modified with the Ctrl key must be quoted first with Ctrl/V.
Because
the
q
and
v
keys do not have commands
associated with them, they are good keys to map.
For example, to map a key sequence that inserts a line into your text that says "This space held for new text", you could use the following command:
:map q oThis space held for new text [Ctrl/V] [Escape] [Return]
Note the use of Ctrl/V to quote the Escape character.
A.3.5 Saving Your Customizations
You can make your environment customizations permanent by placing the
appropriate
ex
commands in a file named
.exrc
in your home directory.
Commands in this file will take effect
every time you enter
vi
or
ex
.
In this
file, you do not need to use the
vi
command
:
, because these commands are read directly by the underlying
ex
editor.
For example, to customize your environment to always display line numbers
for your files, to use the map sequence shown in the previous section, and
to set an automatic right margin of five spaces, you would first open the
.exrc
file with
vi
in your home directory, and
add the following lines of text:
set number
map q oThis space held for new text [Ctrl/V] [Escape]
set wrapmargin=5
After you write this file, verify that it works by opening your example file.