You are on page 1of 10

Unit 3

Chapter 4: ​Vi Editor

● Corrections include many activities like inserting text, removing characters or


lines, moving paragraphs and so on. The process of carrying out such activities
is known as editing.

THE vi EDITOR

● vi​ stands for visual editor. This editor is started by using the command ​vi​ at the
shell prompt.

The Three Modes of the vi Editor

● 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 ​command​ mode


● This is the mode into which the user gets into as soon as s/he enters into the ​vi
environment from the shell by using the ​vi​ command. In the command mode all the
keys pressed by the user are interpreted to be the editor commands. The keys that
are pressed in this mode are not displayed on the screen.
● The user can get into the input mode by giving anyone of the following
commands.
o, O, r, R, i, I, s, S, a, A

● 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

Editing a file with the editor involves the following steps:

● Invoking the editor.


● Moving the cursor to the correct editing position.
● Editing the file using proper editing commands.
● Saving the edited file and quitting ​vi​.

Invoking the vi Editor

● 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.

MOVING THE CURSOR

Moving by One Position


Moving by Words

Moving to Either Ends of a Line

Moving to the Top, Bottom or Middle of the Screen


Moving by Screens

The G (go) Command

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.

Changing the Case

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.

COPYING AND MOVING TEXT


Copying involves extracting the required text and then placing the extracted text at
any required position in the file. Moving involves deleting the required text and then
placing it at any required position in the file.
More Editor Buffers
vi​ actually works on a copy of the file called the editor buffer.

With ​vi​ there exists as many as 36 buffers. They are

● 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.

Yanking the Text

Placing the Text

In ​vi​, the placing (referred to as pasting in word processing) activity is referred to as


putting.​ Putting is done using either the ​p​ (lowercase p) command or ​P​ (uppercase
P) 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.

WRITING SELECTED LINES ONTO A SEPARATE FILE

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 set Command


:set number​ or :​set nu​ commands sets the line number option. When this option is
set, line numbers appear automatically. By default, no line numbers appear. If
required the option ​nonumber​ can be used with the ​set​ command to remove line
numbers.

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

The .exrc File

This is an automatic initialization file, which will be present in the home directory.
This is an optional file.

The EXINIT Variable

This is an environment variable. The value of this variable can be set to any required
vi​ options

You might also like