Professional Documents
Culture Documents
THE vi EDITOR
● vi stands for visual editor. This editor is started by using the command vi at the
shell prompt.
● The vi editor works in three modes—the command mode, the input mode and
the ex mode. The relationship between these three modes is shown in Fig. 5.1.
● The input mode: This mode is also known as the input text mode or the
insert mode. As already mentioned in the previous sub-section the user can
get into this mode by using anyone of the following commands:
i, I, a, A, o, O, r, R, s, S
This mode permits the insertion of new text, appending to the existing text
and replacement of text. At any time, the user can go back to the command
mode by pressing the Esc key. Pressing the Esc key when in the command
mode does not cause any unfavourable outcome, it just beeps indicating
that it is already in the command mode.
● The ex mode This mode is also known as the last-line mode and
allows the user to use the commands in the bottom line on the vi screen.
In fact, the bottom line (the last line) of the vi screen is called the
command line. From the command mode the user can get into the ex
mode by typing the colon character (:). The chief advantage of this mode
is that the commands entered in this mode are displayed in the
command line, the last line. A colon (:) prompt in the last line indicates
that the editor is now in the ex mode. Anything entered in front of the
colon (:) prompt is taken as an ex command. The user can get back into
the command mode by using the Enter key.
EDITING WITH vi
● A vi editor can be invoked in any one of the following ways. It is invoked to
create a new file by giving the vi command without any argument as follows.
$vi
A blank screen with (1) the cursor on the left-top corner on the screen (2) a message
of the form new in the last line and, (3) tilde characters (~) in the beginning of all
the other lines
● The second method of invoking the vi is to create a new file with a name, say
malnad. This is done by giving the vi command with the file name as its
argument as follows.
$vi malnad
The behavior will be exactly similar to the first method except that in this case
the message on the last line will be malnad[New File].
Quitting the vi Editor
● When editing is done with the vi editor, the actual editing takes place on a
buffer called the editor buffer. In other words, the editing is not done directly
on the disk file.
● Sometimes the user may not like to save the changes made and may quit
without saving the changes. In any case, one or more of the following
commands are used to quit the vi environment and go back to the shell.
1. ZZ :This command saves the file with the changes made, quits the vi
environment and gets into the shell environment directly from the command
mode.
2. :w :This command just saves or writes the buffer contents on to the disk file
and continues to remain in the command mode.
3. :x :This command saves the file with the changes made and quits or exits
the vi environment.
4. :wq: This command is known as the write and quit command. It behaves
exactly like :x.
5. :q :This command quits the vi environment if the changes made in the
editor are already written. If changes made are not written or saved it will give
a suitable message.
6. :q! : This command quits the vi environment without saving the buffer
contents even if certain changes were made.
EDITING
Text Insertion
● The i Command This command invokes the insertion mode. Whatever
is typed will be inserted to the left of the present cursor position.
● The I Command This command also invokes the insertion mode.
Whatever is typed will be inserted at the beginning of the current line.
● The a Command This command invokes the append mode. Whatever is
typed will be inserted to the right of the present cursor position.
● The A Command This command also invokes the append mode.
Whatever is typed will be appended at the end of the current line.
● The o Command This command invokes the insertion mode by opening
a blank line below the current line. Whatever is typed gets into this blank line.
● The O (uppercase) Command This command invokes the insertion mode
by opening a blank line above the current line. Whatever is typed gets into this
blank line.
Text Replacement
The r Command Using this command only one character at the current cursor
position can be replaced. When this command is given, the editor momentarily gets
into the input mode, accepts the new character, replaces the character at the cursor
position by the new character and comes back to the command mode.
The R Command This command invokes the input mode for the replacement
of more than one character from the current cursor position only on the current line.
Actually the input text is overwritten on the current line.
Text Substitution
The s Command This command invokes the input mode for the substitution of
any amount of text at the cost of the character at the current cursor position.
The S Command This command invokes the input mode for the substitution
of any amount of text at the cost of the current line. In other words, the current line
is removed and substituted with the input text.
The case of a character can be changed from lowercase to uppercase and viceversa
by using the ~ (tilde) character. To change the case of any required character the
user has to move the cursor on to the character and then give the ~ (tilde) command.
● Unnamed buffer
● Named buffers: "a, "b, "c, …, "z
● Numbered buffers: "1, "2, "3, …, "9
Deleting the Text
The characters x and X are character-deletion commands. One character to the left
of the current cursor position is deleted using the X command. A character under
the current cursor position is removed by using the x command.
PATTERN SEARCHING
In vi any required pattern can be searched by using the commands / (forward slash)
and ? (question mark) in the command mode. These two characters appear on the
same key on the keyboard. The / command is used to search in the forward direction
whereas the ? command is used to search in the backward direction.
Replacing a Pattern
In vi a pattern is replaced by a new pattern using the :s command obviously: this is
an ex mode command. The general form of this command is:
:s/pattern/new pattern/
UNDOING COMMANDS
A command that was just previously executed can be undone by using the u
(lowercase) command in the command mode. When one more u command is used
immediately, this will undo the previous undo command.
JOINING LINES
One or more consecutive lines can be joined using the J (uppercase J) command.
The use of a J joins the current line and the next line. Repeat factors can be used
with this command also. Thus the use of 3J joins the next three lines to the current
line and so on.
Selected line or lines can be written in a separate file using the :w command along
with the name of the file in which lines are to be written.
The ab Command
This is also an ex mode command and is used to have short hand abbreviations for
commonly used expressions or the input text.
The map Command
Using this command one can connect one or more commands to a single key. This
process is known as mapping.
:map Q :q!^M
This is an automatic initialization file, which will be present in the home directory.
This is an optional file.
This is an environment variable. The value of this variable can be set to any required
vi options