Tải bản đầy đủ (.docx) (9 trang)

Using the vi Editor

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (137.65 KB, 9 trang )

Using the vi Editor
Objectives
Upon completion of this module, you should be able to:
• Differentiate between the three odes of vi and identify the commands that belong
to each mode
• Start vi
• Position and move the cursor in vi
• Create text in vi
• Delete text in vi
• Copy or move text in vi
• Set vi options
• Exit the vi editor
• Perform search and replace functions within vi
Discussion – Editing system files is a powerful tool for customizing the Linux environment.
What tools would you use to edit system files?
Introduction to vi
The visual display (vi) editor is an interactive editor that is used to create and/or modify text
files.
The vi editor uses a screen display, but you cannot use the mouse to position the cursor.
All editing with the vi editor is done within a buffer. Changes can be written to the disk or
discarded.
It is important for users who are learning to become system administrators to know how to use
vi. It is the only full screen editor that can be used to edit crucial system files. This skill is also
needed in case the windowing system is not available.
 Depending on the instructor’s style, this module may be best taught by covering
the basic modes and commands of vi contained in the lecture parts of this module in a
summary fashion. The purpose of the lab for this module is to allow the students to learn vi
by using it. The lab covers in a practical fashion much of what is covered in the module, along
with some things that are not.
vi Modes
There are three modes of operation in vi:


• Command mode
• Entry mode
• Last-line mode
When you open a file with vi, you are in command mode. In this mode, you can enter
positioning and editing commands to perform functions. While in command mode you can do
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.
You must be in entry mode to enter text. To enter text, you must type a vi insert command such
as i, o, or a. This takes vi out of command mode and puts it into entry mode. In this mode, text
will not be interpreted as editing commands. When you finish entering text in your file, press the
Escape key to return to command mode.
Invoking vi
Command Format
vi [option(s)] [filename]
view [filename]
Example
To create a new file, invoke vi with a new file name by typing
$ vi filename
Table 9-1 contains commands you can use to create, edit, or view a file.
Table 9-1 : vi Commands
Command Meaning
vi filename Open or create a file
vi Open a new file to be named later
vi -r filename Recover a crashed file
view filename Open a file as read-only
 Consider having students run vi with set showmode set from the beginning.
Input Commands
To append or insert text, use the following options:
Table 9-2 Append and Insert Commands
Command Meaning

a Append text after the cursor
A Append text at the end of the line
i Insert text before the cursor
I Insert text at the beginning of the line
o Open a new line below the cursor
O Open a new line above the cursor
Positioning Commands
The following pages list the vi editor’s editing and positioning commands, which are used to
make changes.
The vi editor is case sensitive, so use the specified case when using the editing and positioning
commands.
The Table 9-3 shows the key sequences for character movement:.
Table 9-3 : Key Sequences
Command Meaning
h, ←, or Back Space Move left one character
j or ↓ Move down one line
k or ↑ Move up one line
l,→, or Space bar Move right (forward) one character
w Move forward one word (including punctuation)
W Move forward one word (past punctuation)
b Move back one word (including punctuation)
B Move back one word (past punctuation)
e Move to end of current word
$ Move to end of line
0 (zero) or ^ Move to beginning of line
Return Move down to beginning of next line
H Move to top of screen
M Move to middle of screen
L Move to bottom of screen
Control-f Page forward one screen

Control-d Scroll down one-half screen
Control-b Page back one screen
Control-u Scroll up one-half screen
Editing Commands
Deleting Text
To delete text, use the following options:
Table 9-4 Text Deletion Commands
Command Meaning
x (lowercase) Delete character at the cursor
X (uppercase) Delete character to the left of the cursor
dw Delete word (or part of word to right of cursor)
3dw Delete three words
dd Delete line containing the cursor
3dd Delete three lines
D Delete line to right of cursor (from cursor position to the end of the
line)
dG Delete to end of file
d1G Delete from beginning of file to cursor
:5,10d Delete lines 5 through 10
Undoing, Repeating, and Changing Text Commands
To change text or to cancel or repeat edit functions, use the following commands. Many
of these commands change you to Insert mode until you press Escape.
Table 9-5 Editing Commands
Command Meaning
cw Change word (or part of word) at the cursor location to the end of
the word
3cw Change three words
R Overwrite or replace characters on line
C Change from cursor to end of line
s Substitute string for character(s)

r Replace character at cursor with one other character
i (Return) Break line
J Join current line and line below
xp Transpose character at cursor and character to the
right
~ Change case of letter (upper or lower) at cursor
u Undo previous command
U Undo all changes to current line
:u Undo previous last-line command
Copying and Pasting Text
To copy and paste text, use the following options:
Table 9-6 Copy and Paste Commands
Command Meaning
yy Yank a copy of line
Y Yank a copy of line
p Put yanked or deleted line below current line
P Put yanked or deleted line above current line
:1,3 co 5 Copy lines 1 through 3 and put after line 5
:4,6 m 8 Move lines 4 through 6 to line 8 (line 6 becomes line
8; line 5 becomes line 7, and line 4 becomes line 6)
Note – Both delete and yank write to a buffer. When yanking, deleting, and pasting, the
put commands insert the text differently depending on whether you are pasting a word(s)
or a line(s).
Saving and Quitting Files

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×