OSU Navigation Bar

The Ohio State University

Department of Statistics

Cockins Hall
rollover image OSU Statistics
            Home

design element

OSU Statistics

Home

News

Research & Consulting Groups

People

For Visitors

For Prospective Students

For Current Students, Staff & Faculty

Contact Us



rollover image

For Current Students & Faculty

rollover image

Courses

rollover image

Links

rollover image

Computer Support

rollover image

Internal Documents

rollover image

webmail

Using vi

QUICK GUIDE TO vi ON UNIX

Please note that UNIX commands are case sensitive. Be sure to type commands exactly as they are shown in upper or lowercase.

TWO UNIX EDITORS

vi is one of two supported editors available on the Statistics UNIX system; emacs is the other. To create or edit any files on the UNIX system, or to post messages to any of the more than 700 bulletin boards and newsgroups available through the rn or Pnews programs, you need one of the editors.

STARTING VI

To invoke the vi editor, type vi at the UNIX shell prompt. If you do not indicate a file name, vi will display a blank screen with the number 1 at the top line and subsequent lines starting with the tilde (~) character. To begin and name a new file at the same time, or to edit a file already created, type vi filename. As you input text and press return/enter at the end of each line, the ~ on each line is replaced with the line number.

Esc + COMMANDS

Performing any editing functions, such as moving the cursor without using the arrow keys, inserting and deleting text, changing or overwriting text, saving files, and exiting the editor, is at least a two-step process. Every vi command is initiated by pressing the esc key first.

CURSOR MOVEMENT

With cursor movement, for example, you can use the arrow keys to move around in your file or you can use the following vi commands. The commands with (n) enable you to specify an optional number of characters, words, etc, to move past; the default is one unit.

(n)h

back (n) character(s)

(n)j

down (n) line(s)

(n)k

up (n) line(s)

(n)l

forward (n) character(s)

H

start of top line of screen

M

start of middle line of screen

L

start of last line of screen

G

start of last line of file

(n)G

start of line (n)

0

(zero) start of current line

$

end of current line

(n)w

forward (n) word(s)

(n)b

back (n) word(s)

e

end of word

^f

forward one screen

^b

back one screen

^d

down half screen

^u

up half screen

(^=control key)

TYPING/INSERTING TEXT

To start a file after invoking vi, press esc, type i or a, then begin typing. Always remember to press the enter/return key at the end of each line; (Note: if you have accessed vi through the rn or Pnews program and you are editing a message file to be posted to a newsgroup, insert a blank line as the first line of your message and begin typing at line 2.)

As with cursor movement, vi provides different ways to insert text in addition to the i command. These commands, which must be preceded by esc, appear in the following list.

i

inserts text before cursor

a

adds text after cursor; does not overwrite text

I

inserts text at start of line

A

adds text to end of line

r

overwrites character under cursor

R

overwrites characters until end of line or until esc

o

(alpha o) opens new line after current line to type text

O

(alpha O) opens new line before current line to type text

DELETING TEXT

To correct a mistake, position the cursor in the appropriate position, press esc, then enter one of the following commands:

(n)dd

deletes (n) line(s)

(n)dw

deletes (n) word(s)

D

deletes from cursor until end of line or esc

(n)x

deletes (n) character(s)

X

deletes previous character

SAVING A FILE

When you finish editing your text, you can save the file in several ways. Saving a file is accomplished through one of the file management functions that are executable only on the bottom line of the screen. To access the bottom line, press esc and type a colon (:), which displays at the bottom line. Then type one of the commands from this list, then press return/enter:

BOTTOM-LINE COMMANDS

:w

writes changes to file and continues edit session

:wq

writes changes to current file and quits edit session

:w! filename

overwrites another file; default is current file

:q

quits edit session with no changes made

:q!

quits edit session and discards changes

:n

edits next file in a list when vi is opened specifiying more than one file

:f oldname new

changes name of current file to new name

:r filename

inserts another file at current cursor position

COMPLETING A FILE; EXITING VI

The bottom-line commands provide several ways to end the editing session. Using any of the commands from the list above, you can exit the current file saving your changes, exit without saving changes, or move to the next file if you specified a list of file names when you invoked vi.

EXAMPLE PROCEDURE

Now let's go through the procedure of creating, editing, and saving a file using vi and a few of the more common commands. We'll begin by creating the file named "announcement" that appears in the previous examples. Type the command in this form at the UNIX shell prompt:

{username 52} vi announcement

The cursor appears to the right of the number 1 at the top line. Press esc followed by the command i, and begin typing. On the bottom line in the right-hand corner a message tells you that you are in INPUT MODE. When you finish entering text.

To make any changes to the text, use the arrow keys to position the cursor or press esc and type nj to move the cursor down to the appropriate line (where n is the number of lines you want to jump over). Press esc followed by one of the commands to insert, add, delete, or overwrite text. When you are ready to save the file, press esc followed by : to access the bottom command line. Type w or w filename and press return/enter to write (save) this version of the file and return to the current edit session. Type wq or wq filename to save the file and exit the edit session.

OTHER FEATURES

You also can make changes to a file by using some of the specialized commands, shown below, which should be self-explanatory. As with the other commands, these also are preceded by esc.

(n)cc

changes (n) characters on line(s) to end of line or until esc

cw

overwrites characters in word to end of word or until esc

(n)cw

overwrites characters of the next (n) words

c$

overwrites text to line end

ct(x)

ovewrites text up to occurrence of specified letter

C

overwrites remaining text on current line or until esc

J

joins current line and next line

u

undoes last command on current line

.

repeats last command given

s

substitutes text for current character

S

substitutes text for current line

:s

substitutes new word(s) for old

&

repeats last substitution (:s command)

(n)yy

copies (yanks) (n) lines to buffer

(n)yw

copies(yanks) (n) words to buffer

p

puts yanked text after current line or cursor

P

puts yanked text before current line or cursor

ONLINE HELP

If you need assistance, you can get help from the online UNIX manual by typing man vi at the UNIX shell prompt.



If you have trouble accessing this page, or need an alternate format contact webmaster@stat.osu.edu.