Introduction to the VI Text Editor

Table of Contents

  1. INTRODUCTION
  2. INVOKING VI
  3. ENTERING TEXT
  4. ISSUING AND UNDOING COMMANDS
  5. HOW TO MOVE THE CURSOR
  6. HOW TO DELETE AND CHANGE TEXT
  7. USING BUFFERS TO RETRIEVE DELETED TEXT
  8. HOW TO COPY AND MOVE TEXT
  9. HOW TO SEARCH FOR AND REPLACE TEXT
  10. HOW TO READ FROM AND WRITE TO A FILE
  11. HOW TO EDIT A SECOND FILE
  12. HOW TO SAVE AND EXIT
  13. OTHER USEFUL COMMANDS
  14. CUSTOMIZING VI
  15. ADVANCED TOPICS
  16. HOW TO GET HELP

INTRODUCTION

Vi (pronounced vee-eye), the standard screen-oriented editor provided with Unix operating systems, is the ``visual'' mode of the Ex line editor. Both Vi and Ex commands can be issued from within Vi, as you will see in the examples that follow.

INVOKING VI

To invoke Vi, type


vi filename

where filename is the name of the file or files to be created or modified. Pattern matching characters, such as * or ?, can be used when specifying the filename; Vi will be prepared to edit each file that is matched in alphabetical order. If filename exists, your screen will show the first screenful of the file; if the filename does not exist, you will see a screen similar to the one shown below.

Vi puts the contents of the file you specify into a temporary buffer; any changes you make during your editing session will only be made to this temporary copy (buffer). Changes will be made to your permanent file when you explicitly save to the file using a write command. (See the section of this document entitled ``How to Read from and Write to a File.'')

To create a new file named ``test.vi,'' type


vi test.vi

The Vi Screen

If the filename you give Vi does not exist, your terminal screen will look something like the following:

~
~
~
~
~
~
~
~
~
~
~
"test.vi" [New file]
 

The cursor is positioned at the top left of the screen. Each of the tildes (~) represents an empty line. The information at the bottom of the screen gives status information. Some Vi commands are typed using the left part of the bottom line; the right part of the bottom line is used to display a message informing you of the ``mode'' you are in.[*]

Vi Modes

Vi has two modes: command mode and input mode.

When you start your Vi editing session, you are in command mode. If you want to insert text, you first need to issue a command to put you into input mode. You cannot do anything in input mode except insert text. For instance, you cannot move the cursor while in input mode. This means that if you are in input mode and you want to move the cursor, you will have to end input mode and then issue cursor movement commands.

A typical editing session would consist of the following steps.

  1. Invoke the Vi editor. When the editing session begins, you are in command mode.
  2. Issue cursor movement commands to move the cursor.
  3. Issue an input command to put you into input mode.
  4. Insert text into the buffer.
  5. Get out of input mode and into command mode by pressing the escape key (represented by ESC in this document).
  6. Issue commands (such as cursor movement, delete or change commands).
  7. Repeat steps 1 through 6 until you are ready to save the buffer to a file and end your editing session.

ENTERING TEXT

Since Vi is a bi-modal editor, as was described above, anything you type while in command mode will be interpreted as a command, and anything you type while in input mode will become text in your file. There are several commands which will initiate input mode; pressing the escape key will end input mode.

Initiating Input Mode

Command

Meaning

Mode

a

append after cursor

APPEND MODE

A

append at end of current line

APPEND MODE

i

insert at cursor

INSERT MODE

I

insert at beginning of current line

INSERT MODE

o

open a line below current line

OPEN MODE

O

open a line above current line

OPEN MODE

All of the above commands will initiate input mode; return to command mode by pressing the escape key (ESC). If the computer or terminal issues a beep, you are already out of input mode and in command mode; it never hurts to press ESC more than once if you are not certain what mode you are in.

To insert text into a new file called ``test.vi,'' type i. Then type the lines as shown below. Use the backspace key (may be marked ) to correct errors as you type. Press the carriage return key at the end of each line.

This is a practice file to help me<CR>
learn how to use the Vi<CR>
editor. The more I practice, the better<CR>
I will get!<CR>
<CR>
Two paragraphs will give me enough to practice<CR>
moving the cursor, deleting text, moving and copying<CR>
text. ESC

Note that the last thing you enter is ESC, which ends input mode. If you now type CTRL-G (hold down the control key while typing the letter g), the screen will look like this:

This is a practice file to help me
learn how to use the Vi
editor.  The more I practice, the better
I will get!
 
Two paragraphs will be enough to practice
moving the cursor, deleting text, moving and copying
text.
~
~
~
~
~
~
"test.vi" [Modified] line 8 of 8 --100%--

If you prefer to enter text without having to press the carriage return key at the end of each line, issue the following command:


:set wrapmargin=10<CR>

The colon (:) causes the cursor to move to the bottom of the screen. Type the rest of the command, ending with a carriage return. Now Vi will automatically break the input lines when they get within ten characters of the right margin. This is called ``power typing.'' You need only press the carriage return key to start a new paragraph.

To turn power typing off, type


:set wrapmargin=0

For more information on setting options like wrapmargin, see the section on Customizing Vi.

ISSUING AND UNDOING COMMANDS

Issuing Commands

There are two types of Vi commands:

It is possible to accidently put Vi into line-editing mode; you will know this has happened when a colon and the cursor remain at the bottom of the screen after each command. To return to full-screen mode, enter the command:

vi<CR>

Undoing a Command

It is possible to undo a command if you remember to undo it right away! The following table summarizes the undo commands; additional commands to retrieve deleted text are shown in a later section.

Undo Commands

u

reverses effects of previous command that changed the buffer (commands a,i,d,c,m,co,s, discussed below)

U

returns current sentence to its previous state

:e!

undoes all commands issued since file was last written to disk

HOW TO MOVE THE CURSOR

The table below shows some of the commands you can use to move the cursor in Vi. You will probably find a half-dozen commands that you can remember and use regularly, but you might want to refer to this table from time to time to pick up an occasional new trick! Some commands require the use of the control key, which may be marked on your keyboard with CONTROL or CTRL (indicated with CTRL- in this document). Hold down the control key and type the letter that is specified. Although the letters are shown in upper case in this document, no shift is required for control sequences. For all other commands, the case of the letter is important and should be typed exactly as shown.

Cursor Motion Commands

h or CTRL-H or backspace or <-

left one character

l or space or ->

right one character

j or CTRL-J or CTRL-N or down arrow

down one line, same column

k or CTRL-P or up arrow

up one line, same column

0 or ^

beginning of current line

$

end of current line

+ or <CR>

beginning of next line

-

beginning of previous line

G

beginning of last line of file; if prefixed with a number, goes to beginning of specified line (use 1G to go to first line of file)

b

back one word

w

forward one word

e

end of next word

CTRL-D

scroll down in file a half-screen

CTRL-U

scroll up in file a half-screen

CTRL-F

forward one screen

CTRL-B

back one screen

H

beginning of top line of screen

M

beginning of middle line of screen

L

beginning of last line of screen

z<CR>

line-up; current line to top of screen

z.

current line to middle of screen

z-

line-down; current line to bottom of screen

)

forward a sentence

(

back a sentence

}

forward a paragraph

{

back a paragraph

``

go to previous location (two left single quotes)

%

go to matching bracket

Most Vi commands may be preceded by a number to specify the number of times to execute the command. For example:

3w

move cursor forward 3 words

3)

move cursor forward 3 sentences

36G

put cursor on (``go to'') line 36

HOW TO DELETE AND CHANGE TEXT

Deleting Text

The following table summarizes the commands to delete text:

dw

delete from cursor to end of current word

d$ or D

delete from cursor to end of line

dd

delete current line

d)

delete from cursor to end of sentence

d}

delete from cursor to end of paragraph

x

delete single character under cursor

.

repeat last command that changed the buffer

These commands may be preceded by a number indicating the number of words, lines, etc., that you want to delete. For example, the command 5dd will delete 5 lines.

Changing Text

The change commands put you into input mode, marking the end of the amount of text to be changed with a $. Once you have entered the new text, press ESC. The following table summarizes the commands to change text:

End input with ESC:

cw

change from cursor to end of word

c$ or C

change from cursor to end of line

cc or S

change line

c)

change from cursor to end of sentence

c}

change from cursor to end of paragraph

R

replace (overlay) characters on current line

No ESC needed:

r

replace single character at cursor

xp

transpose two characters

The change commands may also be preceded by a number indicating the number of words, lines, etc., that you want to change. For example, the command 3cw will mark three words for change.

USING BUFFERS TO RETRIEVE DELETED TEXT

The Nameless Buffer

The text that was most recently deleted is stored in a nameless buffer; generally it can be retrieved by using the put command. Lower-case p puts the deleted text below or after the cursor position; upper-case P puts the deleted text above or before the cursor. If the text is one or more lines, it will be put in the line below (p) or above (P) the current line. If the deleted text is a portion of a line, it will be retrieved into the current line either after (p) or before (P) the cursor. Text deleted with one of the change commands (such as cw or cc) is also stored in the nameless buffer and can be retrieved using one of the put commands.

Yanking Text

Like the delete and change commands, the yank commands store text in the nameless buffer. Unlike the delete and change commands, the yank commands do not delete text--rather they copy text to the nameless buffer. The following table summarizes the commands to yank text.

yw

yank from cursor to end of word

y$

yank from cursor to end of line

yy or Y

yank current line

y)

yank from cursor to end of sentence

y}

yank from cursor to end of paragraph

The yank commands may be preceded by a number indicating the number of words, lines, etc., that you want to delete. For example, 5yy will yank five lines.

Because the yank commands copy text to the nameless buffer, p or P can be used to bring back recently yanked text.

The Numbered Buffers

The nameless buffer only holds the last text that was deleted, changed or yanked. If you delete or change a large block of text, expecting it to be stored in the nameless buffer, and then, before retrieving the large block, you accidentally delete, change or yank some other text, the large block of text will no longer be in the nameless buffer. Instead, the most recently deleted, changed or yanked text will be in the nameless buffer. This can be especially frustrating if the last delete, change or yank placed a small chunk of text like a word or a blank line into the nameless buffer!

Fortunately, Vi provides a way to retrieve the large block of text. Vi saves up to nine deleted or changed blocks of whole lines in buffers numbered from 1 to 9. The contents of buffer 1 will often be the same as the contents of the nameless buffer; buffers 2-9 will contain previously deleted or changed text. (Yanked text is not put into the numbered buffers.) Vi only puts whole lines of text into the numbered buffers. The text in these buffers can be retrieved with the command:

"np

where n is a number from 1 through 9.

If you are not sure which numbered buffer to specify, you can retrieve the contents of all nine numbered buffers with the command:

"1p . . . . . . . . (use all eight periods to retrieve from all nine buffers)

Each time you type a period, the contents of the next higher-numbered buffer will be retrieved. If a buffer is empty, Vi will display the message Nothing in register n where n is the buffer number.

By inserting a u before the . , the restored text will be deleted. In this way, you can search for the text you want to retrieve, deleting the unwanted text as you go. Stop typing u. when you have found the text you want.

The Named Buffers

Vi also provides twenty-six named buffers. The named buffers are named with the letters of the alphabet. If you want to store text in a named buffer or retrieve text from a named buffer, you need to specify the particular named buffer as a part of your delete, yank or put command. Named buffers are specified as follows:

"n

where n is the name of the buffer. If the name is upper case, deleted or yanked text will be appended to the end of the current contents of that buffer. If the name is lower case, Vi will overwrite the current contents of the named buffer with the new text.

The specification of the named buffer precedes the appropriate yank or delete command so that the text will be put in the named rather than the nameless buffer. In the following example, four lines of text will be deleted and stored in the buffer named k.

"k4dd

See the section entitled ``How to Edit a Second File'' for an example of how to use named buffers to copy text between files.

The following table summarizes the commands used for retrieving text.

p

puts most recently deleted or yanked text below current line or after cursor in current line

P

puts most recently deleted or yanked text above current line or before cursor in current line

"np

puts contents of buffer n after current line; n can be 1-9 a-z or A-Z

"1p . . . . . . . .

retrieves text from all nine numbered buffers

 

HOW TO COPY AND MOVE TEXT

There are several ways to copy and move text. You can store the text in the nameless buffer and then retrieve it or you can describe a block of text with line numbers or symbols and use Ex commands; another method is described in the ``Advanced Topics'' section of this document.

Move the cursor to the place where you want to put the copy and enter either
p to put the lines after the current line

or

P to put the lines above the current line