Introduction
vi is a powerful editor with many
features. It can handle very large files much easier than a program like
Microsoft Word. Unlike Word, vi is only a TEXT EDITOR and you cannot include
graphics or fancy fonts in your file.
Although there are lots of different
commands in vi, it is possible to get started using vi knowing only a small
subset of them. As you feel more comfortable with vi, you can learn the more
advanced features.
vi has 3 modes:
- write mode, used for entering text
- command mode and
- command line mode, used for entering commands to vi.
Remember, in vi, the mouse cannot be
used to issue editor commands or move the cursor.
Write
Mode
When you first enter the editor, you
are in the command mode. To enter the write mode, type the letter a
for append. This is one of the four possible commands for entering the write
mode. vi is Case Sensitive. Lower case commands are different from upper case
commands.
Command
Mode
You are in command mode whenever you
hit esc to leave the write mode. In command mode, you can move the cursor
anywhere in the file.
The x key deletes individual characters,
while dd deletes an entire line. To enter the insert mode type the i key.
When you're done inserting, hit the "esc" key to return to the
command mode.
Command-Line
Mode
Command-line mode is used for such
things as writing changes and exiting the editor. To enter command-line mode,
type : while in command mode. The : will now appear at the bottom of
the screen and the command which you type will appear on that line.
1.
How to create a new file or modify an existing file:
EXAMPLE:
% vi
practice
In this example, the new file
"practice" is created if it doesn't already exist.
The screen will look like this after
opening a new file entitled, "practice".
=============================================================
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"practice"
[New File]
=============================================================
The
Edit Mode
Once in vi, you are placed in the
command mode. You cannot enter text in this mode. If you try, the letters you
try to type will not appear on the screen. This is because ordinary letters are
interpreted as commands.
You must enter the edit mode to type
text.
To do so, use one of the following
commands. The vi text editor is case sensitive, which means the same letters in
upper and lower case have different command responses.
2.
How to add information to a file:
What to Type
|
Result
|
a
|
append after the cursor
|
i
|
insert before the cursor
|
The "esc" key must be used
to end any of the above commands or to begin any of the following commands
(depending on which way you look at it). You must also hit the "esc"
key prior to any cursor movement. When in doubt, hit the "esc" key
and proceed.
3.
How to delete information from a file:
What to Type
|
Result
|
x
|
deletes the character the cursor
is under
|
dd
|
deletes the line the cursor is on
|
4.
More edit commands
In the command mode, it is possible
to move the cursor up, down, left or, right by using the arrow keys on the
keyboard. However, no matter what type of keyboard you have, it is possible to
use the vi commands, h, j, k, and l, all in lowercase, to control the cursor.
These commands move the cursor as
follows:
What to Type
|
Result
|
j
|
moves the cursor up one line
|
k
|
moves the cursor down one line
|
h
|
moves the cursor backwards on a
line
|
l
|
moves the cursor forward on a line
|
5.
How to save your work and exit from vi
First you need to enter the command
mode by pressing the Escape key.
Now you are ready to give any one of a number of line commands.
Now you are ready to give any one of a number of line commands.
What to Type
|
Result
|
:wq
|
saves the current changes and
exits vi
|
:w!
|
saves the current changes but does
not exit vi
|
:q!
|
exits vi without saving any change
|
6.
More command lines
Other popular command include
What to type
|
What it does
|
ndd
(for example 3 dd)
|
Deletes n number of lines
|
^
|
Go to start of line
|
nY
|
Copy n number of lines
|
$
|
go to end of line
|
:1
|
goes to top of file
|
:5
|
goes to fifth line of file
|
:$
|
goes to bottom of file
|
:set
nu
|
will number all your lines
|
:set
nonu
|
turn off line numbering
|
:1,$s/oldtext/newtext/g
|
Global substitutions
|
Vi Quick Reference
ENTERING vi
vi name start vi editor with file name .
The file is created if it doesn't exist.
LEAVING vi
ZZ exit from vi, saving changes.
:q! exit from vi, discarding changes.
CURSOR POSITIONING
h moves left one character position.
j moves down one line.
k moves up one line.
l moves right one character position.
0 (zero) moves to the beginning of a line.
w moves right one word.
b moves left one word.
CTRL-u moves up 1/2 screen full.
CTRL-d moves down 1/2 screen full.
G moves to the bottom of the file.
nG moves to line number n .
CTRL-l clear screen and re-draw.
TEXT MODIFICATION
itextESC inserts text to the left of the cursor.
Insert doesn't cause the cursor to move;
text appears as it is typed, terminate with
ESC.
atextESC appends (inserts) text to the right of
the cursor, terminate with ESC.
RtextESC Replaces (overprints) characters at the
cursor position, terminate with ESC.
dd deletes the line the cursor is on.
ndd deletes n lines from the cursor position.
D deletes characters from the cursor position
to the end of the line.
x deletes the character at the cursor.
nx deletes n characters to the right of the
cursor.
u undo the last change.
PATTERN SEARCHING
/pat/ positions the cursor at the next
occurrence of the string pattern.
NOTES:
ESC represents the ESC key. Press the ESC key when
it is called for in the above commands.
CTRL- represents the CTRL key. Hold the CTRL key and
press the following key simultaneously.
CURSOR POSITIONING
} move down one paragraph.
{ move up one paragraph.
mx save the current cursor position and label it
with the letter x. (x is any letter)
'x return to the cursor position labeled x.
TEXT MODIFICATION
dw delete the next word.
. (period) repeat last change.
A append at the end of the current line.
P put back deleted line(s). Text deleted with D
and dd commands may be pasted back with the P
command. Text is pasted in before the cursor
position.
:a,bs/old/new/
From line number 'a' to line number 'b',
substitute the pattern 'old' with the pattern
'new'. You may use any text string which
doesn't contain a carriage return in place of
the 'old' and 'new' strings. Use CTRL-G to tell
what line the cursor is on.
PATTERN SEARCHING
// search for the next occurrence of a previously
specified search string.
MISCELLANEOUS
:w write out current changes. The vi editor works
on a copy of your file. The :w command causes
the editor to write its copy over the original
which is on the disk.
:w name write out changes to the file name . This is
like the :w command but the changes are written
into the file you specify. (good for making
intermediate copies)
Cut and Paste Move to the begining of the text to cut. Use
dd to delete (cut) several lines. Use D to cut
only the end of one line. Move to the place
where you wish to paste the text. Use P to
put back the text. You may need to clean up
the spacing after pasting.
No comments:
Post a Comment