Editing files using VI


The vi Editor

A standard editor in Unix operating systems is "vi", pronounced vee-eye. Like Unix, vi is cryptic, designed to minimize typing. You can use other editors if you wish, but you will be tested on vi in your class quizzes and exams.

Simply enter       vi filename
to call up vi. If the file does not exist, it will be created. If the file does exist, it will be read into the edit buffer.

A few notes on vi before we get into the commands:

To enter insert mode, position your cursor at the desired location and enter one of the following characters. The letter you type will not be displayed because you are not yet in insert mode.
 

i insert before cursor position 
a insert after cursor position 
O insert before current line 
o insert after current line 

To end any insertion, type the Esc key.
If you are not sure, type the Esc key anyway, it won't hurt.

To move around in the file:
 

Arrow keys. You can use the keyboard arrow keys.
Ctrl f moves you forward one 'screen' at a time 
Ctrl b moves you backward one 'screen' at a time 
G moves you to the end of the file 
/search_string/ If you want to go to the location in a file where a specific word or phrase is located, you can put that word inside slashes "/" and vi will take you to that spot. Note that it will take you to the first occurrence of that search string. If that's not the one you want, enter n and vi will take you to the next occurence of the string.

To replace character(s):

r replace the current character 
R replace text starting from the cursor position 

Replacement initiated by R must be terminated by typing the Esc key.

To delete character(s):

x delete the current character 
[n]dd delete the current line(s) - the line(s) are moved into the edit buffer.
e.g. dd deletes the current line
e.g. 3dd deletes 3 lines starting at the current line.
In other words, precede dd with a number to specify the number of lines to delete.

To "cut/copy & paste" within the vi editor:
Use vi commands to cut or copy lines into the edit buffer and then move to the desired location in your file and enter the vi paste command.

[n]dd delete (cut) the current line(s) (See the description just given.)
[n]yy yank (copy) the current line(s) - a copy of the lines is placed into the edit buffer.
e.g. yy yanks the current line
e.g. 3yy yanks 3 lines starting at the current line.
In other words, precede yy with a number to specify the number of lines to yank.
p paste the line(s) from the edit buffer after the current cursor position.
P paste the line(s) from the edit buffer before the current cursor position.

To "cut/copy & paste" from outside the vi editor:

Setup your PC
When you are in a browser like Netscape or another PC application, use your mouse to highlight the text that you want to copy into the clipboard.
Select Edit -> Copy from the application's menu.
Now activate your Telnet window, with vi active.
:set noai
This vi command translates to "set no autoindent".
You'll find that if you don't set this, when you do your paste, the text will become very messy as vi attempts to automatically indent lines.
i
Get into insert mode. (First make sure you are at the desired location in your file.)
Edit -> Paste
Select this option from the Telnet menu. It takes the contents of the Windows clipboard and pastes it into your file.

To exit the VI editor:

:q! exits vi without saving changes 
:wq saves the file and then exits 
ZZ saves the file and then exits 

Example of Creating a File with vi.

Here is an example of how to create a file called "header.txt" using vi.

Start by entering:   vi header.txt

In vi, you are either in command mode or insert mode. You are now in command mode so that every letter you type is interpreted as a command.

To start entering text, type the letter "i" to get into insert mode. You will remain in insert mode until you type the ESC key; otherwise, everything you type is entered into your file.
NOTE: do not use the arrow keys while you are in insert mode.

While in insert mode, enter the following lines. Do not worry about mistakes, you can correct them later.

     /***********************************************************
        Name:
        Student Number:
        Assignment Number:
        Program Creation Date:
        Purpose of the Program:
     ***********************************************************/

Type the Esc key to get out of insert mode. You are now in command mode and can make corrections/additions.

For example, you could use the arrow keys to move the cursor to the colon ":" following "Name". Then type the "a" key to get into append mode so that you can add your name to that line.
Remember to type the Esc key to get out of insert mode.

When you are finished making all changes, be sure to write the contents of the edit buffer and quit vi by entering:

 :wq 

You will notice that as soon as you type the colon, the cursor will move down to the bottom left corner of the screen. If this does not happen, perhaps you are still in insert mode. Just type the Esc key again to get back to command mode.


For more information on the vi editor, refer to the 5.2. VI EDITOR section of the CS Dept Users Manual.

The Users Manual can also be accessed from the CS Dept Home page, following the links: Computing Information - User's Manuals.
 
Copyright: Department of Computer Science, University of Regina.