vi_tutorial.doc

advertisement
COP2551
Paul Higbee
INTRODUCTION TO vi
The text editor named vi is a much more
powerful editor than pico. It also takes more
practice to learn to use vi. However, the time it
takes to learn vi should be paid back within about
two programming assignments, so the sooner you
migrate to vi, the less total time you will spend on
your programming assignments for this course.
GENERAL BACKGROUND/STARTING & STOPPING vi
If you want to create a file named "filename"
with vi you start by entering at the $ prompt:
vi filename
If your file already exists and you want to do
some more editing, you will enter the same command:
vi filename
The first thing you must realize about the vi
editor is that it functions in two distinct modes:
command mode and input mode.
When vi first starts, it will be in command
mode. When you want to enter text, you must switch
to input mode. When you want to move the cursor
around your document, or delete text, or do almost
anything other than enter text, you must switch back
to command mode.
When you are in command mode there are a number
of commands that will put vi into input mode. We
will look at some of these commands in this
tutorial. When you are in input mode, all you have
to do to get into command mode is press the <esc>
key. It never hurts to press the <esc> key. If you
are already in command mode, the computer will beep
at you, but no harm is done.
Here are a few more fundamentals before getting
into the nitty-gritty of vi:
1.
While you are entering text, the text is stored
in the editing buffer, a portion of main
memory. The text will not actually be saved on
disk until you give one of the commands (while
in command mode)that saves the text.
2.
The bottom line of the vi screen is called the
status line. The information on the status
line varies with different situations. When
you first start vi, the name of the file you
are editing is displayed on the status line.
At any time when you are in command mode, you
can press ^g to see the file name and the
current line number displayed (^g means press
and hold the control key, then press the 'g'
key). You will also enter certain commands,
called colon commands, on the status line.
3.
When you first start to create a file, you will
see the tilda character ~ in the first column
of each line. vi places this mark on lines
where text has yet to be entered.
We have looked at how to start vi. You also
need to know how to get out of vi. If you want to
save your document and exit vi, from command mode
type ZZ. That's two upper-case Z's. If you want to
exit vi without saving what is in the editing
buffer, from command mode type :q! . If you haven't
yet entered or modified any text, you can use :q to
exit, without the exclamation point. Also, you can
save your editing buffer without exiting vi by
entering :w when you are in command mode. Recall,
that you get into command mode by pressing <esc>.
It is a good idea to save your work often, and to
occasionally back it up by copying it to a different
file name, perhaps filename.bak.
2
COMMANDS FOR LOADING AND SAVING FILES
vi filename
ZZ
:wq
:q!
:w
:r filename2
Start vi, edit filename (entered
from Unix command line).
Terminate vi, saving edited file.
Also terminates vi, saving edited
file.
Terminate vi, leaving file
unchanged.
Save edited file and stay in vi.
Read text from another file into the
current text at the line following
the cursor.
Note: When you type the colon your cursor moves
to the bottom of the screen and waits there for the
next character. You must be in command mode before
typing the exit or read commands shown above. Some
commands, like the ZZ terminate command, do not
require typing the colon first. These commands are
executed immediately as opposed to the "colon
commands" that require you to press <return> to
execute the command.
3
ENTERING TEXT IN vi
To enter text, you must first press one of the
following keys to get into input mode:
i
a
I
A
O
o
Insert text starting at the cursor.
Insert text starting after the cursor.
Insert text beginning at start of line.
Insert text beginning at end of line.
Insert text on a new line before the
current line (open a new line above the
current line).
Insert text on a new line after the
current line.
When entering text, you can use the backspace
key to back over previous entered characters on a
line and retype the characters.
DELETING TEXT IN vi
To delete text, you must first get into command
mode by
pressing <esc> . The following commands will delete
text.
x
dW
D
dd
ndd
.
:i,jd
Delete character under the cursor.
Delete to end of current word.
Delete to end of current line.
Delete current line.
Delete n lines starting with current
line where n is an integer.
Repeat the previous command -- often
useful when deleting.
Delete from line i to line j inclusive.
Note that this is a colon command.
4
You may use the $ character and the . character
in colon commands. The $ means "last line of text"
and the . (period or "full stop") means the "current
line of text" or "line where the cursor is". For
example, you can delete all text from the current
line to the end of the file by giving the command
:.,$d
When deleting text, you may end up with short lines
and want to join one line with the next. From
command mode, typing the character J will join the
current line with the next line. If you want to
split lines, put the cursor where you want the line
to end, press i to get in input mode, and press
return to split the line with that portion of the
line after the cursor dropping down to the next
line.
J
Joins lines (actually deletes the
newline character at the end of the
line).
UNDOING MISTAKES IN vi
If you delete something and suddenly realize
that it was a mistake, you can use the vi "undo"
commands.
u
U
Undo the last change made.
Restore current line to previous state.
MOVING AROUND IN YOUR TEXT
The cursor always shows where you are in your
text. The following commands are used to move the
cursor. Other, more advanced commands, also exist.
You must, of course, be in command mode (press <esc>
) to use these commands. It is important to be
aware of how to use the k, j, l, h keys to move
around, since these keys can also be used on the
Unix/Linux command lines when your shell is the Korn
shell (which ours is).
5
Note that the 'h' and 'l' keys do the same thing as
the left and right arrow keys, but you don't have to
lift your fingers off the home keys (if you are a
touch typist).
<space>
l
right arrow
h
left arrow
k
up arrow
j
down arrow
0
$
Move
"
"
Move
"
Move
"
Move
"
Move
Move
G
nG
Move to last line of file.
Move to line #n where n is an
integer.
<cntrl-u>
<cntrl-d>
<cntrl-f>
Move half a
Move half a
Move a full
text.
Move a full
<cntrl-b>
right one character.
"
"
"
"
"
"
left one character.
"
"
"
up one line.
"
"
"
down one line.
"
"
"
to start of line.
to end of line.
screen up in text.
screen down in text.
screen forward in
screen back in text.
Note: You can turn on line numbers in vi by
using the colon command
:set nu
Turn line numbers off using
:nonu
6
SEARCHING FOR TEXT STRINGS
Often the fastest way to get to a location in a
file is to search for a word that you know is in
that location. If initially you find that word, but
at a different location, you can easily repeat the
search.
/xxx
?xxx
n
N
Search
Search
Repeat
Repeat
forward for the text string xxx.
backward for the text string xxx.
search in same direction.
search in opposite direction.
CHANGING TEXT
You may have misspelled a word or just want to
change either a single character, a string of
characters, or a word everwhere it occurs in your
text. The following are some of the commands that
can be used. Except for 'r', you will be put in
input mode and remain in input mode until you
press <esc>.
r
Change (replace) character under the
cursor and stay in command mode.
R
Replace text starting at the cursor
position.
C
Change remainder of line. A $ will
appear at the end of the line. The $
and all characters between the current
cursor position and the $ will go away
after you press return.
:i,js/oldtext/newtext/
Change first occurrence of oldtext
between lines numbered i to j
(inclusive) to newtext.
:i,js/oldtext/newtext/g
Change all occurrences of oldtext
between lines numbered i to j
(inclusive) to new text. s stands for
substitute and g stands for global.
7
COPYING OR MOVING TEXT
You can use the delete commands discussed above
or "yank" commands to place text in a cut-and-paste
buffer. The cursor can then be moved to another
location in the text and a "put" command can be used
to copy the yanked or deleted text to the new
location.
yw
yy
nY
P
p
Place current English word in buffer.
Place current line in buffer.
Place n lines in buffer.
Paste cut (deleted or yanked) text
before cursor.
Paste cut text after cursor.
The summary of vi provided above is not
complete. Chapters 2 and 8 of the Sobell book, A
Practical Guide to the Unix System, provides a more
complete coverage along with plenty of examples.
What I have provided here should, however, satisfy
most of your editing requirements.
Paul Higbee
6/18/03
8
Download