Using the Vi Text Editor

Using the Vi Text Editor

    Vi is a full-screen text editor that is almost universally available on 
UNIX-based computer systems.  There are also versions of vi for the IBM 
PC (and compatibles) and the Macintosh. Vi is useful for editing program files,
entering data, composing mail messages, and plain text editing.  

    Please note: UNIX is a case-sensitive operating system; when you enter 
the commands listed below, you must type them in the appropriate case.

    In this document the word current, when used in reference to some unit
of text, identifies where the cursor is -- e.g., current character, current
word, current line.

    Optional variables will appear in square brackets [].

===========================================================================

Starting vi

    To edit a file with vi, type any of the following commands at the UNIX 
system prompt.

vi              create a new, unnamed file

vi filename     edit an existing file or create a new file named filename

vi -r filename  recover an edit session that was interrupted by a 
                system crash (this may not recover all of the changes 
                you made to your file in your last editing session)

vi -R filename  open the file in Read Only mode

===========================================================================

Vi modes

    Vi has two modes: command mode and input mode.  While in input 
mode, everything you type is inserted into your document (including 
command mode commands).  While in command mode, everything you 
type is executed as a command to edit your document.  To change from 
input mode to command mode, press the Esc key.  There are several ways 
to change from command mode to input mode that are listed below.

===========================================================================

Entering input mode

    All of the following commands will change your mode from command 
to input.

i       enter input mode before the cursor

I       enter input mode at the start of the current line

a       enter input mode after the cursor

A       enter input mode at the end of the current line

o       create a new line below the cursor and enter input mode on it

O       create a new line above the cursor and enter input mode on it

===========================================================================

Moving around within the current text window

    The following commands allow you to move around the current text 
appearing on your screen.  Most of the commands can be preceded by a 
number, which will move the cursor that number of characters, words, or 
lines.  For example, 4j would move the cursor down four lines.  When a 
number is not specified, one space or line is assumed.

     Please note: be careful when using the arrow keys on your keyboard as 
they often do not work properly with vi and may cause unpredictable 
results.

[num]h          move num spaces to the left

[num]Backspace  move num spaces to the left

[num]l          move num spaces to the right

[num]space bar  move num spaces to the right

[num]j          move down num lines in the same column

[num]k          move up num lines in the same column

[num]Return     move to the beginning of the numth line down

[num]+          move to the beginning of the numth line down

[num]w          move right to the beginning of the numth word 
                following the current word

[num]e          move right to the end of the numth word counting the 
                current word

[num]b          move left to the beginning of the numth previous word

^ or 0 (zero)   move to the beginning of the current line

$               move to the end of the current line

H/h             move to the beginning of the first line on the screen

M/m             move to the beginning of the middle line on the screen

L               move to the beginning of the last line on the screen

===========================================================================

Moving around the entire document

    These commands allow you to change the portion of your document 
that appears on your screen and to move around the document quickly.

Ctrl-f          move forward a screen

Ctrl-b          move back a screen

Ctrl-u          move up half a screen

Ctrl-d          move down half a screen

numG            move to the specified line 
                (use Ctrl-g to display the current line number) 

:num            move to the specified line 

G               move to the last line in the file

:$              move to the last line in the file

===========================================================================

Searching for text

    Vi has search commands which allow you to find a particular section of 
your document quickly.

/pattern        search forward through the document for the next 
                occurrence of the pattern

?pattern        search backward through the document for the next 
                occurrence of the pattern

n               repeat search in the same direction

N               repeat search in opposite direction

f(char)         search forward from the cursor in the current line 
                for a single character (char)

F(char)         search backward from the cursor in the current line
                for a single character (char) 

;               repeat single character search in either direction (f or F)

===========================================================================

Deleting text

    The following commands allow you to delete single characters, words, 
or whole lines of text with a single command.  Most of the commands can 
be preceded by a number to delete more than one character, word, or line 
at a time.  For example, 3dd would delete three lines at once.  When the 
number is not specified, one character/word/line is assumed.  These 
commands put the deleted text into the buffer, which can then be placed 
elsewhere in the document by using the put commands explained later in 
this guide.  While in input mode, you can use Backspace to correct mistakes 
on the current line of text.  

x          delete character under cursor

[num]x     delete num characters from cursor forward

X          delete character before the cursor

[num]X     delete num characters before the cursor

[num]dw    delete num words starting with the current word from 
           the cursor on

D          delete text from the cursor to the end of the current line

[num]dd    delete num lines starting with the current line

===========================================================================

Copying ("yanking") and pasting text

    Vi allows you to copy or delete blocks of text and place them elsewhere 
in your document.  The put commands below will insert into the document 
any text which has been put into the buffer by the delete commands above 
or the yank (copy) commands below:

[num]yw    copy num words into the buffer, starting with the current 
           word from the cursor on

[num]yy    copy num lines into the buffer, starting with the current 
           line 

[num]Y     copy num lines into the buffer, starting with the current line 

p          put any text in the buffer after or below the cursor

P          put any text in the buffer before or above the cursor

===========================================================================

Changing text

     Vi has several commands that allow you to change an existing piece of 
text without first deleting it; the change commands overtype the current 
text.

[num]cw    change num words, starting at the cursor in the current 
           word--the word(s) will be replaced by any text you type until 
           you press Esc

C          change the current line from cursor to end--the current line 
           will be replaced by any text you type until you press Esc

r          replace the current character (you do not need to press Esc)

R          edit the current line from cursor to end in typeover mode--you 
           will remain in typeover mode until you press Esc (you can press 
           Return to insert more lines before pressing Esc)

s          replace the current character--the current character will be 
           replaced by any text you type until you press Esc

S          replace the entire current line--the current line will be replaced 
           by any text you type until you press Esc

===========================================================================

Saving your file and quitting vi

   There are several ways to save your document and exit vi.  Be sure to 
use the save command often when editing an important document.

:w              save changes (i.e., write) to your file

:w filename     save changes (i.e., write) to the file specified (must be 
		used if saving for the first time)

:q              quit vi (when you haven't made any changes)

:q!             quit without saving changes

ZZ :wq          save changes to file and then quit (if file already given a 
		name)

===========================================================================

Marking text

    Marking sections of text allows you to move around a large document 
quickly and easily.

m(char)         mark the document at the current character with a single, 
                lowercase letter (char)

'(char)         move to the beginning of the line marked by char

`(char)         move to the character marked by char (note: NCSA telnet for 
                the Macintosh by default remaps ` to Esc)

===========================================================================

Executing UNIX system commands (escaping to the shell)

    Vi has two commands which make it easy to execute UNIX commands 
without exiting vi.

:!cmd           execute a single command (cmd) and return to vi

:sh             start up a new UNIX shell -- to return to vi from the shell, 
                type exit or Ctrl-d

===========================================================================

Setting editor options

    Vi has several options which affect the way vi works and which alter 
your editing environment.  These options can be set by hand from within vi 
(must be reset every time you use vi), or they can be made permanent (i.e., 
set automatically) by creating a .exrc file and including the commands 
there or setting the EXINIT environment variable in your .login or .cshrc 
file.  Use the set all command to get a list of all of the options; for 
explanations of those options, consult the manual listed at the end of this 
handout.

:set               list the currently set editor options

:set all           list all of the editor options

:set option=value  set an option that takes a numeric or string value

:set option        turn on a toggled option

:set nooption      turn off a toggled option
 
===========================================================================

Miscellaneous

u                  undo the last change

U                  undo the changes made to the current line

.                  repeat your last command again

``                 return to your previous position

''                 return to the beginning of the line at your previous 
                   position in the document

Ctrl-l             redraw the screen if it has been altered by output from 
                   some other program or a transmission error (such as a 
                   talk request)

Ctrl-g             print the line number of the current line and how many 
                   lines are in the document

%                  show matching (), {}, or [] when the current character is 
                   one of the characters

[num]J             join num lines together, including the current line 
                   (delete the Returns between the lines)

:r (filename)      read a file into the document below the current line

:e (filename)      edit a new file

===========================================================================