From owner-freebsd-doc Fri Aug 16 19:21:36 1996 Return-Path: owner-doc Received: (from root@localhost) by freefall.freebsd.org (8.7.5/8.7.3) id TAA07814 for doc-outgoing; Fri, 16 Aug 1996 19:21:36 -0700 (PDT) Received: from hod.tera.com (hod.tera.com [206.215.142.67]) by freefall.freebsd.org (8.7.5/8.7.3) with ESMTP id TAA07808 for ; Fri, 16 Aug 1996 19:21:34 -0700 (PDT) Received: from athena.tera.com (athena.tera.com [206.215.142.62]) by hod.tera.com (8.7.5/8.7.3) with ESMTP id TAA16214; Fri, 16 Aug 1996 19:20:59 -0700 (PDT) From: Gary Kline Received: (from kline@localhost) by athena.tera.com (8.7.5/8.7.3) id TAA09516; Fri, 16 Aug 1996 19:20:59 -0700 (PDT) Date: Fri, 16 Aug 1996 19:20:59 -0700 (PDT) Message-Id: <199608170220.TAA09516@athena.tera.com> To: doc@freebsd.org Subject: vi tutorial Cc: kline@tera.com Sender: owner-doc@freebsd.org X-Loop: FreeBSD.org Precedence: bulk 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:

Using vi

The vi text editor is a widely available visual, 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.




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

Starting and stopping

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

% vi samp
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.

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

``ZZ'' ---without the quotation marks, of course---and note that the Z's must be upper case-- is a synonym for ``:wq''.

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.

Inserting text

Right now vi is in command mode. Any key entered is a command to vi.

Try typing ``i'' to enter insert mode. 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.

You cannot, however, use the arrow keys while in insert mode! 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.

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.

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

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.

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

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.

Cursor movement

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.

``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

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

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

Replacing text

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!

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''
:1,$s/pascal/Pascal/g
will do the replace globally throughout the document. (From the 1st line, ``1'' to the end, ``$'')




--

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)

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