Mastering the vi Editor
Vi Editor
The UNIX visual editor, vi, is a command-driven screen editor. It is
case-sensitive. Uppercase and lowercase letters are treated differently.
WQ is interpreted differently than wq. Using the vi editor, you can create
and modify text files. The initial vi screen is illustrated below:
The tildes (~) indicate nonexistent lines, which are not the same as lines
that contain blanks. When you enter vi your cursor will be positioned at
the upper left-hand corner of the screen and you will be in command mode.
Modes
Vi has two modes: 1) the command mode and 2) the insert or append mode.
When vi is executed you are placed in the command mode. In the command
mode, you can move around the file using the directional (cursor movement)
arrow keys or the forward search command (/word). To add text to the file,
you must leave the command mode and go into the append mode. TO do this,
ou can use commands such as A, a, I, i, O, or o. Once you enter on e of
these commands, you are in the append mode. These commands operate as
follows:
A appends at end of line
a appends after cursor
I inserts at the beginnin of line
i inserts before cursor
O opens a line above current line
o opens a line below current line
While you are in insert (append) mode, whatever you type is added to the
file until you press ESC to return to the command mode.
The ESC key is the essential component of the visual editor. Whenever you
are in doubt about where you are in vi - command or append mode - press
the ESC key. In the append mode, ESC moves you to the command mode. IN the
command mode, ESC produces a beep or flash, indicating you are already in
the command mode.
Creating and Editing
To create a file with vi, begin the procdess by typing vi filename. (If
youa re using elm to send mail, the elm program invokes vi for you.) The
screen clears and the initial vi screen with tildes (~) appears. You are
in command mode at this point. GO into the append mode and add your text.
If you make a typing mistake while entering the line use the delete key to
backup and correct it. (Remember, if you wish to use the directional arrow
keys you must press ESC and get into the command mode. A common error is
to try to move the cursor while you are in append mode instead of command
mode. If you do this, a string of strange characters such as ^K^K^L^L or
^[A^[B will appear on the screen. Should this happen, press ESC and use
the x command to delete the unwanted characters.) After you have entered
your text into vi, you must instruct vi to save it. TO save the file, you
must return to command mode by pressing ESC. In the command mode press the
coloe (:) key. A colon (:) will appear in the lower left-hand corner of
your screen. This line is called the vi command line and is reserved for
entering certain vi commands such as:
w writes the file to current filename
wq writes (saves) the file to current filename and quits (exits) work on
file
q quits (exits) work on a file if no changes or additions have been made
q! Quits (exits) work on a file but does NOT write changes
To edit an existing file, begin the process by typing vi filename. The
file will be displayed on the screen. The process then becomes the same as
indicated above with creating - change to apend mode, make your changes,
and save the file.
Saving and Exiting (Quiting)
To save a file in vi you must be in the command mode. From the command
mode, type a colon (:) followed by one of the following vi commands:
w writes the file to current filename
wq writes (saves) the file to current filename and quits (exits) work on
file
To exit (quit) the editor you must be in command mode. To exit without
saving changes type a colon (:) followed by one of the following vi
commands:
q quits (exits) if no changes or additions have been made
q! quits (exits) but does NOT write changes
Vi Command Summary
Mode-changing
ESC escape from text append (insert) mode and return to vi command
mode
:q quit work on a file if no changes have been made
:q! quit work on a file but do not write changes
:w write the current file to filename
:wq write changes and quit work on the file
cursor positioning
<- move cursor one space left
-> move cursor one space right
0 (zero) move cursor to first position of current line
$ move cursor to last position of current line
G move cursor to last line in file
NG move cursor to line number n
:n move to line number n
- move cursor to first character of next line
+ move cursor to last line displayed on the screen
H move cursor to top line displayed on screen
Display adjusting
Ctrl f scroll forward one screen
Ctrl b scroll backward one screen
Ctrl l refresh the screen
Edit commands
A append at end of line
a append after cursor
I insert at beginning of line
i insert before cursor
O open a line above current line
o open a line below current line
ESC terminate append (insert) mode
dd delete current line
ndd delete n number of lines
D delete reminder of line
x delete current cursor position
X delete back one character
Searching commands
/word search forward for word
n repeat previous search
Miscellaneous commands
:r file read in contents of file
!! repeat last system command
|