Getting started
Overview
Using UNIX
Usernames
Logging in
Logging
out
Commands
Typing in
commands
Commands and
options
Communication with other
users
Email
Other
communication facilities
Files
The editor Vi
Vi commands (command
mode)
Vi commands (colon mode)
Other editors
Input
and output
Scripts
Here-documents
Pipes
Making copies of input
and output
Pagers
Emergencies
Getting
help
Summary
Exercises
|
The editor Vi
Vi is the 'standard' screen editor on UNIX. Pronounced
'vee-eye', it stands for 'VIsual display editor'. Whilst being the
editor of choice for many systems programmers, it has a reputation,
arguably undeserved, for being difficult to use. There is a
tradeoff here - if you use a simple, graphical editor, then you
will be able to create files quickly, and easily make
simple changes to them. However, complex editing cannot
often be done quickly with simple editors. There is a learning
curve for using Vi, but once mastered the benefits later on are
substantial. Furthermore, the skills needed to use Vi overlap
substantially with those needed for UNIX shell programming, and
include topics covered later on in this book, such as Regular
Expressions and the stream editor Sed.
The command vi invokes the Vi screen editor, which
has facilities to enter text into a file and to change text already
there. In this subsection we discuss only a small proportion of
Vi's facilities, enough to allow you to create and edit files for
the rest of this book. To edit the file myfile (say),
type vi followed by the name of the file:
$ vi myfile
If you have already created a file called myfile
then choose a filename you do not yet have.
Your screen (or window) will be cleared, and the cursor will
appear in the top-left corner of the screen. Along the left-hand
side of the screen will be a column of ~ (tilde) indicating that
those lines on the screen are not (yet) being used by Vi.
Additionally, a message may appear on the bottom line of the
screen.
Now, type the letter a (append text) and type in
several lines of text - the Return key will terminate each
line - followed by ESC. Then press h ,
j , k , l , and see the cursor
moving around the screen one square at a time:
You won't be allowed to move the cursor to any location that
does not contain text. Position the cursor near the centre of your
text and press a again. Type in more text - it will
appear after the cursor position. As before, ESC will
terminate the input. If you're not sure whether or not you have
typed ESC, then type it again - if you type too many, the
extra ones will be ignored and will do no harm. Move the cursor to
the centre again, type i (insert text), and repeat
what you did for a - you will see that text is
inserted, but this time before the cursor position.
To delete text, you can remove one character at a time by
placing the cursor on it and typing x . To remove a
whole line, place the cursor on the line and type dd .
To remove part of the line from the cursor to the end of the line,
type D . Try these commands now.
When you have finished making all the changes you desire, type
ZZ (being careful to use capitals) and the contents of
the file will be stored on disk and you will be returned to the
shell.
There are three modes that Vi may be in. There is
command mode, during which you can move the cursor
around the screen, and generally move from one part of the file to
another, deleting and altering text at the point of your cursor.
When you enter Vi, you always start in command-mode. Secondly,
there is colon-mode. This is necessary when you
wish to perform more complicated operations on your file that
cannot (easily) be done with simple keystrokes. Third, there is
input mode during which you can enter text.
To enter colon-mode, you must be in command-mode, so make sure
you are not entering text (type ESC if necessary). Then
type a colon (: ) (not followed by Return).
The cursor will immediately move to the bottom line of your screen
and will be preceded by a colon. At this stage, there are a few
colon-mode commands that you must know. If you make a mistake while
typing, then the command u (undo), either in
colon-mode or in command-mode, will correct it. If you accidentally
type a colon, you can return to command-mode by just pressing
Return. In the following discussion we assume that you are
in command-mode unless otherwise stated.
Try using Vi to create in file myfile just two
lines:
hello
there
After you have done this, and left Vi by typing ZZ ,
use the command ls to check which files you now have.
You should find:
$ ls
myfile
Now edit the file myfile again and remove the two
lines by typing dd twice to delete each one in turn.
Choose another document, such as a book or a newspaper, and copy a
couple of paragraphs into the file. Make sure that you enter them
correctly, using the Vi commands we have just discussed to make any
corrections. If the file fills more than one screen you can
'scroll' backwards and forwards through the file by typing
ctrl-U and ctrl-D
respectively.
Some other cursor-moving commands are useful:
^ (caret) moves the cursor to the start of the
current line,
$ (dollar) moves the cursor to the end of the
current line.
The file will contain words, which just as in English are
sequences of letters and/or digits:
w moves forward through the file to the
start of the next word,
e moves forward through the file to the next
end of a word,
b moves backwards to the start
of a word.
If you know that there's a word, or sequence of characters, in
the file that you wish to find, then Vi will search for that
string:
- typing
/ followed by the string you are looking
for, followed by Return, will look forwards in
the file for the string, and
- typing
? , rather than / , searches for
a previous occurrence of a string.
So, in order to search for the next occurrence of
hello , you should type
/hello
followed by Return. If a line is too long, you can
split it into two by positioning the cursor where you wish it to be
split, and using i or a to insert a
Newline character. If you have two lines you wish to join
to a single one, place the cursor on the first one and type
J (join).
There are also colon-mode commands for moving about the file.
For each colon-mode command you must press Return at the
end of the command:
:0 (digit zero) moves the cursor to the start of
the file,
:$ moves the cursor to the beginning of the last
line of the file,
:n moves the cursor to the beginning of
line n.
Practice the other commands listed above and those in Tables of
command mode and colon mode commands, and get
used to them. You will have to create many files, and it is
worthwhile getting used to the editor at this stage. Some sites
will support a command called vilearn (not POSIX)
which is a user-friendly program that teaches you how to use Vi,
and if it is available you may find it very helpful.
At this point, mention must be made of an editor called
ex . Strictly speaking, ex and Vi are the
same animal - think of Vi permanently in colon-mode so
that after each colon-mode command the cursor is prompted on the
bottom line by a colon, and you have ex . The command
visual to ex will turn it into Vi. If you
are in Vi and in command-mode the command Q (quit)
will put you permanently in colon-mode, namely in
ex .
|