Thursday, May 27, 2010

A quick reference list of vi editor commands


Using the viEditor
Objectives
This module introduces the vi editor and describes the vi commands.
These commands include the input commands, the positioning
commands, and the editing commands.
Upon completion of this module, you should be able to:
l Describe the fundamentals of the vi editor
l Modify files by using the vi editor
The following course map shows how this module fits into the current
instructional goal.

Fundamentals of the vi Editor
The visual display or vi editor is an interactive editor that you can use to
create and modify text files. You can use the vi editor when the desktop
environment window system is not available. The vi editor is also the
only text editor that you can use to edit certain system files without
changing the permissions of the files.
All text editing with the vi editor takes place in a buffer. You can either
write the changes to the disk, or discard them.

The vi Editor Modes of Operation
The vi editor is a command-line editor that has three basic modes of
operation:
l Command mode
l Edit mode
l Last line mode

Command Mode
The command mode is the default mode for the vi editor. In this mode,
you can perform commands to delete, change, copy, and move text. You
can also position the cursor, search for text strings, and exit the vi editor.

Edit Mode
You can enter text into a file in the edit mode. The vi editor interprets
everything you type in the edit mode as text. To enter the edit mode,
perform the commands:
l i – Inserts text before the cursor
l o – Opens a new blank line below the cursor
l a – Appends text after the cursor

Last Line Mode
You can use advanced editing commands in the last line mode. To access
the last line mode, enter a colon (:) while in the command mode. The
colon places your cursor at the bottom line of the screen.

Switching Between the Command and Edit Modes
The default mode for the vi editor is the command mode. When you
perform an i, o, or a command, the vi editor switches to the edit mode.
After editing a file, press Escape to return the vi editor to the command
mode. When in the command mode, you can save the file and quit the
vi editor.
The following example shows how to switch modes in the vi editor:
1. Perform the vi filename command to create a file. You are
automatically in the command mode.
2. Type the i command to insert text. The i command switches the
vi editor to the edit mode.
3. Press Escape to return to the command mode.
4. Perform the :wq command to save the file and exit the vi editor.

Using the vi Command
The vi command enables you to create, edit, and view files in the
vi editor.
The syntax for the vi command is:
vi
vi filename
vi options filename
If the system crashes while you are editing a file, you can use the
-r option to recover the file.
To recover a file, perform the command:
$ vi -r filename
The file opens so that you can edit it. You can then save the file and exit
the vi editor.
$ vi -R filename
The file opens in read-only mode to prevent accidental overwriting of the
contents of the file.

Modifying Files With the vi Editor
You can use the vi editor to view files in the read-only mode, or you can
edit files in the vi editor using the vi editing commands. When using the
vi editor, you can move the cursor using certain key sequences.

Viewing Files in the Read-Only Mode
The view command enables you to view files in the read-only mode. It
invokes the vi editor with the read-only option. Although most of the vi
commands are available, you cannot save changes to the file.
The syntaxfor the view command is:
view filename
To view the dante file in the read-only mode, perform the command:
$ cd
$ view dante
The dante file appears. Perform the :q command to exit the file and the
vi editor.

Inserting and Appending Text
Table 5-1 describes the commands that you can use to insert and append
text to a file using the vi editor. These commands switch the system to the
edit mode. To return to the command mode, press Escape.
Note – The vi editor is case sensitive. Use the appropriate case for the
input commands.
Also, most of the editing commands and cursor movement can be
preceded by a number to repeat the command that number of times.
Table 5-1 Input Commands for the vi Editor
Command Function
a Appends text after the cursor
A Appends text at the end of the line
i Inserts text before the cursor
I Inserts text at the beginning of the line
o Opens a new line below the cursor
O Opens a new line above the cursor
:r filename Inserts text from another file into the current file

Moving the Cursor Within the vi Editor
Table 5-2 shows the key sequences that move the cursor in the vi editor.
Table 5-2 Key Sequences for the vi Editor
Key Sequence Cursor Movement
h, left arrow, or
Backspace
Left one character
j or down arrow Down one line
k or up arrow Up one line
l, right arrow, or
space bar
Right (forward) one character
w Forward one word
b Back one word
e To the end of the current word
$ To the end of the line
0 (zero) To the beginning of the line
^ To the first non-white space character on the line
Return Down to the beginning of the next line
G Goes to the last line of the file
1G Goes to the first line of the file
:n Goes to Line n
nG Goes to Line n
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
Control-G Displays current buffer information

Editing Files by Using the vi Editing Commands
You can use numerous commands to edit files using the vi editor. The
following sections describe basic operations for deleting, changing,
replacing, copying, and pasting. Remember that the vi editor is case
sensitive.
Using the Text-Deletion Commands
Table 5-3 shows the commands that delete text in the vi editor.
Note – Output from the delete command writes to a buffer from which
text can be retrieved.
Table 5-3 Text-Deletion Commands for the vi Editor
Command Function
R Overwrites or replaces characters on the line at and to the
right of the cursor. To terminate this operation, press
Escape.
C Changes or overwrites characters from cursor to the end
of the line.
s Substitutes a string for a character at the cursor.
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 from the cursor to the right end of the
line.
:n,nd Deletes Lines n –n (for example, :5,10d deletes
Lines 5–10).

Using the Text-Changing Commands
Table 5-4 describes the commands that you can use to change text, undo a
change, and repeat an edit function in the vi editor. Many of these
commands change the vi editor to edit mode. To return to command
mode, press Escape.
Table 5-4 Edit Commands for the vi Editor
Command Function
cw Changes or overwrites characters at the cursor location
to the end of that word
r Replaces the character at the cursor with one other
character
J Joins the current line and the line below
xp Transposes the character at the cursor and the character
to the right of the cursor
~ Changes the case of the letter, either uppercase or
lowercase, at the cursor
u Undoes the previous command
U Undoes all changes to the current line
. Repeats the previous command

Using the Text-Replacing Commands
Table 5-5 shows the commands that search for and replace text in the vi
editor.

Using the Text-Copying and Text-Pasting Commands
The yy command yanks lines of text and holds a copy of the lines in a
temporary buffer. The put commands (p, P) inserts the lines of text from
the temporary buffer and writes the text into the current document at the
specified location. The copy (co) and move (m) commands copy or move
specified lines to a requested location within the file.
Table 5-6 shows the commands that yank (yy) and put (p, P) text in the vi
editor.
Table 5-5 Search and Replace Commands
Command Function
/string Searches forward for the string.
?string Searches backward for the string.
n Searches for the next occurrence of the string. Use
this command after searching for a string.
N Searches for the previous occurrence of the string.
Use this command after searching for a string.
:%s/old/new/g Searches for the old string and replaces it with the
new string globally.
Table 5-6 Copy and Paste Commands
Command Function
yy Yanks a copy of a line
p Puts yanked or deleted text under the line containing
the cursor
P Puts yanked or deleted text before the line containing
the cursor

Using the File Save and Quit Commands
Table 5-8 describes the commands that save the file and quit the vi editor.
Table 5-7 Additional Copy and Paste Commands
Command Function
:n,n co n Copies Lines n –n and puts them after Line n. For
example, :1,3 co 5 copies Lines 1–3 and puts them
after Line 5.
:n,n m n Moves Lines n –n to Line n. For example, :4,6 m 8
moves Lines 4–6 to Line 8. Line 6 becomes Line 8.
Line 5 becomes Line 7. Line 4 becomes Line 6.
Table 5-8 Save and Quit Commands
Command Function
:w Saves the file with changes by writing to the disk
:w new_filename Writes the contents of the buffer to new_filename
:wq Saves the file with changes and quits the vi editor
:x Saves the file with changes and quits the vi editor
ZZ Saves the file with changes and quits the vi editor
:q! Quits without saving changes

Customizing a vi Session
You can customize a vi session by setting variables for the session. When
you set a vi variable, you enable a feature that is not activated by default.
You can use the set command to enable and disable variables.
Table 5-9 describes some of the variables of the set command, including
displaying line numbers and invisible characters, such as the Tab and the
end-of-line characters.
To create an automatic customization for all of your vi sessions, complete
the following steps:
1. Create a file in your home directory named .exrc.
2. Enter the set variables into the .exrc file.
3. Enter set variable without the preceding colon.
4. Perform one command on one line.
The vi editor reads the .exrc file located in your home directory each
time you open a vi session, regardless of your current working directory.
Table 5-9 Edit Session Customization Commands
Command Function
:set nu Shows 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 ^I for a Tab
and $ for end-of-line characters
:set nolist Turns off the display of invisible characters
:set showmode Displays the current mode of operation
:set noshowmode Turns off the mode of operation display
:set Displays all the vi variables that are set
:set all Displays all set vi variables and their current
values

Exercise: Using the vi Editor
In this exercise, you practice performing vi editor commands in the
tutor.vi tutorial. Use Figure 5-3 on page 5-14 as a reference to complete
the exercise.
Preparation
No special preparation is required for this exercise.
RLCD
In addition to being able to use local classroom equipment, this lab was
designed to also use equipment located in a remote lab data center.
Directions for accessing and using this resource can be found at:
http://fn1.brom.suned.com/
Ask your instructor for the particular SSH configuration file that you
should use to access the appropriate remote equipment for this exercise.
Tasks
To learn how to use the vi editor, complete the following steps.
1. Make sure that you are in your home directory. To open the
tutor.vi tutorial file, perform the following command:
$ vi tutor.vi
2. Complete the lessons outlined in this tutorial.

Search Functions
/exp Go forward to exp
?exp Go backward to exp
Move and Insert Text
:3,8d Delete line 3-8
:4,9m 12 Move lines 4-9 to 12
:2,5t 13 Copy lines 2-5 to 13
:5,9w file Write lines 5-9 to file
Save Files and Exit
:w Write to disk
:w newfile Write to newfile
:w! file Write absolutely
:wq Write and quit
:q Quit editor
:q! Quit and discard
:e! Re-edit current file,
Discard buffer
Control Edit Session
:set nu Display line number
:set nonu Turn off line number
:set all Show all settings
:set list Display invisible
Characters
:set wm=5 Wrap lines 5 spaces
From right margin
Screen/Line Movement
j Move cursor down
k Move cursor up
h Move cursor left
l Move cursor right
0 Go to line start (zero)
$ Go to line end
G Go to last file line
Word Movement
w Go forward 1 word
b Go backward 1 word
Search Functions
n Repeat previous search
N Reverse previous search
Delete Text
x Delete 1 character
dw Delete 1 word
dd Delete 1 line
D Delete to end of line
d0 Delete to start of line
dG Delete to end of file
Cancel Edit Function
u Undo last change
. Do last change again
Copy and Insert Text
Y Yank a copy
5Y Yank a copy of 5 lines
p Put below cursor
P Put above cursor

No comments:

Post a Comment