Tutorial: The vi Editor

Dr. Thomas E. Hicks
Computer Science Department
Trinity University


What Is An Editor
VI is an text editor available on almost all UNIX systems. All of the VI editor commands are case sensitive!

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.


Why Do We Use VI?

 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.

  1. It is on every UNIX system - all the same.
  2. Since it is not a full screen editor, it can be used for modem remote connections
  3. It works on all monitor and computer hardware combinations
  4. It is an extremely powerful editor. It has many more options than those specified here. See A Practical Guide to the UNIX System by Mark Sobell for additional information.
  5. It is used by many mail programs.
  6. It is inexpensive.

vi FileName - Open/Create A Text File With The VI Editor

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!


Two Modes - Input Mode & Command Mode

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 - Command Mode - To Enter Input Mode

 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.


:## - Command Mode - Move To Line ##

 :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 - Command Mode - Move To Last Line 

G        <-- When in the command mode, moves to the last line of the file


What Is A Line?

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 - Command Mode - To Enter Input Mode

 A        <-- When in the command mode, moves your cursor to the end of the current line and places you in the input mode.


<ESC> - Input Mode - To Return To Command Mode

<ESC>         <-- Hitting the Escape Key returns the user to the command mode.


x - Command Mode - Delete The Current Character

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 - Command Mode - Delete The Current Line

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 - Command Mode - Undo

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- Save The File & Exit The VI Editor

<ESC> :wq            <-- May be used from either the command mode or the input mode to save the file and exit the editor.


<ESC> :w- Command Mode - Save The File & Remain In The VI 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!- Abort - Cancel All Changes To The File & Exit The VI 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> <DownArrow><LeftArrow><RightArrow>- Command Mode - Using Arrow Keys

<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.


/??? - Command Mode - Move To Next Occurrence of String ???

 /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 - Command Mode - Yank/Copy ## Lines To Imaginary Clipboard

yy        <-- When in the command mode, copies the cursor line to the imaginary clipboard.
5yy      <-- When in the command mode, copies the cursor line and next four lines 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!


P & p- Command Mode - Paste Imaginary Clipboard Contents

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.

---------------------------- Optional/Extra Credit --------------------------


J- Command Mode - Join Lines

J      <--   When in the command mode, joins next line to current line


^F- <CTRL> F - Command Mode - Screen Forward

^F      <--   Move forward one screen.


^B- <CTRL> B - Command Mode - Screen Backward

^B      <--    Move backward one screen.


:r FileName- Command Mode - Merge Files

: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.


Possible Exam/Quiz Questions - VI

May be accessed through URL: http://www.cs.trinity.edu/~thicks
May also be accessed through URL: http://carme.cs.trinity.edu
This Document May Not Be Printed or Reproduced Without Written Permission.
 2003 Copyright : Dr. Thomas E. Hicks
Permission granted : Professional Educators & College Students may print one copy of this page!

Dr. Thomas E. Hicks

Computer Science Department    
Trinity University

"Dr. Web"