Visual (vi) Editor
CHAPTER
7
• Objectives
– Upon completion of this module, you should be
able to:
• Define the three modes of operation used by the vi
editor.
• Start the vi editor.
• Position and move the cursor in the vi editor.
• Switch between vi modes.
• Create and delete text.
• Copy or move text.
• Set vi options.
• Perform search and replace functions within the vi editor.
• Exit the vi editor.
Introducing vi
• The visual display (vi) editor
is an interactive editor used Figure 7-1
to create or modify text files.
• All text editing with the vi
editor takes place in a buffer.
• Changes can either be
written to the disk or be
discard.
• For those who intend on
becoming system
administrators, it is important
to know how to use the vi
editor.
• You need to know how to
use the vi editor in case the
windowing system is now
available. Terminal Window – Initial vi Display
• The vi editor is also the only
text editor that can be used
to edit certain system files
vi Modes
• The vi editor is a command-line editor
that has three basic modes of operation:
– Command mode
– Edit mode
– Last line mode
Command Mode
• This is the default mode for the vi
editor.
• In this mode, you can enter commands
to delete, change, copy, and move text;
position the cursor; search for text
strings; or exit the vi editor.
Edit Mode
• In this mode, you can enter text into a
file.
• To instruct the vi editor to enter edit
mode, enter one of the following three
commands:
– i (insert)
– o (open)
– a (append)
Last Line Mode
• While in command mode, you can use
advanced editing commands by typing
a colon (:), which places you at the
bottom line of the screen.
• This is called last line mode.
• However, all commands are initiated
from command mode.
Switching Modes
• By typing i, o, or a command, the vi editor
leaves the default command mode and enters edit
mode.
• In edit mode, text is not interpreted as commands by
the vi editor. Now, everything you type is entered
into the file as text.
• When you have finished entering text in the file, you
can return the vi editor to command mode by
pressing the Escape key.
• When you are back in command mode, you can then
save the file an quit the vi editor.
• For example:
1. Type vi finename to create a file.
2. Type the i command to insert text.
3. Press the Escape key to return to command mode.
4. Type :wq to write and save the file and exit the vi editor.
Invoking the vi Editor
• To create a new file, invoke the vi editor with a new file name
by typing commands to create, edit, or view a file.
• Command Format
– vi option(s) filename
– view filename
• Input commands
– To insert or append text, use the commands in Table 7-1.
Table 7-1
Command Meaning
a Appends text after the cursor Note
A Appends text at the end of the
The vi editor is case
line
sensitive, so use the specified
i Inserts text before the cursor
case when using these cursor-
I Inserts text at the beginning of input commands.
the line
o Opens a new line below the
cursor
Positioning Commands
Table 7-2
• Table 7-2 Command Meaning
h, , or Back
shows the Space
Moves left one character
j or Moves down one line
key
k or Moves up one line
sequences l, , or Space bar Moves right ( forward ) one character
that control w Moves forward one word
cursor b Moves back one word
movement e Moves to the end of the current word
in the vi $ Moves to the end of the line
editor. 0 (zero) Moves to the beginning of the line
^ Moves to the first non-whitespace character
on the line
Return Moves down to the beginning of the next line
Control-F Pages forward one screen
Control-D Scrolls down one-half screen
Control-B Pages back one screen
Control-U Scrolls up one-half screen
Control-L Refreshes the screen
Editing Commands
• Deleting Text
– To delete text, use the options in table 7-3.
Table 7-3
Command Meaning
x Deletes a character at the cursor
dw Deletes a word ( or part of the word to the right of the
cursor)
dd Deletes the line containing the cursor
D Deletes the line to the right of the cursor ( from cursor
position to the end of the line )
:5, 10d Deletes Lines 5 through 10
Undoing, Repeating, and
Changing Text Commands
• To change text, undo a change, or
repeat an edit function, use the
commands in Table 7-4.
• Many of these commands change the
vi editor to edit mode.
• To return to command mode, press the
Escape key.
Edit Commands for the vi Editor
-1
Table 7-4
Command Meaning
cw Changes a word (or part of a word ) at the cursor
location to the end of the word
R Overwrites or replaces characters on the line
C Changes from cursor to end of the line
s Substitutes string for characters
R Replaces the character at the cursor with one other
character
Edit Commands for the vi Editor
-2
Table 7-4 ( Continued )
Command Meaning
J Joins the current line and the line below
xp Transposes the character at the cursor and the character
to the right
~ Changes the case of the letter ( uppercase or lowercase )
at the cursor
u Undoes the previous command
U Undoes all changes to the current line
u Undoes the previous last-line command
:r filename Inserts ( reads ) the file at the line after the cursor
Edit Commands for the vi Editor
-3
• To search and replace text, use the options in
table 7-5.
Table 7-5
Command Meaning
/string Searches forward for the string
?string Searches backward for the string
n Finds the next occurrence of the string
N Finds the previous occurrence of the
string
:%s/old/new/g Searches and replaces globally
Copying and Pasting Text
• The copy commands write the copied text into a temporary
buffer. The paste commands read the text from the temporary
buffer and write the text into the current document at the
specified location.
• To
Table 7-6copy and paste text, use the options in table 7-6
Command Meaning
yy( lowercase ) Yanks a copy of a line
p ( lowercase ) Puts yanked or deleted text after the current position
p ( uppercase ) Puts yanked or deleted text before the current
position
:1, 3 co 5 Copies Lines 1 through 3 and puts them after Line 5
:4, 6 m 8 Moves Lines 4 through 6 to Line 8 ( Line 6 becomes
Note Line 8; Line 5 becomes Line 7, and Line 4 becomes
Line buffer.
Both delete and yank write to a 6 ) When yanking, deleting, and pasting, the put
commands insert the text differently depending on whether you are pasting words or lines.
Saving and Quitting Files
Table 7-7
Command Meaning
:w Saves the changes ( write buffer )
:w new_filename Writes the contents of the buffer to
new_filename
:wq Saves the changes and quits vi
:x Saves the changes and quits vi
zz Saves the changes and quits vi
:q! Quits without saving changes
:wq! Saves the changes and quits vi ( ! Overrides
read-only permissions for the owner of the
file only )
Customizing Your vi Session
• The vi editor includes options for customizing edit
sessions, such as:
– Displaying line numbers
– Displaying invisible characters, such as tab and end-of-line
characters
• Use the set command in command mode to control
these options, as shown in table 7-8.
• You can also place these options in a file you create
in you home directory called .exrc.
• The set options are placed in this file, without the
preceding colon, one command to a line.
• After the .exrc file has been created, it is read by
the system each time you open a vi session.
Edit Session Customization
Commands
Table 7-8
Command Meaning
:set nu Show line numbers
:set nonu Hides line numbers
:set ic Instructs searches to ignore case
:set noic Instructs searches to be case sensitive
:set list Displays invisible characters, such as tab and
end-of-line
:set nolist Turns off the display of invisible characters
:set showmode Displays current mode of operation
:set noshowmode Turns off the mode display
:set Displays all the vi variables that are set
Displays all possible vi variables and their
:set all
current settings
Positioning & Refreshing
Command
• To find a particular line, use the options in table 7-9.
Table 7-9
Command Meaning
G Goes to the last line of the file
1G Goes to the first line of the
file
:21 Goes to Line 21
21G Goes to Line 21
• To clear the screen or insert files, use the option in
table 7-10.
Table 7-10
Command Meaning
Control-L Refreshes the screen
Chapter7 exercise: Using the vi
• Tasks
Editor
– Complete the following steps:
1. In your home directory, there should be a file called tutor.vi.
Make sure you are currently in your home directory, and then
open this file with the command:
$ vi tutor.vi
This opens a vi tutorial file.
2. Complete the lessons outlined in this tutorial.
$ vi demo
Comman
d
: / ? Mode i a o
Return Escape
Last
Edit
Line
Mode
Mode