Experiment No.
- 3
Aim: - Familiarize with ‘vi’ editor and Linux GUIs.
There are three type of editors
• ED
• EX
• VI
ED and EX are line editor, in this editor line number is assigned to the lines in a file where as
vi is the text editor or screen editor. The vi editor has been part of UNIX-based systems since the
1970s, and its interface shows it. It is arguably one of the last editors to actually use a separate
command mode and data entry mode; as a result, most newcomers find it unpleasant to use.
The vi editor is difficult to learn at first, but once you know it, you never have to use a mouse
or a function key—you can edit and move around quickly and efficiently within files just by using
the keyboard. It shows as much of the file as it can fit on screen. It is the first full screen editor
which allow the user to view and edit the entire document at a time creating and editing of a file
becomes easy using vi editor.
The advantage of learning vi and learning it well is that one will find vi on all Unix based
systems and it does not consume an inordinate amount of system resources. Vi works great over
slow network ppp modem connections and on systems of limited resources. One can completely
utilize vi without departing a single finger from the keyboard.
Starting vi or say to invoke vi
To create a file
Example: - $ vi file.txt
To invoke vi type vi and file name that you want to create when we type vi and the file
name press enter key then vi clears the screen and display a window in which we can enter and
edit text. The easiest way to learn more about vi is to start it and enter :help.
Modes of operation: - There are three mode of operation of vi editor.
• Command mode: - In this mode we insert text in blank text file or editing in existing
text file. Input mode also known as insertion mode. When a key is pressed of input mode
it does not appear on screen but subsequent key depressions do.
• Input mode: - In this mode all key passed by user are interpreted to the editor commands
that is for moving cursor for left-right top-bottom etc. In this the key pressed are not
displayed on screen. By default vi editor in command mode.
• Execute mode: - This mode is used to handle the files and perform substitutions. It
always starts with columns ‘:’.
Input mode: -
• Insert and append:-
i- To insert the text in a file to anywhere.
I- To insert the text in a file only at the beginning of a line.
a- To append the text in file any where
A- To append the text at end of line
• Replace :-
r- Replace single terminal character in a file
s- One character replaced by multiple characters
R- Replace all text on the right of cursor position
S- Replace entire line
• Open line: -
o- Line editing at below of current line
O- Line editing at upper of current line
Execution mode: - These are following command.
:w- saves file and remains in editing mode
:x- saves file and quits editing mode
:wq- same as ‘:x’
:q- quit from editor without saving the buffer
:q!- forcefully quit from the editor without saving the buffer
:sh- escapes to UNIX shell
:recover- recovers file from a crash
Navigation: - There are several navigation keys for controlling the cursor movement.
k- Moves cursor up
j- Moves cursor down
h- Moves cursor left
l- Moves cursor right
b- Moves back to beginning of word
e- Moves forward to end of word
w- Moves forward to beginning of word
0(zero) - moving to the beginning of a line
|- to position the cursor to certain column
$- moves to end of line
[Ctrl-f] - Scrolls forward
Ctrl-b] - scrolls backward
Ctrl-d] – scrolls half page forward
[Ctrl-u] - scrolls half page backward
G –goes to end of file
Repeating the last command:- the vi editor has the facility to repeat the last command,
but most editor do not have. The dot (.) Command is used for repeating both Input and Command
mode that perform editing tasks.
Searching for a pattern: -vi is strong in search and replacement activities. Searching can
be made in both forward and reverse directions, and can be repeated. It is initiated from the
Command mode by pressing a /, which shows up in last line as :
/ - For forward search
? - For backward search
Command for repeating the last pattern searches are:
n- Repeats search in the same direction along which previous search was made
N- Repeats search in direction opposite to that along which previous search was made
Disadvantages: -
• There are no of self explanatory error message in vi if anything goes wrong then there is
an error message appear. Only speaker keeps to inform that something wrong
• No help available in vi editor
• There are three mode in which vi editor work but in each mode same key create different
effects
• vi is key sensitive. Vi can handle that maintain plain text that no fstyle in fault. No graphic
just simple text file we can create, edit and print.
Command Mode :- Where keys are are used as command to act on text.
Input/output Mode :- Where any key depressed is entered as text.
(i) i :- To insert text anywhere in a line . This character doesn’t show up on the
screen , but pressing this key changes the mode from command to input.
(ii) I :- To insert text only at beginning of a line.
(iii) a :- To append text to the right cursor position.
A :- To append text at the end of line.
(iv) o:- This will insert an empty line below current line.
O :- This will insert an empty line above current line.
(v) r :- To replace one single character with another.
R :- To replace more than a single character.
Last line or ex mode :- where commands can be entered in the last line of the screen to
act on text.
(i) :q :- quits editing mode when no change are made to file.