home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / editor-faq / vi / part1 next >
Encoding:
Internet Message Format  |  2004-04-19  |  34.8 KB

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!news2.telebyte.nl!newsfeed.stueberl.de!news1.spb.su!news.runnet.ru!cclinfo!not-for-mail
  2. From: ellidz@pobox.com (E. Larry Lidz)
  3. Newsgroups: comp.unix.questions,comp.editors,comp.answers,news.answers
  4. Subject: vi editor FAQ (Frequently Asked Question List), Part 1/2
  5. Followup-To: comp.editors
  6. Date: 19 Apr 2004 04:02:04 GMT
  7. Organization: COCOS Lab. 
  8. Lines: 740
  9. Approved: news-answers-request@MIT.EDU
  10. Message-ID: <c5vivs$adr$1@cclinfo.perm.su>
  11. NNTP-Posting-Host: lyceum.pstu.ac.ru
  12. X-Trace: cclinfo.perm.su 1082347324 10683 195.19.167.110 (19 Apr 2004 04:02:04 GMT)
  13. X-Complaints-To: usenet@ccl.ru
  14. NNTP-Posting-Date: 19 Apr 2004 04:02:04 GMT
  15. Summary: This posting contains a list of Frequently Asked
  16.         Questions (and their answers) about the vi editor.
  17. X-Last-modified: 2004-04-09
  18. Originator: ellidz@h-69-3-98-37.chcgilgm.covad.net (E. Larry Lidz)
  19. Xref: senator-bedfellow.mit.edu comp.unix.questions:195304 comp.editors:72865 comp.answers:56940 news.answers:270255
  20.  
  21. Archive-name: editor-faq/vi/part1
  22. Posting-Frequency: 2nd and 17th of every month
  23. Last-modified: 2004-04-09
  24. Version: 1.89
  25.  
  26.                 vi editor FAQ 
  27.                                 -------------
  28.  
  29. 0.0 - Introduction -- How do I use this FAQ?
  30.  
  31.   This document is broken into multiple sections.  First, a general
  32. introduction and discussion of what vi is in sections 0 and 1.  Section
  33. 2 is a collection of "novice" questions, questions that someone without
  34. much experience with vi might ask.  These include the differences
  35. between command and insert mode, and continue ending with questions
  36. such as "How do I cut and paste?"  Then, section 3 is geared toward the
  37. intermediate vi user.  Starting with "How do I do a search and
  38. replace," and continuing onward from there, until it finally ends with
  39. a discussion of vi macros.  Also included is a vi quick reference.
  40. This should give a canonical list of vi commands.  Next, is a list of
  41. :set commands, all of which can be put into a .exrc file to customize
  42. your editing environment.
  43.   The quick reference was confirmed on a machine running SunOS with the
  44. System V distribution of vi.  Each command should work under System V and
  45. UCB versions of vi other than the one shipped with SunOS, but I have
  46. not personally confirmed this.
  47.   Unless otherwise specified this document assumes that you are in
  48. command mode.
  49.   An attempt was made to retain much of the terminology used in the
  50. original vi documentation, whenever I could remember what it was.
  51.   Also, I will often refer to regular expressions (often called
  52. regex's). Please look at the man page (man 5 regexp will show it on
  53. most Unix programs) or another reference (the O'Reilly Mastering
  54. Regular Expressions book in the bibliography, for example) for more
  55. information on what they are and what they do. They're really useful,
  56. and it won't be a waste of time. Trust me.
  57.  
  58. 0.1 - Index
  59.  
  60. First File:
  61.  
  62. 0.0 - Introduction -- How do I use this FAQ?
  63.   0.1 - Index
  64.   0.2 - Can I distribute this FAQ?
  65.   0.3 - What can you do to help out with this faq?
  66.   0.4 - Where can I read this FAQ in other languages?
  67. 1.0 - What is vi?
  68.   1.1 - What is the big deal about vi?  Why does anyone use it?  More
  69.          importantly, why should *I* use it?
  70.   1.2 - Wow!  This sounds great!  Is there any reason not to use vi?
  71.   1.3 - What different operating systems is vi available for?
  72.   1.4 - Okay, you've convinced me.  I'm going to learn vi.  Where do I
  73.          start?
  74.   1.5 - What are some of the vi clones that are available?
  75. 2.0 - Learning vi.
  76.   2.1 - What games will help me learn vi?
  77.   2.2 - What is the difference between Command mode & Insert mode?
  78.   2.3 - Wait, my keyboard doesn't have a <Esc> key!  What should I do?
  79.   2.4 - What are all of those ~s?
  80.   2.5 - I can't get used to using hjkl, do you have any suggestions?
  81.   2.6 - How do I quit without saving?
  82.   2.7 - How do I insert a file?
  83.   2.8 - How do I search for text?
  84.   2.9 - How do I search for a control sequence?
  85.   2.10 - How do I reformat text?
  86.   2.11 - How do I copy text?
  87.   2.12 - Ahhhh!!!  I just hit dG and lost my dissertation!  What can I
  88.           do?  (Or, I've just made a mistake, what should I do?)
  89.   2.13 - vi appears to be frozen or acting strange, what can I do?
  90.           (Also, I can't get rid of the colon prompt, now what?)
  91.   2.14 - I've just written my dissertation and have been told that I 
  92.           need to have each section in a different file, what should 
  93.           I do?
  94.   2.15 - What's the deal with all of these : commands?
  95.   2.16 - Argh! vi has gone crazy! None of the commands work! Now what?
  96. 3.0 - How do you do a search and replace?
  97.   3.1 - My / key is broken!  How can I search and replace?
  98.   3.2 - How do I run a program from within vi?
  99.   3.3 - Ahhh!!  I was writing my dissertation, and the computer crashed!
  100.   3.4 - Any tips for making vi programmer friendly?
  101.   3.5 - Macros -- How do I write them?
  102.   3.6 - How do I make a function key a Macro?
  103.   3.7 - Is there anyway to abbreviate text?
  104.   3.8 - How do I spell check the current document?
  105.   3.9 - How do I get rid the ^M's at the end of each line of my file?
  106.          How do I make a macro to do it?
  107.   3.10 - I've got a hardcopy terminal, can I still use vi?
  108.   3.11 - Oh, okay, is THAT what open mode is?  But I don't have a 
  109.          hardcopy terminal, and it still starts in open mode!
  110.   3.12 - How can I get the source code to vi?
  111. 4.0 - More advanced topics in vi
  112.   4.1 - I see that sections are defined by SHNHH HU and paragraphs by
  113.         IPLPPPQPPLIbp by default? What language is that in? What in
  114.         the world does it mean?
  115.   4.2 - But when I program, I've noticed that I can use ]] and [[
  116.         to move between functions. My functions don't have .SH or 
  117.         anything at the beginning, what gives?
  118.   4.3 - How do I work with multiple files at the same time?
  119.   4.4 - So, I've started up vi, but now I want to be able to edit another
  120.         file. How do I do this?
  121.   4.5 - How can I get back to the first file, though?
  122.   4.6 - But I'm only wanting to edit one file at a time. I've got one
  123.         open and I want to switch permenatly to another file. How do 
  124.         I do that?
  125.   4.7 - How do I add another file into the middle of my current file? 
  126.  
  127. Second File:
  128.  
  129. 5.0 - What's online at the vi archives?
  130. 6.0 - Silly vi tricks, and silly macros
  131.   6.1 - Silly vi tricks
  132.   6.2 - Silly macros
  133. 7.0 - Alphabetical vi quick reference
  134.   7.1 - Command mode input options ( : commands)
  135.   7.2 - set options
  136. 8.0 - Setting up .exrc file
  137.   8.1 - Sample .exrc file
  138. 9.0 - Bugs in vi
  139. 10.0 - Glossary of terms
  140. 11.0 - Bibliography of Books that cover vi
  141.  
  142. 0.2 - Can I distribute this FAQ?
  143.  
  144.   Yes, as long as you do not alter it in any fashion, or charge any
  145. money for it.  If you plan on including it in a book, CDRom, or other
  146. such publication, please contact me first.
  147.  
  148. 0.3 - What can you do to help out with this faq?
  149.  
  150.   First, send me any errors that you may find.  Also, any suggestions
  151. that you might have are also appreciated.  Better yet, anything that
  152. you think is unclear.  My email address is:
  153.             ellidz@eridu.uchicago.edu
  154.   Also, use vi!  Spread the word!  I'm sorry I can't list everyone who
  155. has contributed to this faq, as many, many people have given me advice
  156. and helped out, but I fear if I were to, the list of contributors would
  157. become as long as the document itself.
  158.  
  159. 0.4 - Where can I read this FAQ in other languages?
  160.  
  161.   [Note: I've lost my list of other people's translations, if you've
  162.    translated the FAQ to another language and your translation is not
  163.    listed here, please let me know.                                  ]
  164.  
  165.   Marc Simon (msimon@astrolabe.frmug.org) has translated the FAQ into
  166. French. It is available at: <http://tux.u-strasbg.fr/vim/vifaq.html>.
  167.  
  168.   Daniel Silva has translated the FAQ into Portuguese. It is available
  169. at: <http://www.linuxdicas.com.br/vifaq.txt>.
  170.  
  171. If you're interested in translating the FAQ into another language, let
  172. me know, I'm always happy to have it be more widely available. If you
  173. let me know before you start working on it I can make sure you're not
  174. duplicating someone else's efforts.
  175.  
  176. 1.0 - What is vi?
  177.  
  178.   vi is a Visual Editor (hence the name -- vi for VIsual).  What is a
  179. visual editor (as opposed to a non-visual one)?  Visual editors are
  180. ones that let you see multiple lines of the document that you are
  181. editing as you edit it.  This seems pretty common in most editors
  182. today, so the idea of a non-visual editor is a little strange.
  183. Examples of non-visual editors are sed, ex, ed, and edlin (the last one
  184. being the editor shipped with DOS until relatively recently.)
  185.   vi was written by William Joy as part of the bsd distribution of
  186. Unix.  It was later used by AT&T, and has been standard Unix since.
  187.  
  188. 1.1 - What is the big deal about vi?  Why does anyone use it?  More
  189. importantly, why should *I* use it?
  190.  
  191.   vi is the default visual editor under Unix, and is therefore shipped
  192. with all recent versions of Unix.  (Recent being defined as post 1984
  193. or so.)  This means that whenever you run across a machine that is
  194. running a Unix of some sort, you will know that you have a powerful
  195. editor at your finger tips.  Why else? vi is a powerful editor.  Also,
  196. once you know vi, you can edit files really quickly, as it is extremely
  197. economical with the keystrokes.  Due to its different modes for
  198. inserting and issuing commands, it is much faster than most non-mode
  199. based editors.  It is also a very small editor.  (The version on my
  200. machine is 200k) Also, it can do almost anything, as long as you know
  201. how to get it to do what you want.
  202.   Furthermore, as an editor it allows a lot of interaction with the
  203. Unix operating system. It allows you to use many of the powerful Unix
  204. utilities from within vi. It's possible to, from within vi, kill off a
  205. list of specific processes that you want to kill. If you haven't given
  206. yourself permission to write to the file you're editing by accident, you
  207. can change the permission without leaving vi.
  208.   vi works by having commands that work on objects. By learning only a
  209. few commands and a few objects you can learn how to do a lot of things,
  210. simply by combining the commands and the objects. Even the commands are
  211. separated into subcategories. For example, the movement commands and
  212. the modification commands are different. If you know that d is the
  213. command to delete, and j is the command to go down a line, and you know
  214. how to combine them (dj), you can delete down one line. When you use
  215. numbers it gets even more powerful. d5j will delete 5 lines below you.
  216. Then, once you learn the character for "paragraph" (}), you already
  217. know how to delete 5 paragraphs.
  218.  
  219.  
  220. 1.2 - Wow!  This sounds great!  Is there any reason not to use vi?
  221.  
  222.   Yes.  There is a very good reason.  It can be somewhat hard to learn,
  223. and until you do so, it will be slow and painful.  Once you learn it,
  224. it will be faster, but the process of learning it is slow.  I've been
  225. asked if vi was an easy editor to learn, whether it was intuitive or
  226. not.  My general response to this question is:  "Yes, some of us think
  227. so.  But most people think that we are crazy."
  228.  
  229. 1.3 - What different operating systems is vi available for?
  230.  
  231.  Unix.  That's it.  (Well, technically, the 4.3BSD version included
  232. support for VMS as well.) However, there are many, many clones of vi
  233. that are available for different operating systems.  I personally have
  234. used vi clones under:  Unix, Dos, OS/2, Mac System 7.  (See below for a
  235. list of specifics.)
  236.  
  237. 1.4 - Okay, you've convinced me.  I'm going to learn vi.  Where do I
  238. start?
  239.  
  240.   Well...  That's a tricky one.  There are many good books out there
  241. that cover vi; most books on Unix have at least one chapter devoted to
  242. it.  There are also many books devoted specifically to vi.  I don't
  243. have any preference, so your best bet might be to ask your local vi
  244. guru where they learned.  Also, play around.  Fire up vi with a
  245. non-important document (your dissertation is NOT a good document to
  246. learn vi with...) and play around.  I can't imagine anyone learning vi
  247. without playing around with it quite a bit.  Remember, if you get
  248. confused, just hit the Escape key a couple of times, and you'll be in
  249. command mode again.
  250.   I should mention at some point, and I guess here is as good a place
  251. as any, that people who think they might want to do Unix system
  252. administration, or any type of configuration of Unix machines will
  253. probably also want to learn ed or ex, as some versions of Unix do not
  254. put vi in the root partition, and one might be stranded without it at
  255. some point.  Ed is a good choice.
  256.  
  257. 1.5 - What are some of the vi clones that are available?
  258.  
  259.   Just to list a few:  STvi (STevie), elvis, vile, vim, and nvi, xvi.
  260.  
  261. elvis is available for: Amiga, DOS, OS/2, Unix, VMS, Atari, 
  262. Psion3a Handheld. 
  263.  
  264. Stratus machines running VOS can get elvis at:
  265.      ftp://ftp.stratus.com/pub/vos/tools/tools.html
  266.  
  267. Lemmy is available for Win95/NT at:
  268.      http://www.accessone.com/~jai
  269.  
  270. nvi is the vi that ships with BSD 4.4. 
  271.  
  272. STevie is available for:  Atari ST, DOS, Unix, Macintosh.  
  273.   MacOS is available at any info-mac mirror in /info-mac/text
  274.     (Such as ftp://ftp.hawaii.edu/pub/mac/info-mac/text)
  275.   Stratus machines running VOS can get STevie at:
  276.      ftp://ftp.stratus.com/pub/vos/tools/tools.html
  277.  
  278.  
  279. vile is available for: BeOS, DOS, OS/2, Unix, VMS, Window95, WindowsNT 
  280.   The source is available at: http://dickey.his.com/vile/vile.html.
  281.   xvile, a X-windows aware version of vile exists, as well.
  282.  
  283. Vim is available for: AmigaOS, Atari MiNT, BeOS, DOS, MacOS,
  284.                       OS/2, Unix, VMS, Windows
  285.   General information, documentation and much more about Vim is on
  286.           http://www.vim.org/             The Vim Pages
  287.   A list of sites which mirror Vim is available at:
  288.           http://www.vim.org/dist.html    Vim Distribution Page
  289.   There are three mailing list for Vim:
  290.           "VIM announcements list"    vim-announce@vim.org
  291.           "VIM help mailing  list"             vim@vim.org
  292.           "VIM development   list"         vim-dev@vim.org
  293.   Information on subscription is on page
  294.           http://www.vim.org/mail.html
  295.  
  296. viper is available for GNU Emacs.
  297.   ftp://ftp.cs.sunysb.edu/pub/TechReports/kifer/viper.tar.Z
  298.  
  299. xvi is available for: DOS, Unix.
  300.  
  301. Mortice Kern Systems (support@mks.com), who I have absolutely no
  302. affiliation with, offers a commercial version of vi for DOS/Windows,
  303. OS/2, and WindowsNT. It is included as part of the MKS Toolkit (a set
  304. of Unix-like tools for Windows) and cannot be purchased separately.
  305.  
  306.   There are some differences between the different vi clones.  Many
  307. offer improvements, but most still allow the commands that are listed
  308. in this document, but there may be some differences.  Refer to the
  309. documentation that comes with the clone for details.
  310.  
  311. 2.0 - Learning vi.
  312.  
  313.   These are some basic hints for the novice vi user.  First, keep a
  314. command summary with you at all times.  A quick reference guide/command
  315. summary is included later on.  Second, get a good book that covers vi.
  316. This document is not the best way to learn it (at least not yet.)  I'm
  317. not sure if this document should teach people to to use vi from
  318. scratch, as there are many good books on it already.  However, there
  319. are hints here.  As for choosing a book, the standard rules apply:
  320. look at it before buying it.  See if any of it makes sense to you.
  321. Make sure that it has exercises that you can practice with.  Compare it
  322. with other books -- after all, vi can be very confusing, and you want
  323. to make sure that it is the book that is confusing, and not just that
  324. you underestimated the difficulty of learning vi.
  325.   Also, seek out vilearn or vitutor, programs designed to teach you the
  326. basics of vi.
  327.   
  328. 2.1 - What games will help me learn vi?
  329.  
  330.   This may seem a bit silly, but there are many games on Unix systems
  331. that can help you learn to use vi.  These help particularly with the
  332. basics.  Although I don't know of any games that help with every vi
  333. command, I do know of a few that will help you learn to use hjkl to
  334. move the cursor around.  NetHack, a rogue-like game, is particularly
  335. good for this, as it is a large game and can be entertaining for quite
  336. some time.  Nethack is available from ftp.uu.net in /pub/games/nethack.
  337. Not to make the other games sound worse, but some other ones are:
  338. rogue, moria, omega, worm, and snake.
  339.  
  340.   Another fun thing to do to learn vi better is to play a game with
  341. friends to figure out the least number of vi keystrokes needed to do a
  342. specific task in vi.
  343.  
  344. 2.2 - What is the difference between Command mode & Insert mode?
  345.  
  346.   Often cited as one of the main problems with vi, and equally often
  347. cited as being one of its best strengths, vi differentiates between a
  348. "Command mode" and an "Insert mode."  Understanding this difference is
  349. VITAL to learning vi.  When one starts vi it starts in command mode.
  350. In this mode, one can move around the file, and issue commands to
  351. change certain areas of the text, cut, copy and paste sections of the
  352. text and do much more.  Insert mode is where one can actually insert
  353. text.  In other words, command mode is used to move around the
  354. document, and insert mode is used to type text into the document.
  355.   Commands such as:  a, i, c, C, O, o and others will switch one from
  356. command mode to insert mode.
  357.   <Esc> or <ctrl-c> will take one out of insert mode and return one to
  358. command mode.
  359.   Get used to this distinction.  It is one of the things that makes vi
  360. different from most other editors.  It also allows one to do a lot of
  361. things without taking one's hands from the standard keyboard position.
  362.   For novices it is often nice to know what mode you are in.  If you
  363. type: "echo set showmode >> $HOME/.exrc" at the Unix command line, it
  364. will make it so that each time vi is started it will default to showing
  365. the mode on the bottom right of the screen.  (To understand what this
  366. is doing, see the sections on "setting up a .exrc" and the "set
  367. options.")  If it does not show anything in the bottom right, it means
  368. that you are in command mode.  I call a few different modes (append,
  369. open and insert) "insert mode" in this document.
  370.  
  371. 2.3 - Wait, my keyboard doesn't have a <Esc> key!  What should I do?
  372.  
  373.   Try hitting <ctrl-[> instead.  If your keyboard has a <Meta> key, try
  374. that.  If you don't have a <Meta> key, many keyboards use <Alt>,
  375. <Diamond>, <Start> or <Menu> as a <Meta> key (however, if you've got
  376. any of those keys, you almost certainly have an <Esc> key).  If neither
  377. of these work, try <ctrl-3>.  Some DEC terminals use the F11 key as
  378. escape.
  379.  
  380. 2.4 - What are all of those ~s?
  381.  
  382.   They're just there to let you know where the bottom of your file is,
  383. they are not actually in your document, and you do not need to worry
  384. about them.
  385.  
  386. 2.5 - I can't get used to using hjkl, do you have any suggestions?
  387.  
  388.   First, if your terminal is set properly and you have a good
  389. connection, you should be able to use the arrow keys.  However, if you
  390. think that you will be using vi a lot, then it makes sense to learn
  391. hjkl, as they are faster to type.  Also, there are occasions where you
  392. may have a bad connection and the ESC sequences may get lost.
  393.   Here is a simple mnemonic to help remember this: j extends below the
  394. line, and k above the line when written by hand.
  395.  
  396. 2.6 - How do I quit without saving?
  397.  
  398.   :q! will do it.
  399.  
  400. 2.7 - How do I insert a file?
  401.  
  402.   :r <filename>
  403.  
  404.   For example, to insert the file /etc/motd, type:  :r /etc/motd
  405.  
  406.   This will insert the file at the current location in the file you are
  407. working on.  If you specify a number before the r, it will insert it at
  408. that location in the file.
  409.  
  410. 2.8 - How do I search for text?
  411.  
  412.   /<text> will search forward.  ?<text> will search backwards.  ?? or
  413. // will repeat the last search.  It is worth noting that these are
  414. pretty much standard in Unix.  In addition, in vi, n will repeat the
  415. last search in the same direction as the original search.  N will
  416. repeat the last search pattern in the opposite direction of the
  417. original search. For example /foo will find the next occurrence of "foo"
  418. from where you are. n will find the one after that. N will take you to
  419. the first occurrence. Hitting N a second time will get the same result
  420. as ?foo would have in the first place. 
  421.   
  422.   Regular Expressions may be used within searches.
  423.  
  424. 2.9 - How do I search for a control sequence?
  425.  
  426.   /<ctrl-v><ctrl-<seq>>
  427.  
  428.  <ctrl-v> will tell vi to take the next character literally, and not to take
  429. it as a command.
  430.  
  431. 2.10 - How do I reformat text?
  432.  
  433.   If your computer has the program fmt on it, all you need to do is
  434. type !}fmt from command mode (without a : before it).  This will
  435. reformat the text from the current location until the end of the
  436. paragraph.  If your machine does not have fmt, you need to find a
  437. similar program.  (I gather there are many such programs available from
  438. the public domain, but I do not know much about them.)
  439.  
  440.   You may also want to look into par. Par is similar to fmt in
  441. function, but has more feature. Par can be downloaded from:
  442. http://www.cs.berkeley.edu/~amc/Par.
  443.  
  444. 2.11 - How do I copy text?
  445.  
  446.   Okay, this might be a bit complicated.  Take from this section what
  447. you can, and reread it a few times.  Also, experiment.
  448.   "<letter>yy will copy one line of text into register <letter>.  (A
  449. register is vi-lingo for a place to store data that was cut or copied.)
  450. <letter> must be between a and z.  "<letter>dd will delete one line and
  451. place it in register <letter>.  You may use a number before the yy or
  452. dd to specify the number of lines.  Using an uppercase <letter> will
  453. append the text into the register leaving what was there before.
  454. "<letter>p will put the text after the cursor.  "<letter>P will put it
  455. before the cursor.  If the register contains the beginning or end of a
  456. line, the line will be placed on another line as appropriate.  Y may be
  457. used as a short cut for yy.  In addition, y$, yH, yM, etc. are valid,
  458. as are the equivalent d commands.  For quick cuts and pastes, no
  459. register need be specified.  In this case, no appending is allowed, and
  460. the register will be removed if another delete command is given.
  461. (Including x).
  462.   For example, to move the previous paragraph, one would go to the top
  463. of the paragraph, type "a13dd, move to the position in which one wishes
  464. to put the paragraph, and then type "ap to put it below the current
  465. line.
  466.   Now, presumably you want to be able to cut and paste into areas that
  467. are not just the end of the line.  In order to do this, use m<letter>
  468. to mark an area.  This letter may be the same as a cut/copy register,
  469. they are stored in different area of memory.  Then, type "<register>[y
  470. or d]`<letter>.  Where <register> is the register to put the text into,
  471. <letter> is the letter used to mark, and y or d as appropriate.
  472.  
  473. 2.12 - Ahhhh!!!  I just hit dG and lost my dissertation!  What can I
  474. do?  (Or, I've just made a mistake, what should I do?)
  475.  
  476.   u will undo the last command.  U will undo changes to the current
  477. line.  (Granted, a one line dissertation would not be much.) :e! will
  478. reload the current document without saving any changes.  In addition,
  479. deleted text gets stored in the registers numbered from 1 to 9.  "<n>p
  480. will put the last nth deletion.  You can quickly search the registers
  481. by trying one, hitting u, and trying the next.  (In order to expedite
  482. this, vi uses . slightly differently than normal.  Instead of repeating
  483. the last command, it will try the next register, so all you need to do
  484. is: "1p u . u ., etc. until you undo the delete you want to undo.)
  485.  
  486. 2.13 - vi appears to be frozen or acting strange, what can I do?
  487. (Also, I can't get rid of the colon prompt, now what?)
  488.  
  489.   If vi appears to have frozen, make sure that you haven't hit <ctrl-S>
  490. by mistake.  In order to undo a <ctrl-s>, hit <ctrl-q>.  
  491.   If there is a colon and you can't get rid of it, you've entered ex by
  492. accident.  Generally this happens by hitting "Q" from command mode.
  493. Just type vi to get back into vi.
  494.  
  495. 2.14 - I've just written my dissertation and have been told that I need
  496. to have each section in a different file, what should I do?
  497.  
  498.  :[m],[n]w <filename> will save between lines m and n to <filename>.
  499. This line numbering method works for almost every : command.  If you
  500. use :[m],[n]w >> <filename> it will append it to the file.
  501.  
  502. 2.15 - What's the deal with all of these : commands?
  503.  
  504.   The commands that follow a : are commands from the ex editor.  These
  505. allow a lot of flexibility and power.  For example, there are many
  506. different ways to search and replace, all of which have some
  507. similarities (in fact, they are in some ways the same...)
  508.  
  509. 2.16 - Argh! vi has gone crazy! None of the commands work! Now what?
  510.  
  511.   The most common time for vi to act insane when it comes to typing is
  512. when the caps lock key has accidentally been hit. Many keyboards on
  513. Unix servers have caps lock and control in the opposite position from
  514. many PCs, causing people to occasionally hit it. Just for fun, try
  515. navigating in vi with the caps lock on sometime.
  516.  
  517. 3.0 - How do you do a search and replace?
  518.  
  519.   Well, there are a few methods.  The simplest is:
  520.  
  521.     :s/old/new/g 
  522.   But, this only does it on the current line...  So:
  523.     :%s/old/new/g 
  524.   In general: 
  525.     :[range]s/old/new/[cgi] 
  526.  
  527.   Where [range] is any line range, including line numbers, $ (end of
  528. file), . (current location), % (current file), or just two numbers with
  529. a comma or semicolon between them.  (Or even: .,+5 to mean the next
  530. five lines).
  531.  
  532.   [cgi] is either c, g, i, or nothing.  c tells vi to prompt you before
  533. the changes, g to change all of the occurrences on a line.  (type yes
  534. to tell vi to change it.)  i tells vi to be case insensitive on the
  535. search.  The g after the last slash tells it to replace more than just
  536. the first occurrence on each line.
  537.  
  538.   Another method is:
  539.     :g/foobar/s/bar/baz/g This searches for foobar, and changes it to
  540. foobaz.  It will leave jailbars alone, which the other method will not.
  541. Unfortunately, if jailbars appears on the same line as foobar, it will
  542. change, too.
  543.  
  544.   Of course you can also use regular expression search patterns, and a
  545. few other commands in the replacement part of the text.  If you use \(
  546. and \) in the pattern to escape a sequence (and use \1, \2, etc., you
  547. can do lots of nifty things.
  548.  
  549.   For example:
  550.     :g/foo/s/^\([^ ]*\) \([^ ]*\)/\2 \1/
  551.   will swap the first and second words on every line containing "foo".
  552.  
  553.   Special sequences allowed are:
  554.     &        everything which was matched by the search 
  555.     \[1-9]   The contents of the 1st-9th \(\) pair 
  556.     \u       The next character will be made uppercase 
  557.     \U       The characters until \e or \E will be made uppercase 
  558.     \l       The next character will be made lowercase
  559.     \L       The characters until \e or \E will be made lowercase
  560.     \[eE]    end the selection for making upper or lowercase
  561.  
  562.  
  563. 3.1 - My / key is broken!  How can I search and replace?
  564.  
  565.   Well, okay, it doesn't really need to be a /.  Lots of things will
  566. work fine.  (letters, numbers and a few other things won't...)
  567.  
  568. 3.2 - How do I run a program from within vi?
  569.  
  570.   :!cmd will run the program cmd.  :sh will run an interactive shell.
  571. Within this shell, you may, if you want, run vi again.  This is
  572. particularly useful when you are editing makefiles and config files for
  573. programs in an attempt to get a program to compile.  The advantage over
  574. :e is that you do not need to save the file, and it will be in its old
  575. place when you exit the shell.  (I advise saving the file anyway...)
  576.  
  577. 3.3 - Ahhh!!  I was writing my dissertation, and the computer crashed!
  578.  
  579.   Well, you should get mail about this, but you should be able to
  580. recover the file by typing vi -r <filename> where <filename> is the
  581. name of the file that you were editing at the time of the crash.  vi -r
  582. will give you a list of files that can be recovered.
  583.  
  584. 3.4 - Any tips for making vi programmer friendly?
  585.  
  586.   :set ai will make it auto-indent for you.  
  587.   :set sw=# where # is a number will set the shiftwidth (tabwidth).  
  588. You can then use <<, >> to shift a line left or right.  Plus, you 
  589. can use <% to shift a {, ( or [ set left or right (with >%).  
  590. You must be on top of the specific {, }, (, ), [ or ] of the pair 
  591. to shift them.  
  592.   :set sm will show the matching {, ( or [ when you type the closing
  593. one.  
  594.   :set lisp will make some changes that are useful for lisp
  595. programming.  () will move back and forth over s-expressions, and {}
  596. will move without stopping at atoms.
  597.  
  598. 3.5 - Macros -- How do I write them?
  599.  
  600.   :map <lhs> <rhs> where <lhs> is up to ten characters and <rhs> is up
  601. to 100.  This will make it so that whenever you type <lhs> it will
  602. replace it with <rhs>.  All macros should start in command mode (except
  603. those defined with map!), but may end in any mode you desire.  Remember
  604. to use <ctrl-v> before any control characters that you may use.
  605.   If you are using an Ex command, such as |, it needs to be escaped
  606. while vi is scanning the line.  You should map it as :map foo
  607. <ctrl-v><ctrl-v>|.
  608.   :unmap <lhs> will remove the macro.  :map! <lhs> <rhs> will make
  609. <lhs> insert <rhs> into the text of the document.  map! macros may have
  610. lhs's that are much longer.
  611.  
  612. 3.6 - How do I make a function key a Macro?
  613.  
  614.   If <lhs> is #n where n is 0-9, it will be mapped to the appropriate
  615. function key.
  616.  
  617. 3.7 - Is there anyway to abbreviate text?
  618.  
  619.   Yep, of course.  This is vi, it can do anything.  :ab email
  620. ellidz@midway.uchicago.edu will make it so that whenever you type
  621. email as a specific word, it will extend it to the entire
  622. unabbreviated word.  :una email will unabbreviate it.
  623.  
  624. 3.8 - How do I spell check the current document?
  625.  
  626.   Here is a macro to do it.  These should be put in your .exrc file.
  627. (More on .exrc files later on.) It is a pretty simple macro, it just
  628. calls ispell on the current file.  Of course, to use this you need
  629. ispell on your system.  To use it, just hit V with vi.  (V is not used
  630. by vi, so it makes a good key.)
  631.  
  632.   map V :w<enter>:!ispell % <enter>:e!<enter><enter>
  633.  
  634. The second <enter> makes it so that one does not need to hit return
  635. after it is done checking the spelling.
  636.  
  637. 3.9 - How do I get rid the ^M's at the end of each line of my file?
  638. How do I make a macro to do it?
  639.  
  640.   These generally appear from DOS files that get converted to Unix.
  641. They're easy to get rid of.
  642.   :%s/<ctrl-v><enter>//g will do it.  
  643.  
  644.   The macro bit is a bit trickier.  Not something that most people can
  645. guess on their own.  Here it is:
  646.   map v :%s/<ctrl-v><ctrl-v><ctrl-v><ctrl-v><ctrl-v><enter>//g
  647.  
  648.   Yes, that's right.  5 of them.  The last one is to escape the enter.
  649. Two of the other four are to escape the other two <ctrl-v>'s.  Since the
  650. macro gets read on the ex line, you need to have two <ctrl-v>'s (since you
  651. are searching for <ctrl-v> <enter> to replace it with nothing...)
  652.  
  653.  In general, if you are having trouble with a macro that uses the ex
  654. line, you need to escape things a lot.  Sometimes it is quicker to just
  655. keep throwing <ctrl-v>'s in until it works.
  656.  
  657. 3.10 - I've got a hardcopy terminal, can I still use vi?
  658.  
  659.   Okay, okay, so I don't expect anyone to actually ask this...  But, I
  660. thought it was bizarre enough to throw in anyway.  (And, it actually
  661. answers a very common question...)
  662.   vi will start up in a specific mode, called "open mode" in this
  663. situation.  Things work more or less the same.  Deleted characters will
  664. appear on your print out as \'s.  vi will act as if the size of the
  665. window is only one line.  ^r will retype the current line.  z redraws
  666. the window around the current line.
  667.  
  668. 3.11 - Oh, okay, is THAT what open mode is?  But I don't have a
  669. hardcopy terminal, and it still starts in open mode!
  670.  
  671.   Well, what is happening here is that vi doesn't know what type of
  672. terminal you have.  It decides that in this situation the best thing to
  673. do is to assume that you have the worst terminal possible.  This might
  674. not seem useful, as not very many people need open mode, but it also is
  675. the mode that needs to know the least information about your terminal.
  676.   Now, how to deal with it.  It is possible to change it for the
  677. specific session, but in general, this is not useful.  If you know
  678. your terminal type, you can set it from the Unix prompt (setenv TERM
  679. <termtype> under csh and it's variants, and:  TERM=<termtype> ; export
  680. TERM under sh and its variants.).
  681.   Better yet would be to edit your .profile or .cshrc to include this so
  682. it is automatically done for you when you login.  (Of course, you need
  683. to either know ed or be able to set it at the Unix prompt before you'll
  684. be able to edit the file...)
  685.   If you do not know your terminal type, try vt100.  Most modern
  686. terminals and terminal emulators can emulate vt100.  If this does not
  687. work, find someone to help you.
  688.  
  689. 3.12 - How can I get the source code to vi?
  690.  
  691.   The source code to the original vi is available from:
  692. <http://ex-vi.berlios.de>.
  693.  
  694.   You may also want to look at the source for some of the various vi 
  695. clones.
  696.  
  697. 4.0 - More advanced topics in vi.
  698.  
  699. 4.1 - I see that sections are defined by SHNHH HU and paragraphs by
  700. IPLPPPQPPLIbp by default? What language is that in? What in the world
  701. does it mean?
  702.  
  703.   Man pages are written in a language called nroff/troff. Nroff is a
  704. general purpose text formatting language, similar to TeX. What does
  705. this have to do with vi and the sections? Use the Unix more command to
  706. look at a man page (say, /usr/man/man1/vi.1 or something). You'll see
  707. parts of the page split into sections, like .SH Name. The .SH is
  708. defining a section of the man page. Notice that SH is the first two
  709. characters of the section set option. 
  710.   The section and paragraph sections are defined by a listing of pairs
  711. of macros for nroff/troff. The next section will start with a period
  712. and then any of the pairs of letters that the section is defined as.
  713. Paragraphs work the same way -- except that a blank line is also
  714. considered a paragraph break.
  715.  
  716. 4.2 - But when I program, I've noticed that I can use ]] and [[ to move
  717. between functions. My functions don't have .SH or anything at the
  718. beginning, what gives?
  719.  
  720.   Luckily for many C programmers, an open curly brace at the beginning
  721. of a line is also defined as a section break.
  722.  
  723. 4.3 - How do I work with multiple files at the same time?
  724.  
  725.   vi allows for multiple files to be open at the same time. Unlike many
  726. editors, you only see one file at a time. If you start vi with multiple
  727. files on the command line they'll all be opened up. You'll start out in
  728. the first file, and you can move to the next file by typing :n. If
  729. you've made changes to the current file that you don't want to keep,
  730. you can go on by using :n!. Note that this will discard any changes
  731. that you've made. If you want to save, you need to use :w first.
  732.  
  733. 4.4 - So, I've started up vi, but now I want to be able to edit another
  734. file. How do I do this?
  735.  
  736.   :n <filename> will load that file into vi. You'll need to save your
  737. current file.
  738.  
  739. 4.5 - How can I get back to the first file, though?
  740.  
  741.   :e # will edit the last file that you were editing. You can use this
  742. to swap back and forth quickly between two different files. If you are
  743. editing many files, you can use :n to cycle between them.
  744.  
  745. 4.6 - But I'm only wanting to edit one file at a time. I've got one
  746. open and I want to switch permanently to another file. How do I do that?
  747.  
  748.   Just use :e <filename> and it will load it up.
  749.  
  750. 4.7 - How do I add another file into the middle of my current file? 
  751.  
  752.   :r <filename> will read in a new file at the current location. It is
  753. worth noting that Unix in general deals with the output of commands as
  754. files in many respects. Because of this, you can easily read in the
  755. output of a command into the file: :r !<command> will read in the
  756. output.
  757.  
  758. Copyright, E. Larry Lidz, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
  759. 2002, 2003.  
  760. All Rights Reserved.
  761.