BBS水木清华站∶精华区

发信人: corpus (corpora), 信区: Unix 
标  题: vi-faq1(fwd) 
日  期: Sun Sep  8 16:47:30 1996 
 
Newsgroups: comp.unix.questions,comp.editors,comp.answers,news.answers 
Path:  
senator-bedfellow.mit.edu!bloom-beacon.mit.edu!news.kei.com!news.mathworks.com! 
gatech!swrinde!cs.utexas.edu!uwm.edu!vixen.cso.uiuc.edu!uchinews!kimbark!ellidz 
 
From: ellidz@kimbark.uchicago.edu (E. Larry Lidz) 
Subject: vi editor FAQ (Frequently Asked Question List), Part 1/2 
X-Nntp-Posting-Host: midway.uchicago.edu 
Message-ID: <D8r6w4.Dyu@midway.uchicago.edu> 
Followup-To: comp.editors 
Summary: This posting contains a list of Frequently Asked 
        Questions (and their answers) about the vi editor.   
Sender: news@midway.uchicago.edu (News Administrator) 
Reply-To: ellidz@midway.uchicago.edu 
Organization: University of Chicago -- Academic Information Technologies 
Date: Thu, 18 May 1995 02:57:39 GMT 
Approved: news-answers-request@MIT.EDU 
Lines: 484 
Xref: senator-bedfellow.mit.edu comp.unix.questions:92203 comp.editors:16112  
comp.answers:11963 news.answers:44302 
 
Archive-name: editor-faq/vi/part1 
Posting-Frequency: 2nd and 17th of every month 
Last-modified: 1995/05/03 
Version: 1.2 
 
                                vi editor FAQ  
                                ------------- 
 
0.0 - Introduction -- How do I use this FAQ? 
 
  This document is broken into multiple sections.  First, a general 
introduction and discussion of what vi is in sections 0 and 1.  Section 
2 is a collection of "novice" questions, questions that someone without 
much experience with vi might ask.  These include the differences 
between command and insert mode, and continue ending with questions 
such as "How do I cut and paste?"  Then, section 3 is geared toward the 
intermediate vi user.  Starting with "How do I do a search and 
replace," and continuing onward from there, until it finally ends with 
a discussion of vi macros.  Also included is a vi quick reference. 
This should give a canonical list of vi commands.  Next, is a list of 
set commands, all of which can be put into a .exrc file to customize 
your editing environment. 
  The quick reference was confirmed on a machine running SunOS with the 
UCB distribution of vi.  Each command should work under System V and 
UCB versions of vi other than the one shipped with SunOS, but I have 
not personally confirmed this. 
  Unless otherwise specified this document assumes that you are in 
command mode. 
  An attempt was made to retain much of the terminology used in the 
original vi documentation, wherever I could remeber what it was. 
  If you prefer to use the World Wide Web to look at FAQs, you can 
find this FAQ at: http://www.macom.co.il/vi/index.html 
 
0.1 - Index 
 
First File: 
 
0.0 - Introduction -- How do I use this FAQ? 
  0.1 - Index 
  0.2 - Can I distribute this FAQ? 
  0.3 - What can you do to help out with this faq? 
1.0 - What is vi? 
  1.1 - What is the big deal about vi?  Why does anyone use it?  More 
         importantly, why should *I* use it? 
  1.2 - Wow!  This sounds great!  Is there any reason not to use vi? 
  1.3 - What different operating systems is vi available for? 
  1.4 - Okay, you've convinced me.  I'm going to learn vi.  Where do I 
         start? 
  1.5 - What are some of the vi clones that are available? 
2.0 - Learning vi. 
  2.1 - What games will help me learn vi? 
  2.2 - What is the difference between Command mode & Insert mode? 
  2.3 - Wait, my keyboard doesn't have a <Esc> key!  What should I do? 
  2.4 - What are all of those ~s? 
  2.6 - How do I quit without saving? 
  2.7 - How do I insert a file? 
  2.8 - How do I search for text? 
  2.9 - How do I search for a control sequence? 
  2.10 - How do I reformat text? 
  2.11 - How do I copy text? 
  2.12 - Ahhhh!!!  I just hit dG and lost my dissertation!  What can I 
          do?  (Or, I've just made a mistake, what should I do?) 
  2.13 - I've just written my dissertation and have been told that I  
          need to have each section in a different file, what should  
          I do? 
  2.14 - What's the deal with all of these : commands? 
3.0 - How do you do a search and replace? 
  3.1 - How do I run a program from within vi? 
  3.2 - Ahhh!!  I was writing my dissertation, and the computer crashed! 
  3.3 - Any tips for making vi programmer friendly? 
  3.4 - Macros -- How do I write them? 
  3.5 - How do I make a function key a Macro? 
  3.6 - Is there anyway to abbreviate text? 
  3.7 - How do I spell check the current document? 
  3.8 - I've got a hardcopy terminal, can I still use vi? 
  3.9 - Oh, okay, is THAT what open mode is?  But I don't have a  
         hardcopy terminal, and it still starts in open mode! 
 
Second File: 
 
4.0 - What's online at the vi archives? 
5.0 - Silly vi tricks, and silly macros 
  5.1 - Silly vi tricks 
  5.2 - Silly macros 
6.0 - Alphabetical vi quick reference 
  6.1 - Command mode input options ( : commands) 
  6.2 - set options 
7.0 - Setting up .exrc file 
  7.1 - Sample .exrc file 
8.0 - Bugs in vi 
9.0 - Glossary of terms 
10.0 - Bibliography of Books that cover vi 
 
0.2 - Can I distribute this FAQ? 
 
  Yes, as long as you do not alter it in any fashion, or charge any 
money for it. 
 
0.3 - What can you do to help out with this faq? 
 
  First, send me any errors that you may find.  Also, any suggestions 
that you might have are also appreciated.  Better yet, anything that 
you think is unclear. 
  Also, use vi!  Spread the word!  I'm sorry I can't list everyone who 
has contributed to this faq, as many, many people have given me advice 
and helped out, but I fear if I were to, the list of contributers would 
become as long as the document itself. 
 
1.0 - What is vi? 
 
  vi is a Visual Editor (hence the name -- vi for VIsual).  What is a 
visual editor (as opposed to a non-visual one)?  Visual editors are 
ones that let you see the document that you are editing as you edit 
it.  This seems pretty common in most editors today, so the idea of a 
non-visual editor is a little strange.  Examples of non-visual editors 
are sed, ex, ed, and edlin (the last one being the editor shipped with 
DOS until relatively recently.) 
  vi was written by William Joy as part of the bsd distribution of 
Unix.  It was later used by AT&T, and has been standard Unix since. 
 
1.1 - What is the big deal about vi?  Why does anyone use it?  More 
importantly, why should *I* use it? 
 
  vi is default visual editor under Unix, and is therefore shipped with 
all recent version of Unix.  (Recent being defined as post 1984 or 
so.)  This means that whenever you run across a machine that is running 
a Unix of some sort, you will know that you have a powerful editor at 
your finger tips.  Why else? vi is a powerful editor.  Also, once you 
know vi, you can edit files really quickly, as it is extremely 
economical with the keystrokes.  Due to its different modes for 
inserting and issuing commands, it is much faster than most non-mode 
based editors.  It is also a very small editor.  (The version on my 
machine is 200k) Also, it can do almost anything, as long as you know 
how to get it to do what you want. 
 
1.2 - Wow!  This sounds great!  Is there any reason not to use vi? 
 
  Yes.  There is a very good reason.  It can be somewhat hard to learn, 
and until you do so, it will be slow and painful.  Once you learn it, 
it will be faster, but the process of learning it is slow.  I've been 
asked if vi was an easy editor to learn, whether it was intuitive or 
not.  My general response to this question is:  "Yes, some of us think 
so.  But most people think that we are crazy." 
 
1.3 - What different operating systems is vi available for? 
 
 Unix.  That's it.  However, there are many, many cl many clones of vi that  
are 
available for different operating systems.  I personally have used vi 
clones under:  Unix, Dos, OS/2, Mac System 7.  (See below for a list of 
specifics.) 
 
1.4 - Okay, you've convinced me.  I'm going to learn vi.  Where do I 
start? 
 
  Well...  That's a tricky one.  There are many good books out there 
that cover vi; most books on Unix have at least one chapter devoted to 
it.  There are also many books devoted specifically to vi.  I don't 
have any preference, so your best bet might be to ask your local vi 
guru where they learned.  Also, play around.  Fire up vi with a 
non-important document (your dissertation is NOT a good document to 
learn vi with...) and play around.  I can't imagine anyone learning vi 
without playing around with it quite a bit.  Remember, if you get 
confused, just hit the Escape key a couple of times, and you'll be in 
command mode again. 
  I should mention at some point, and I guess here is as good a place 
as any, that people who think they might want to do Unix system 
administration, or any type of configuration of unix machines will 
probably also want to learn ed or ex, as some versions of Unix do not 
put vi in the root partition, and one might be stranded without it at 
some point.  Ed is a good choice. 
 
1.5 - What are some of the vi clones that are available? 
 
  Just to list a few:  STvi (STevie), elvis, vile, vim, and nvi, xvi. 
 
elvis is available for: Amiga, DOS, OS/2, Unix, VMS, Atari.   
STevie is available for:  Atari ST, DOS, Unix, Mac System 7.   
  Mac System 7 is available at any info-mac mirror in /info-mac/text 
    (Such as ftp.hawaii.edu) 
nvi is the vi that will ship with BSD 4.4.  
vim is available for: Amiga, DOS, Mac System 7, Unix. 
  Amiga, DOS, and the source are available at: 
    ftp.fu-berlin.de /misc/editors/vim  
  Mac System 7 is available at any info-mac mirror in /info-mac/text 
    (Such as ftp.hawaii.edu) 
vile is available for: DOS, OS/2, Unix, VMS.   
xvi is available for: DOS, Unix. 
 
  There are some differences between the different vi clones.  Many 
offer improvements, but most still allow the commands that are listed 
in this document, but there may be some differences.  Refer to the 
documentation that comes with the clone for details. 
 
2.0 - Learning vi. 
 
  These are some basic hints for the novice vi user.  First, keep a 
command summary with you at all times.  A quick reference guide/command 
summary is included later on.  Second, get a good book that covers vi. 
This document is not the best way to learn it (at least not yet.)  I'm 
not sure if this document should teach people to to use vi from 
scratch, as there are many good books on it already.  However, there 
are hints here.  As for choosing a book, the standard rules apply: 
look at it before buying it.  See if any of it makes sense to you. 
Make sure that it has exercises that you can practice with.  Compare it 
with other books -- after all, vi can be very confusing, and you want 
to make sure that it is the book that is confusing, and not just that 
you underestimated the difficulty of learning vi. 
  Also, seek out vilearn or vitutor, programs designed to teach you the 
basics of vi. 
 
2.1 - What games will help me learn vi? 
 
  This may seem a bit silly, but there are many games on Unix systems 
that can help you learn to use vi.  These help particularly with the 
basics.  Although I don't know of any games that help with every vi 
command, I do know of a few that will help you learn to use hjkl to 
move the cursor around.  NetHack, a rogue-like game, is particularly 
good for this, as it is a large game and can be entertaining for quite 
some time.  Not to make the other games sound worse, but some other 
ones are:  rogue, moria, omega, worm, and snake. 
 
2.2 - What is the difference between Command mode & Insert mode? 
 
  Often cited as one of the main problems with vi, and equally often 
cited as being one of its best strengths, vi differentiates between a 
"Command mode" and an "Insert mode."  Understanding this difference is 
VITAL to learning vi.  When one starts vi it starts in command mode. 
In this mode, one can move around the file, and issue commands to 
change certain areas of the text, cut, copy and paste sections of the 
text and do much more.  Insert mode is where one can actually insert 
text.  In other words, command mode is used to move around the 
document, and insert mode is used to type text into the document. 
  Commands such as:  a, i, c, C, O, o and others will switch one from 
command mode to insert mode. 
  <Esc> or ^C will take one out of insert mode and return one to 
command mode. 
  Get used to this distinction.  It is one of the things that makes vi 
different from most other editors.  It also allows one to do a lot of 
things without taking one's hands from the standard keyboard position. 
 
2.3 - Wait, my keyboard doesn't have a <Esc> key!  What should I do? 
 
  Try hitting ^[ instead.  If your keyboard has a <Meta> key, try 
that.  If neither of these work, try ^3. 
 
2.4 - What are all of those ~s? 
 
  They're just there to let you know where the bottom of your file is, 
they are not actually in your document, and you do not need to worry 
about them. 
 
2.5 - I can't get used to using hjkl, do you have any suggestions? 
 
  First, if your terminal is set properly and you have a good 
connection, you should be able to use the arrow keys.  However, if you 
think that you will be using vi a lot, then it makes sense to learn 
hjkl, as they are faster to type.  Also, there are occasions where you 
may have a bad connection and the ESC sequences may get lost. 
 
 
2.6 - How do I quit without saving? 
 
  :q! will do it.  If vi appears to have frozen, make sure that you 
haven't hit ^S by mistake.  In order to undo a ^S, hit ^Q. 
 
2.7 - How do I insert a file? 
 
  :r <filename> 
 
  For example, to insert the file /etc/motd, type:  :r /etc/motd 
 
  This will insert the file at the current location in the file you are 
working on.  If you specify a number before the r, it will insert it at 
that location in the file. 
 
2.8 - How do I search for text? 
 
  /<text> will search forward.  ?<text> will search backwards.  ?? or 
// will repeat the last search.  It is worth noting that these are 
pretty much standard in Unix.  In addition, in vi, n will find the next 
occurrence.  N will repeat the last search, reversing the direction. 
Regular Expressions may be used within searches. 
 
2.9 - How do I search for a control sequence? 
 
  /^V^<seq> 
 
 ^V will tell vi to take the next character literally, and not to take 
it as a command. 
 
2.10 - How do I reformat text? 
 
  If your computer has the program fmt on it, all you need to do is 
type !}fmt from command mode (without a : before it).  This will 
rejustify the text from the current location until the end of the 
paragraph.  If your machine does not have fmt, you need to find a 
similar program.  (I gather there are many such programs available from 
the public domain, but I do not know much about them.) 
 
2.11 - How do I copy text? Okay, this might be a bit complicated.  Take from  
this section what 
you can, and reread it a few times.  Also, experiment. 
  "<letter>yy will copy one line of text into register <letter>.  (A 
register is vi-lingo for a place to store data that was cut or copied.) 
<letter> must be between a and z.  "<letter>dd will delete one line and 
place it in register <letter>.  You may use a number before the yy or 
dd to specify the number of lines.  Using an uppercase <letter> will 
append the text into the register leaving what was there before. 
"<letter>p will put the text after the cursor.  "<letter>P will put it 
before the cursor.  If the register contains the beginning or end of a 
line, the line will be placed on another line as appropriate.  Y may be 
used as a short cut for yy.  In addition, y$, yH, yM, etc. are valid, 
as are the equivalent d commands.  For quick cuts and pastes, no 
register need be specified.  In this case, no appending is allowed, and 
the register will be removed if another delete command is given. 
(Including x). 
  For example, to move the previous paragraph, one would go to the top 
of the paragraph, type "a13dd, move to the position in which one wishes 
to put the paragraph, and then type "ap to put it below the current 
line. 
  Now, presumably you want to be able to cut and paste into areas that 
are not just the end of the line.  In order to do this, use m<letter> 
to mark an area.  This letter may be the same as a cut/copy register, 
they are stored in different area of memory.  Then, type 
"<register>`<letter>[yy or dd].  Where <register> is the register to 
put the text into, <letter> is the letter used to make, and yy or dd as 
appropriate. 
 
2.12 - Ahhhh!!!  I just hit dG and lost my dissertation!  What can I 
do?  (Or, I've just made a mistake, what should I do?) 
 
  u will undo the last command.  U will undo changes to the current 
line.  (Granted, a one line dissertation would not be much.) :e! will 
reload the current document without saving any changes.  In addition, 
deleted text gets stored in the registers numbered from 1 to 9.  "<n>p 
will put the last nth deletion.  You can quickly search the registers 
by trying one, hitting u, and trying the next.  (In order to expedite 
this, vi uses . slightly differently than normal.  Instead of repeating 
the last command, it will try the next register, so all you need to do 
is: "1p u . u ., etc. until you undo the delete you want to undo.) 
 
 
2.13 - I've just written my dissertation and have been told that I need 
to have each section in a different file, what should I do? 
 
 :[m],[n]w <filename> will save between lines m and n to <filename>. 
This line numbering method works for almost every : command.  If you 
use :[m],[n]w >> <filename> it will append it to the file. 
 
2.14 - What's the deal with all of these : commands? 
 
  The commands that follow a : are commands from the ex editor.  These 
allow a lot of flexibility and power.  For example, there are many 
different ways to search and replace, all of with have some 
similarities (in fact, they are in some ways the same...) 
 
3.0 - How do you do a search and replace? 
 
  Well, there are a few methods.  The simplest is: 
    :s/old/new/g But, this only does it on the current line...  So: 
    :%s/old/new/g In general, 
    :[range]s/old/new/[cg] Where [range] is any line range, including 
numbers, $ (end of file), . (current location), % (current file), or 
just two numbers with a dash between them.  (Or even: .,+5 to mean the 
next five lines).  [cgi] is either c, g, i, or nothing.  c tells vi to 
prompt you before the changes, g to change all of the occurrences on a 
line.  i tells vi to be case insensitive on the search.  No character 
after the last slash will only change the first occurrence on the line. 
  My favorite method is: 
    :g/foobar/s/bar/baz/g This searches for foobar, and changes it to 
foobaz.  It will leave jailbars alone, which the other method will 
not.  This is my favorite method, but is harder to remember. 
  Of course you can also use regular expression search patterns, and a 
few other commands in the replacement part of the text.  If you use 
\( and \) in the pattern to escape a sequence, you can do lots of nifty 
things. 
  For example: 
    :g/\(foo\)\(bar\)/s/\2/\1baz/g will change foobar for foobaz. 
  Special sequences allowed are: 
    &        everything which was matched by the search  
    \[1-9]   The contents of the 1st-9th \(\) pair  
    \u       The next character will be made uppercase  
    \U       The characters until \e or \E will be made uppercase  
    \l       The next character will be made lowercase 
    \L       The characters until \e or \E will be made lowercase 
    \[eE]    end the selection for making upper or lowercase 
 
3.1 - How do I run a program from within vi? 
 
  :!cmd will run the program cmd.  :sh will run an interactive shell. 
Within this shell, you may, if you want, run vi again.  This is 
particularly useful when you are editing makefiles and config files for 
programs in an attempt to get a program to compile.  The advantage over 
e is that you do not need to save the file, and it will be in its old 
place when you exit the shell.  (I advise saving the file anyway...) 
 
3.2 - Ahhh!!  I was writing my dissertation, and the computer crashed! 
 
  Well, you should get mail about this, but you should be able to 
recover the file by typing vi -r <filename> where <filename> is the 
name of the file that you were editing at the time of the crash.  vi -r 
will give you a list of files that can be recovered. 
 
3.3 - Any tips for making vi programmer friendly? 
 
  :set ai will make it auto-indent for you.   
  :set sw=# where # is a number will set the shiftwidth (tabwidth).   
You can then use <<, >> to shift a line left or right.  Plus, you  
can use <% to shift a {, ( or [ set left or right (with >%).   
You must be on top of the specific {, }, (, ), [ or ] of the pair  
to shift them.   
  :set sm will show the matching {, ( or [ when you type the closing 
one.   
  :set lisp will make some changes that are useful for lisp 
programming.  () will move back and forth over s-expressions, and {} 
will move without stopping at atoms. 
 
3.4 - Macros -- How do I write them? 
 
  :map <lhs> <rhs> where <lhs> is up to ten characters and <rhs> is up 
to 100.  This will make it so that whenever you type <lhs> it will 
replace it with <rhs>.  All macros should start in command mode, but 
may end in any mode you desire.  Remember to use ^V before any control 
characters that you may use. 
  :unmap <lhs> will remove the macro.  :map! <lhs> <rhs> will make 
<lhs> insert <rhs> into the text of the document. 
 
3.5 - How do I make a function key a Macro? 
 
  If <lhs> is #n where n is 0-9, it will be mapped to the appropriate 
function key. 
 
3.6 - Is there anyway to abbreviate text? 
 
  Yep, of course.  This is vi, it can do anything.  :ab email 
ellidz@midway.uchicago.edu will make it so that whenever you type 
email as a specific word, it will extend it to the entire 
unabbreviated word.  :una email will unabbreviate it. 
 
3.7 - How do I spell check the current document? 
 
  Here is a macro to do it.  These should be put in your .exrc file. 
(More on .exrc files later on.) It is a pretty simple macro, it just 
calls ispell on the current file.  Of course, to use this you need 
ispell on your system.  To use it, just hit V with vi.  (V is not used 
by vi, so it makes a good key.) 
 
  map V :w^M:!ispell % ^M:e!^M^M 
 
The second ^M makes it so that one does not need to hit return after it 
is done checking the spelling. 
 
3.8 - I've got a hardcopy terminal, can I still use vi? 
 
  Okay, okay, so I don't expect anyone to actually ask this...  But, I 
thought it was bizarre enough to throw in anyway.  (And, it actually 
answers a very common question...) 
  vi will start up in a specific mode, called "open mode" in this 
situation.  Things work more or less the same.  Deleted characters will 
appear on your print out at \'s.  vi will act as if the size of the 
window is only one line.  ^r will retype the current line.  z redraws 
the window around the current line. 
 
3.9 - Oh, okay, is THAT what open mode is?  But I don't have a 
hardcopy terminal, and it still starts in open mode! 
 
  Well, what is happening here is that vi doesn't know what type of 
terminal you have.  It decides that in this situation the best thing to 
do is to assume that you have the worst terminal possible.  This might 
not seem useful, as not very many people need open mode, but it also is 
the mode that needs to know the least information about your terminal. 
  Now, how to deal with it.  It is possible to change it for the 
specific session, but in general, this is not useful.  If you know 
your terminal type, you can set it from the Unix prompt (setenv TERM 
<termtype> under csh and it's variants, and:  TERM=<termtype> ; export 
TERM under sh and its variants.). 
  Better yet would to be to edit your .profile or .chsrc to include 
this so it is automatically done for you when you login.  (Of course, 
you need to either know ed or be able to set it at the unix prompt 
before you'll be able to edit the file...) 
  If you do not know your terminal type, try vt100.  Most modern 
terminals and terminal emulators can emulate vt100.  If this does not 
work, find someone to help you. 
.  
 

BBS水木清华站∶精华区