Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 16 Aug 1996 19:20:59 -0700 (PDT)
From:      Gary Kline <kline@tera.com>
To:        doc@freebsd.org
Cc:        kline@tera.com
Subject:   vi tutorial
Message-ID:  <199608170220.TAA09516@athena.tera.com>

next in thread | raw e-mail | index | archive | help

	Hi,

	Enclosed is the vi_tutorial that I've been distributing.
	So far at least 30 people have requested the original
	ASCII version.  The original was somewhat less featureful
	than this HTML draft.

	If you guys like this and wish to include it with the other
	documentation, I hereby give my permission.  ---I would like
	to see something like this for emacs....about which I have 
	little understanding. ---

	Anyway, enjoy!

	gary kline

	Encl:





<!doctype html public '-//IETF//DTD HTML//EN'>
<! Gary Kline et al., 15aug96>
<HTML>
<HEAD> 
<CENTER>
<H1>Using vi </H1>
</CENTER>
</HEAD> 

<P>
The vi text editor is a widely available <EM>vi</EM>sual, 
full-screen text editor.
vi is a powerful editor, but it is also old--and this shows.
vi was created at a time when terminals did not have arrow keys,
or even a control key.  Because of this, vi is modal:
it has several modes it can be in, with its behavior changing
depending on mode.
<BR>
<BR>
<BR>
<BR>
<BR>

<P>
Readers who are at workstation are asked to open a second window
and to tryout vi commands as they work through it.
<BR>
<BR>


<CENTER>
<H2>
Starting and stopping
</H2>
</CENTER>

<P>
To start up vi, change to your home directory and type 
<PRE>
% vi samp
</PRE>
This will start up vi editing a (theoretically) new file named
``samp.'' If samp already exists it will be loaded and displayed 
upon your screen.

<P>
Many different commands can be used to exit vi.  Typing 
<PRE>
:wq
</PRE>
will write the file to disk and quit.  
Typing
<PRE>
:q
</PRE>
will quit if no changes
have been made.  If changes have been made then you must type
<PRE>
:q!
</PRE>
to quit without saving changes.  (Note that when you type a colon and are
in command mode, the cursor will move to the bottom left corner of your 
screen.)
<P>
``ZZ'' ---without the quotation marks, of course---and note that 
the Z's must be upper case--
is a synonym for ``:wq''. 
<P>
These few commands are enough to suggest
the main frustration new users have with vi--there are many commands
to memorize, and many like ``ZZ'' make no sense at all.  They have been
added often as add-ons, and are simply part of the program.  At first,
it is probably best to find one way of doing something and sticking with
it.  You can and probably will add more skills as your go along.
<BR>
<BR>

<CENTER>
<H2>
Inserting text
</H2>
</CENTER>

<P>
Right now vi is in <STRONG>command mode</STRONG>. Any key entered 
is a command to vi.
<P>
Try typing ``i'' to enter <STRONG>insert mode</STRONG>.  vi inserts all 
non-control characters typed while in insert mode into the document.
Some characters in this mode have special meanings; the backspace key,
for example, backs over mistakes. 
<P>
<EM> You cannot, however, use the arrow keys while in insert mode!  </EM>
Try typing something, like the first four
lines of the pledge of allegiance if nothing else comes to mind.
When you are through typing, to leave insert mode, press the 
``ESC'' key.

<P>
Pressing any arrow keys while in insert mode (which happens often)
causes vi to mess up.  If this happens, press ``ESC'' to exit
insert mode, then delete the extra line created.  You can always hit
``ESC'' to get out of insert mode and fix things that you inadvertantly
screwed up.

<P>
Text can also inserted by typing ``a''. This will add it following
the current character.  Press ``ESC'' to get out when you are finished.
<BR>
<BR>


<CENTER>
<H2>
Deleting text
</H2>
</CENTER>

<P>
To delete an entire line, press ``dd''. To delete a character, press ``x''.
``10dd'' will delete 10 lines, and ``5x'' will delete 5 characters.

<P>
If you delete something and want to undo it, press ``u'' for undo.
You can undo most commands in vi.

<P>
Once you have used ``dd'', the text is placed in a buffer.
To move the text you can place the cursor at the point where you want
it to appear and then type ``p''. This is equivalent to a block move.
<P>
A block is copied by using ``yy'' or ``10yy'' (or use any number
instead of 10) to yank into the buffer and then using p normally.
``p''--lower case--places the block below the current line.
``P''--upper case--places the block above the line where your cursor 
currently is.
<BR>
<BR>


<CENTER>
<H2>
Cursor movement
</H2>
</CENTER>

<P>
The cursor can be moved with the arrow keys as long as you are not
in insertmode.  ``h", ``j'', ``k'' and ``l'' also mimic the arrow keys.
Backwards, downward, upward, and forward one space or line respectively.
<P>
``0'' (zero) will move the cursor to the beginning of the line,
``$'' will take you to the end.

<P>
''G'' will go to the end of a file.  Typing

<PRE>
:10 
</PRE>

will take you to line 10,
and any number can be used in place of 10.

<P>
``^F'' (cntl-F) will page forward one screen, ``^B'' goes back one screen.
<BR>
<BR>


<CENTER>
<H2>
Replacing text
</H2>
</CENTER>

<P>
You can replace a single character by typing ``r'' followed by
the new value.  Use ``R'' to replace a whole group of characters,
and be sure to type ``ESC'' when you are done replacing.  Just for
safety's sake!
<BR>
<BR>


<CENTER>
<H2>
Finding and replacing text
</H2>
</CENTER>

<P>
To find a string, type 
<PRE>
/ 
</PRE>
followed by what you want to find.  Note that when you type a slash in
vi--in command mode--, your cursor moves to the bottom left-hand corner of 
your screen.  
<P>
For example, 
<PRE>
/Pascal
</PRE>
finds the next occurrence of the word ``Pascal,''
starting at the current cursor position.  
<P>
``n'' finds the next occurrence.
To replace text, type 
<PRE>
:s/pascal/Pascal/
</PRE>
This will replace
the first occurrence of ``pascal'' with ``Pascal'' 
<PRE>
:1,$s/pascal/Pascal/g
</PRE>
will do the replace globally throughout the document.  (From the 1st line, 
``1'' to the end, ``$'')
<BR>
<BR>
<BR>
<BR>
<BR>

--
<P>
<BLOCKQUOTE>
This tutorial was originally created by Marshall Brain for students
at North Carolina State University.  It has been modified
and augmented for the ESU environment by Phil Pfeiffer and
Brian Sherwood (1993) (phil@esu.edu, sherwood@esu.edu), who waive
all rights to their modifications. Gary Kline (kline@tao.thought.org)
elaborated and also put this document into markup format (1996) 
<P>
If you have ideas on how this brief introduction to vi can be improved,
please forward your ideas to <EM>kline@tao.thought.org</EM>
</BLOCKQUOTE>

</HTML>
























Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199608170220.TAA09516>