Getting started with vi

To start the vi program on a UNIX system, type the following command on the UNIX command line:

vi filename

and press the RETURN key. (Replace filename with the name of the file you wish to edit.) For example, to edit a file named "program.c," type this command:

vi program.c

If program.c does not exist, vi will create the file. Your screen will look like the one below:

~
~
~
~
~
~
~
~
~
~
~
~
~
~
"program.c" [New file]

The ~ characters are placeholders indicating that a line doesn't exist.

If the file exists, you will see the contents of the file displayed on the UNIX terminal screen:

#include "theta.h" 
int main() 
{
int i; 
printf(" i theta0 theta1 inv(theta0,theta1)\n"); 

for (i=1; i<=32; i++) 
{
printf("%3d %6d %6d ",i,theta0(i),theta1(i));
                              
printf("%13d\n",invTheta(theta0(i),theta1(i)));
} 
} 
~
~ 
~ 
~ 
~ 
~ 
~ 
~ 
~ 
"program.c" 10 lines, 236 characters

Moving to the part of the file you want to edit

When you first start vi, you begin in command mode. That is, anything you type is interpreted as a vi command and will not become part of the file. If you are editing a file that already exists, you'll need to move the cursor to the place where you want to add text. There are many ways to move the cursor in the file. To get started, use the keys listed in the table below to move the cursor:

Basic Cursor Movement
Move left one character h left arrow key Lowercase h, j, k, and l are the standard UNIX cursor movement keys used in many other UNIX programs.

Remember that case matters; that is, H and h are separate commands.

More cursor movement keys are listed in the vi tip sheet.
Move up one line j up arrow key
Move down one line k down arrow key
Move right one character l right arrow key
Moving word by word
Move to the next "end-of-word" e Lowercase e and b move the cursor to the next end of word or the previous beginning of a word, respectively. Punctuation counts as a word.
Move to the previous "end-of-word" b
Move to the next "end-of-word" including punctuation as part of the word E Uppercase E and B move the cursor to the next end of word or the previous beginning of a word, respectively. Adjacent punctuation is considered part of the word.
Move to the previous "end-of-word" including punctuation as part of the word B
Additional information on moving the cursor is included in the vi tip sheet.

Modifying the file

Once you have moved the cursor to the location where you want to change or add text to your file, you'll need to use one of these commands to switch from vi command mode to insert mode. In insert mode, anything you type becomes part of the file you are editing.

Basic Insert Mode Commands
Add text immediately after the current cursor position a Case matters; that is, A and a are separate commands.

When you are done adding or inserting text, use the ESC key to exit vi insert mode and return to vi command mode.

More information is available on the vi tip sheet and by following some of the links on the More information page.
Insert text immediately before the current cursor position i
Add new text at the end of the current line A
Insert text at the beginning of the current line I
There are many vi commands that will allow you to delete, replace, modify, and insert text into your file. These commands are described in the tutorials listed on the More information page and on the vi tip sheet.

Editing and saving the file

To edit your file, use the commands listed above (and more advanced commands listed in the files linked from the More information page and on the vi tip sheet). Remember, use the ESC key to leave insert mode and return to command mode.

To save your file periodically while you are editing it, use this key sequence:

ESC :w (Save File)
ESC Press the ESC key to make sure you are in command mode.
:w : gives you a command line on which you can type commands. The w command "writes" (saves) your file.

When you are done editing your file, use this key sequence to save your file and quit vi:

ESC  :wq (Save File and Quit vi)
ESC Press the ESC key to make sure you are in command mode.
:wq : gives you a command line on which you can type commands. The w command "writes" (saves) your file, and the q command "quits" vi and returns you to the UNIX command prompt.

There are many powerful, advanced uses of the vi editor. Review the files linked from the More information page to learn how to use vi to perform complex editing tasks.