Tutorial: The vi Editor
Dr.
Thomas E. Hicks
Computer Science
Department
Trinity University
editor - a software program used to create, display, and/or edit text files. Editors do not permit the normal word processing features such as margin control, font selection, underscore, bold, etc. vi is an example of one of the editors available on all UNIX platforms. Edit is an example of an editor available on all IBM DOS platforms. Simple Text is an example of an editor available on all Macintosh platforms.
The vi editor is a line editor. It is far less friendly
than the editors that many of us are used to in the micro environments.
vi FileName <-- to start the vi editor on any UNIX system. If the file exits, open it! If the file does not exist, create it!
vi ThingsToDo <-- to start the vi editor with file ThingsToDo. If the file exits, open it! If the file does not exist, create it!
vi Hello.f <-- to start the vi editor with file Hello.f. If the file exits, open it! If the file does not exist, create it!
The vi editor has a command mode and an input mode.
The program begins in the command mode. The command mode
is used to issue high level commands.
The input mode is used to enter information.
i
<-- When in the command mode, places you in the input mode at the location
of your cursor. Once in the input
mode, you can enter new information.
:1
<-- When in the command mode, moves to line 1 of the file
:17
<-- When in the command mode, moves to line 17 of the file
G <-- When in the command mode, moves to the last line of the file
Note that the vi editor automatically wraps around when
you get to the right end of the document. It will even wrap in the middle
of a word. The logical line is terminated with a carriage return; a line
may span several physical lines. The numeration, copy, and deletion refers
to the logical lines. In the document below are three lines; the return
is specified!
Good Morning
<Return>
I am not thrilled with the vi editor.
Despite the fact that it is a very powerful and complete editor, it lacks
much of the functionality associated with today's GUI micro editors. This
editor is used my some mail systems. It is available on all UNIX systems.
It is free! <Return>
All that glimmers is not gold! <Return>
:3
<-- When in the command mode, moves to line 3 of the file. Starts with
"All that ..
:2
<-- When in the command mode, moves to line 3 of the file. Starts with
"I am ..
A <-- When in the command mode, moves your cursor to the end of the current line and places you in the input mode.
<ESC> <-- Hitting the Escape Key returns the user to the command mode.
x
<-- When in the command mode, deletes the character beneath the cursor.
3x
<-- When in the command mode, deletes the character beneath the cursor
and the next two characters.
13x
<-- When in the command mode, deletes the character beneath the cursor
and the next twelve characters.
dd
<-- When in the command mode, deletes the cursor line.
3dd
<-- When in the command mode, deletes the cursor line and the next two
lines.
13dd
<-- When in the command mode, deletes the cursor line and the next twelve
lines.
u <-- When in the command mode, undoes the previous step (if possible).
:17
<-- When in the command mode, moves to line 17.
dd
<-- When in the command mode, delete line 17.
u
<-- When in the command mode, undelete line 17.
:17
<-- When in the command mode, moves to line 17.
5dd
<-- When in the command mode, delete lines 17, 18, 19, 20, & 21.
u
<-- When in the command mode, undelete lines 17, 18, 19, 20, & 21.
<ESC> :wq <-- May be used from either the command mode or the input mode to save the file and exit the editor.
:w
<-- May be used from either the command mode or the input mode to save
all of the current changes to
disk. The user remains in the editor.
<ESC> :q!
<-- May be used from either the command mode or the input mode to cancel
all changes to the file and
exit the editor.
<UpArrow>
<-- May be used from either the command mode to move the cursor up one
line.
<DownArrow>
<-- May be used from either the command mode to move the cursor down
one line.
<LeftArrow>
<-- May be used from either the command mode to move the cursor left
one character.
<RightArrow>
<-- May be used from either the command mode to move the cursor right
one character.
/Doc
<-- When in the command mode, move cursor to next occurrence of string
Doc
/1101
<-- When in the command mode, move cursor to next occurrence of string
1101
yy
<-- When in the command mode, copies the cursor line to the imaginary
clipboard.
Clipboard contents are kept for a very short period of
time. If using this for copy and paste, I recommend copy -> move to desired
location -> paste; do not do other steps between!
5yy
<-- When in the command mode, copies the cursor line and next four lines
to the imaginary clipboard.
p
<-- (Small p) When in the command mode, copies the
imaginary clipboard immediately after the cursor line.
P
<-- (Capital P) When in the command mode, copies the imaginary clipboard
immediately before the cursor line.
J <-- When in the command mode, joins next line to current line
^F <-- Move forward one screen.
^B <-- Move backward one screen.
:r FileName <-- To Merge Files
:r Clear.f <-- Copy File Clear.f into the current file at the location of the cursor.
:r Message1 <-- Copy File Message1 into the current file at the location of the cursor.