vi Training Session
Objective
This session will cover the basic features of vi; upon completion you should be able to perform the following:
Insert one character, one line, and multiple lines
Change one character, one word, until desired stop
Delete one character, one word, to end of line, one line, and multiple lines
Search forward or backwards search for a text string
Save and Exit saving changes, abandon changes, and/or exit document
Assumptions
This document is designed to be a tutorial used from home. The process assumes the operating system is Windows 95 or higher, the Internet browser is running and is connected to an Internet service provider (ISP). Please verify at least blinking cursor, VT100 arrows and VT-100/ANSI are set in the terminal preferences.
Setup
Start the telnet session to grace
Click on START
Click on RUN
Enter telnet grace.evergreen.edu in the Open: box
Click on OK or press <Enter>
Verify the telnet terminal preferences
Click on Terminal the box should be checked
Click on Blinking Cursor the box should be checked
Click on VT-100/ANSI the circle should contain a dot
Click on OK
Logon to "grace" with your provided username and password, change directories to "web" and edit a file named test.txt using vi, for example
Red Hat Linux release 6.0 (Hedwig)
Kernel 2.2.5-22 on an i686
logon: pyesha17
Password: *******
Last login: Wed Oct 4 16:34:15 from 12-163.009.popsite.net
Linux grace.evergreen.edu 2.2.5-15 i686
Welcome to grace.evergreen.edu, Evergreens linux server
8/14/00
The first phase in upgrading our linux resources has been completed. Users
spaces have been transitioned to a new machine with much larger storage.
[pyesha17@grace pyesha17]$ this is the original prompt
$ cd web change directories
$ pwd verify location, personal "web" directory
/usr/users3/pyesha17/web
$ vi test.txt edit file named "test.txt"
Insert Text
This will open up an edit window and since the file did not previous exist, position 1 of each line will display the tilde (~) character. vi is opened in command mode. This means vi is waiting to be told what to do. The basic insert commands are
i insert text right at cursor
I insert text at beginning of line
a insert text after cursor location
A insert text at end of line
o insert text in new line below cursor
O insert text in new line above cursor
Once selected the insert mode will remain active until the <escape> key is pressed. Press "i" and notice how "-- INSERT --" is displayed at the bottom of the screen. Enter the following text
The quick blue <enter>
fox over <enter>
<enter>
lazy dog. <enter>
<escape>
The cursor should be displayed under the "l" of the "lazy dog." and "-- INSERT --" is no longer displayed. Use the arrow keys to position the cursor on the <space> before the word "over". Press "a" and enter "jumped<space><escape>". The screen should now appear as
The quick blue
fox jumped over
lazy dog.
~
Use the arrow keys to position the cursor on the "d" of the word "dog". Press "I" the cursor should be at the beginning of the line and "-- INSERT --" is displayed at the bottom of the screen. Enter "the<space><escape>". "-- INSERT --" is no longer displayed at the bottom of the screen and the text should now appear as
The quick blue
fox jumped over
the lazy dog.
~
The cursor should be in front of the word "lazy", enter "A". The cursor should now be at the end of the line and "-- INSERT --" displayed at the bottom of the screen. Please enter the following lines
<enter>
This is line 2<enter>
Thia is line 4<escape>
"-- INSERT --" is no longer displayed at the bottom of the screen and the text should now appear as
The quick blue
fox jumped over
the lazy dog.
This is line 2
Thia is line 4
~
Use the arrow keys to move the cursor somewhere on the "This is line 2" line. Press "o", there should be a blank line below "line 2" and the cursor is in position 1. Enter
This is line 3<escape>
"-- INSERT --" is no longer displayed at the bottom of the screen and the text should now appear as
The quick blue
fox jumped over
the lazy dog.
This is line 2
This is line 3
Thia is line 4
~
Use the arrow keys to move the cursor somewhere on the "This is line 2" line. Press "O", there should be a blank line above "line 2" and the cursor is in position 1. Enter
This is line 1<escape>
"-- INSERT --" is no longer displayed at the bottom of the screen and the text should now appear as
The quick blue
fox jumped over
the lazy dog.
This is line 1
This is line 2
This is line 3
Thia is line 4
~
Change Text
The basic change commands are
r change one character
R continue to change characters until <escape> is pressed
cW change one word
Use the arrow keys to place the cursor on the "a" in "Thia". Enter the following key strokes "rs". The "a" has been changed to "s". The screen should now read as
The quick blue
fox jumped over
the lazy dog.
This is line 1
This is line 2
This is line 3
This is line 4
~
Use the arrow keys to place the cursor on the "b" in "blue". Enter the following "cWbrown<escape>". The word "blue" has been changed to "brown". The screen should now read as
The quick brown
fox jumped over
the lazy dog.
This is line 1
This is line 2
This is line 3
This is line 4
~
Use the arrow keys to place the cursor in position 1 of the blank line after "fox jumped over", press "R" and enter the following
This is a blank line<escape>
Text was added to the blank line. The screen should now read as follows
The quick brown
fox jumped over
This is a blank line
the lazy dog.
This is line 1
This is line 2
This is line 3
This is line 4
~
Delete Text
The basic delete commands are
x delete one character
dw delete one word
d} delete to end of paragraph
dG delete to end of document
D delete to end of line
dd delete current line
Use the arrow keys to place the cursor on the first "i" in "This is a blank line". Press "x" and the "i" in "This" has been deleted. Move the cursor to the beginning of the line and enter "dw" the word "Ths" was deleted. Enter "D" and the remaining text on the line is deleted. Enter "dd" and the entire line is removed. The screen should now read as
The quick brown
fox jumped over
the lazy dog.
This is line 1
This is line 2
This is line 3
This is line 4
~
The "d}" and "dG" are commands to use very sparingly. You may practice them on your own but dont use them on an important file until you have developed a stronger understanding of vi.
Searching
The basic search commands are
/pattern<enter> search forward for a pattern
?pattern<enter> search backward for a pattern
n repeat previous search in direction of initial search
N repeat previous search in reverse direction
Enter ":1" to move the cursor to line 1 position 1 of the file. Enter "/line<enter>". The cursor is now positioned on the word "line" in "This is line 1". Press "n" and the cursor should now be positioned on the word "line" in "This is line 2". Press "N" and the cursor has moved backed to "line" on "This is line 1". The "?" command functions in the same manner as the "/", only in the reverse direction.
Saving and Exiting
The basic save and exit commands are
:w [file] save file in [file] and remain in the current file
:wq [file] save file in [file] and exit from editor
:q exit from editor
:q! exit from editor and discarding changes
ZZ save changes and exit from editor
Enter "ZZ" to exit vi and save the file or ":q!" to exit vi and discard changes.