[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freebsd-doc
Subject:    vi tutorial overview, ASCII version.
From:       Gary Kline <kline () tera ! com>
Date:       1996-09-25 21:38:35
[Download RAW message or body]


Enclosed is the ASCII version of the overview on vi that I'm working on.
Feedback, people??

gary kline

@(#) HowVi.ascii. 25sep96

This ``Using vi'' tutorial is intented to be a brief overview; something
that, within 20 to 30 minutes, will jump-start you on the road to learning
the basics of vi.  Read through and work with this once or twice.  To
launch yourself into a higher orbit of skill and understanding, check out
the References toward the bottom.
   



Using vi 



The vi text editor is a widely available visual, 
full-screen text editor.
vi is a powerful editor, and it is also old--and this shows.
vi was created at a time when most terminals did not have arrow keys.
Because of this, vi is modal:
it has several modes it can be in, with its behavior changing
depending on mode.







Readers who are at workstations should open a second window
and to try out vi commands as they work through this overview.
If you have access to only one window or screen, print a hardcopy
of this overview. 






A parenthetical note:

((You can really begin to edit documents with only a few commands: either
the arrow-keys or ``h'', ``j'', ``k'', and `l''.  Plus the backspace key
and ths spacebar.  These keys will move you around the screen--if you are
in command mode.

Then with ``i'' to insert and ``ESC'' to get back into command mode, you
can enter text.  ``x'' to delete a character and ``r'' to replace a
character round out the minimal techniques.

But, I'm getting ahead of myself, so on with this tutorial. . .  .))






Starting and stopping




To start up vi, change to your home directory and type 

% vi samp

This will start vi editing a (theoretically) new file named
``samp.'' If samp already exists it will be loaded and displayed 
upon your screen.


Many different commands can be used to exit vi.  Typing 

:wq

will write the file to disk and quit.  
Typing

:q

will quit if no changes
have been made.  If changes have been made then you must type

:q!

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.)

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 will add more skills as you go along.





Inserting text




Initially vi is in command mode. Most (not all) keys entered 
are commands to vi itself and will not enter text.

Typing ``i'' is the command to enter text-input mode.  vi inserts all 
non-control characters typed while in text-input mode into the document.
Some characters in this mode have special meanings; the backspace key,
for example, backs over mistakes.  



((If you do accidentally change something
in your vi session, the ``u'' command will undo it  when you
are in command mode.))


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

If you type ``o'' (lower-case), you will open a line immediately below
where your cursor is.  An upper-case ``O'' will insert an empty line
above your cursor.

And some parallels here with ``A'' and ``I.''  If you type an ``A''
on any line, the cursor will move to the end of the text and be ready
to append text.  Typing ``I'' will prepare vi to insert text at the
beginning of the line.

Try typing something, like four
lines from this tutorial if nothing else comes to mind.
When you are through typing, to leave text-input mode, press the 
``ESC'' key to get back into command mode.






Deleting text




To delete an entire line, press ``dd''. To delete a character, press ``x''.
``10dd'' will delete 10 lines, and ``5x'' will delete 5 characters.
Try these deletion commands now.  Undo each, if you wish, by typing ``u''.

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.

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.

Typing ``D'' will delete the rest of the line---from wherever you have
the cursor to the end of that line.
If you want to delete text from wherever you have the cursor is until
you find a specific character, ``dfcharacter'' will do that for you.
If you place the cursor at the beginning of a word and type ``dw''
vi will delete just that word.







Cursor movement




Note that in some cases you cannot use the arrow keys while in insert
mode.  With some kinds of hardware you can.  The only way to determine
whether you can use your arrow keys in text-input mode is to experiment.

As a rule of thumb, the cursor can be moved with the arrow keys as long 
as you are not in text-input mode.  The lower-case letters 
``h", ``j'', ``k'' and ``l'' also mimic the arrow keys.  Backwards, 
downward, upward, and forward one space or line respectively.  The 
backspace key moves the cursor one space backwards; the spacebar 
moves one space forward.  



((If you use some straightforward re-mapping tricks you can map the arrow
keys to the h,j,k,l keys.  Such mapping tricks are beyond the intent of
this overview, however.  --See the References section 
below. ))


As a rule of thumb, the cursor can be moved with the arrow keys as long 
as you are not in text-input mode.  ``h", ``j'', ``k'' and ``l'' also 
mimic the arrow keys.  Backwards, downward, upward, and forward one space 
or line respectively.

``0'' (zero) will move the cursor to the beginning of the line,
``$'' will take you to the end.


''G'' will go to the end of a file.  Typing ``numberG'' will move you to
the line ``number''  And then typing two apostrophes will move you back 
to where you were.  If you were on, say, line 4, then typed ``15G'' followed
by '' you would go from line 4 to 15 and back to line 4.

Typing


:10 


will take you to line 10.  Any number can be used in place of 10.


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






Replacing text (while in command mode)




You can replace a single character by typing ``r'' followed by
the new value.  Use ``R'' to replace a whole group of characters.
By placing the cursor on the first letter of a word  and typing
``cw'' you are ready to change the entire word.  After you have
typed in your replacement, hit ``ESC''  to get back into command
more.

Typing ``C'' from wherever the cursor is to the end of the line
will ready vi to let you re-type the line.

If you type ``cfcharacter'' where character is an ASCII
character, vi will let you change the string from whereever the
cursor is until it finds the character.
Be sure to type ``ESC'' when you are done replacing.  

The period or dot ``.'' means
``Repeat the last command''.  --So it is possible to make accidental changes
to your file while you are still in command mode, but you should not
find this too much trouble.--

Typing ``.'' in command mode is an effective means of replacing various
sections of text easily and quickly. 







Finding and replacing text




To find a string, type 

/ 

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.  

For example, 

/Pascal

finds the next occurrence of the word ``Pascal,''
starting at the current cursor position.  

``n'' finds the next occurrence.
To replace text, type 

:s/pascal/Pascal/

This will replace
the first occurrence of ``pascal'' with ``Pascal''   And the following:

:1,$s/pascal/Pascal/g

will do the replace globally throughout the document.  (From the 1st line, 
``1'' to the end, ``$'')




Miscellaneous features



Here are some miscellaneous features of vi that both new and skilled users
employ.  


The set commands. 


Typing ``:set feature'' lets you set a variety of
features commonly used in editing and for programming.  Here are a few
examples.

:set autoindent

will automatically indent the next line to the same indentation as you
typed above.  To back out of any indentation, you must type a Control-D.
  Typing

:set noautoindent

unsets the autoindent feature.

:set showmatch

will tell the editor to show you matching cases or parenthesis and braces.
This is useful in many programming languages. Using showmatch, vi will
blink each matching pair of parens or braces.

:set noshowmatch

unsets the feature

:set nunmber

causes vi to display number lines along the left-hand side of your display.

:set nonunmber

gets rid of the line numbers.



Executing a command from within vi.  


Typing  ``:! command'' will 
temporarily escape you back to your shell and execute ``command''.
For example:

:!ls -l

will present you with a long listing of whatever files are in your 
current directory.  As soon as the listing is complete, you will be
back inside vi and a colon prompt will be displayed at the lower 
left-hand corner of your display.


Reading another file into vi



Typing ``:r filename'' will cause vi to
include the file ``filename''  within your current file at the
linenumber of your cursor.  For example

:r letter_to_jack


will cause the file ``letter_to_jack'' to be read into your current
file.  Here, you need to be sure at exactly what linenumber you 
want to have ``letter_to_jack'' inserted and be sure that your cursor
is at that line.

Note that you can always undo the insertion by typing
``ESC u'' 




References




This brief overview will get you going with vi.  After you have become
familiar with these few commands and want to learn more, there are a
number of avenues.  

One is to simply ask friends or colleagues; another
is to read of one the many commercial tutorials on vi. Jerry Peek, Tim
O'Reilly and others have a bunch of tricks and techniques for getting the
most out of vi in Unix Power Tools.  A third is to
check out the online documentation on nvi by Keith Bostic.  You will find this
file in /usr/share/doc/usd/13.viref, paper.ascii.gz.  
Another online document titled ``An Introduction to Display Editing
with vi,'' by Bill Joy and Mark Horton lives in 
/usr/share/doc/usd/12.vi/paper.ascii.gz
This fourth reference is the original paper on vi.  You can scroll through
these gzip'd docs with the  zmore utility.

Following are ftp or http sites that have tutorial documentation on using the
vi editor.   There is an exceptionally well-written vi FAQ available at:
 http://www.macom.co.il/vi/index.html  that has all files
in HTML format.  This site will give you considerably more information that
in this overview.

And in Norway at alf.uib.no in /pub/vi, the following files are available
for anonymous ftp:



-rw-r--r--  1 20035        5881 Sep 24  1993 HELP
-rw-r--r--  1 20035       15249 May 20  1995 INDEX
-rw-r--r--  1 20035        1123 Jun 23  1992 WHO
drwxr-xr-x  2 20035        1536 Jun 19 07:39 comp.editors
-rw-r--r--  1 20035       16247 May 25 20:06 comp.editors.EDS
-rw-r--r--  1 20035        9569 Aug 11  1992 comp.editors.FAQ
-rw-r--r--  1 20035        2822 Feb 28  1994 comp.editors.PNT
-rw-r--r--  1 20035        1416 Jul 25  1993 comp.editors2
-rw-r--r--  1 20035         990 Aug  8  1993 comp.editors3
drwxr-xr-x  2 20035        2048 Jun 19 07:39 docs
drwxr-xr-x  2 20035        1536 Jun 19 06:42 macros
drwxr-xr-x  4 20035         512 Jun 19 07:12 programs
drwx------  2 20035        1024 Jun 19 07:39 t
226 Transfer complete.



In the docs directory are several dozen compressed files from one to
several years old.  Several seem worth downloading and reviewing if you
really want to hone your vi skills.

Bottom line: there is lots of freely available literature on learning 
vi out there.  





Concluding remarks



If you are determined to learn a particular skill, you will; even something
that seems as arcane as the vi editor.  vi will almost certainly grow on
you.  Especially as you become more proficient,  vi, with all its tricks
and shortcut commands will let you edit a file very efficiently.

It will probably help to keep a HowTo file, either on paper or online to keep 
track of how to do various things in vi.  After a few days of using vi, 
you're on your way toward its mastery.





The version of vi in FreeBSD was written by Keith Bostic and named nvi.
nvi and nex--the clone of the original ``ex''-- excell in their functionality
and concept.  Bostic claims that nvi is a ``feature-for-feature, bug-for-bug''
replacement for the original program written by Bill Joy.  I have found it
to be even a bit better than the original.  People who can find at least
two such improvements get gold stars by their names!









--


This tutorial was begun by Marshall Brain and augmented by Phil Pfeiffer 
and Brian Sherwood (1993). Gary Kline futher elaborated enhanced this 
with the kind help of many folks interested in the FreeBSD Project.  Gary 
Kline also put this document into markup format (1996) .

If you have ideas on how this brief introduction to vi can be improved,
please forward your ideas to kline@tao.thought.org

























[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic