home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / EDITOR / NVI179B / NVI179B.ZIP / docs / tutorial / vi.advanced next >
Text File  |  1994-01-23  |  71KB  |  1,459 lines

  1. Section 26: Index to the rest of the tutorial
  2.  
  3. The remainder of the tutorial can be perused at your leisure.  Simply find the
  4. topic of interest in the following list, and {/Section xx:/^M} to get to the
  5. appropriate section.  (Remember that ^M means the return key) 
  6.  
  7. The material in the following sections is not necessarily in a bottom up
  8. order.  It should be fairly obvious that if a section mentions something with
  9. which you are not familiar, say, buffers, you might {/buffer/^M} followed by
  10. several {n} to do a keyword search of the file for more details on that item.
  11. Another point to remember is that commands are surrounded by curly-braces and
  12. can therefore be found rather easily.  To see where, say, the X command is
  13. used try {/{X}/^M}.  Subsequent {n} will show you other places the command was
  14. used.  We have tried to maintain the convention of placing the command letter
  15. surrounded by curly-braces on the section line where that command is
  16. mentioned.
  17.  
  18. Finally, you should have enough 'savvy' at this point to be able to do your
  19. own experimentation with commands without too much hand-holding on the part of
  20. the tutorial.  Experimentation is the best way to learn the effects of the
  21. commands.
  22.  
  23.  Section      Topic - description
  24.  -------      -------------------
  25. (Sections 1 through 25 are located in the file vi.beginner.)
  26.     1         introduction: {^F} {ZZ}
  27.     2         introduction (con't) and positioning: {^F} {^B}
  28.     3         introduction (con't) and positioning: {^F} {^B}
  29.     4         positioning: {^F} {^B} ^M (return key)
  30.     5         quitting: {:q!} ^M key
  31.     6         marking, cursor and screen positioning: {m} {G} {'} {z}
  32.     7         marking, cursor and screen positioning: {m} {G} {'} {z}
  33.     8         marking, cursor and screen positioning: {z} {m} {'}
  34.     9         marking and positioning: {m} {''}
  35.    10         line positioning: {^M} {-}
  36.    11         scrolling with {^M}
  37.    12         scrolling with {-} and screen adjustment {z}
  38.    13         notes on use of tutorial
  39.    14         other scrolling and postioning commands: {^E} {^Y} {^D} {^U}
  40.    15         searching: {/ .. /^M}
  41.    16         searching: {? .. ?^M} {n} (in search strings ^ $)
  42.    17         searching: \ and magic-characters in search strings
  43.    18         colon commands, exiting: {:} {ZZ}
  44.    19         screen positioning: {H} {M} {L}
  45.    20         character positioning: {w} {b} {0} {W} {B} {e} {E} {'} {`}
  46.    21         cursor positioning: {l} {k} {j} {h}
  47.    22         adding text: {i} {a} {I} {A} {o} {O} ^[ (escape key)
  48.    23         character manipulation: {f} {x} {X} {w} {l} {r} {R} {s} {S} {J}
  49.    24         undo: {u} {U}
  50.    25         review
  51. (The following sections are in this file.)
  52.    26         Index to the rest of the tutorial ******** YOU ARE HERE *******
  53.    27         discussion of repeat counts and the repeat command: {.}
  54.    28         more on low-level character motions: {t} {T} {|}
  55.    29         advanced correction operators: {d} {c}
  56.    30         updating the screen: {^R}
  57.    31         text buffers: {"}
  58.    32         rearranging and duplicating text: {p} {P} {y} {Y}
  59.    33         recovering lost lines
  60.    34         advanced file manipulation with vi
  61.    34.1          more than one file at a time: {:n}
  62.    34.2          reading files and command output: {:r}
  63.    34.3          invoking vi from within vi: {:e} {:vi}
  64.    34.4          escaping to a shell: {:sh} {:!}
  65.    34.5          writing parts of a file: {:w}
  66.    34.6          filtering portions of text: {!}
  67.    35         advanced searching: magic patterns 
  68.    36         advanced substitution: {:s} 
  69.    37         advanced line addressing: {:p} {:g} {:v}
  70.    38         higher level text objects and nroff: ( ) { } [[ ]]
  71.    39         more about inserting text
  72.    40         more on operators: {d} {c} {<} {>} {!} {=} {y}
  73.    41         abbreviations: {:ab}
  74.    42         vi's relationship with the ex editor: {:}
  75.    43         vi on hardcopy terminals and dumb terminals: open mode
  76.    44         options: {:set} {setenv EXINIT}
  77.    44.1          autoindent
  78.    44.2          autoprint
  79.    44.3          autowrite
  80.    44.4          beautify
  81.    44.5          directory
  82.    44.6          edcompatible
  83.    44.7          errorbells
  84.    44.8          hardtabs
  85.    44.9          ignorecase
  86.    44.10         lisp
  87.    44.11         list
  88.    44.12         magic
  89.    44.13         mesg                    
  90.    44.14         number
  91.    44.15         open
  92.    44.16         optimize
  93.    44.17         paragraphs
  94.    44.18         prompt
  95.    44.19         readonly
  96.    44.20         redraw
  97.    44.21         remap
  98.    44.22         report
  99.    44.23         scroll
  100.    44.24         sections
  101.    44.25         shell
  102.    44.26         shiftwidth
  103.    44.27         showmatch
  104.    44.28         slowopen
  105.    44.29         tabstop
  106.    44.30         tags
  107.    44.31         taglength
  108.    44.32         term
  109.    44.33         terse
  110.    44.34         timeout
  111.    44.35         ttytype
  112.    44.36         warn
  113.    44.37         window
  114.    44.38         wrapscan
  115.    44.39         wrapmargin
  116.    44.40         writeany
  117.    44.41         w300, w1200, w9600
  118.  
  119. Section 27: repetition counts and the repeat command {.}
  120.  
  121. Most vi commands will use a preceding count to affect their behavior in some
  122. way.  We have already seen how {3x} deletes three characters, and {22G} moves
  123. us to line 22 of the file.  For almost all of the commands, one can survive by
  124. thinking of these leading numbers as a 'repeat count' specifying that the
  125. command is to be repeated so many number of times.
  126.  
  127. Other commands use the repeat count slightly differently, like the {G} command
  128. which use it as a line number.
  129.  
  130. For example:
  131.  
  132. {3^D} means scroll down in the file three lines.  Subsequent {^D} OR {^U} will
  133. scroll only three lines in their respective directions!
  134.  
  135. {3z^M} says put line three of the file at the top of the screen, while {3z.}
  136. says put line three as close to the middle of the screen as possible.
  137.  
  138. {50|} moves the cursor to column fifty in the current line.
  139.  
  140. {3^F} says move forward 3 screenfulls.  This is a repetition count.  The
  141. documents advertise that {3^B} should move BACK three screenfulls, but I
  142. can't get it to work.
  143.  
  144. Position the cursor on some text and try {3r.}.  This replaces three characters
  145. with '...'.  However, {3s.....^[} is the same as {3xi.....^[}.
  146.  
  147. Try {10a+----^[}.
  148.  
  149. A very useful instance of a repetition count is one given to the '.' command,
  150. which repeats the last 'change' command.  If you {dw} and then {3.}, you will
  151. delete first one and then three words.  You can then delete two more words with
  152. {2.}.  If you {3dw}, you will delete three words.  A subsequent {.} will delete
  153. three more words.  But a subsequent {2.} will delete only two words, not three
  154. times two words.
  155.  
  156. Caveat: The author has noticed that any repetition count with {^B} will NOT
  157. work: indeed, if you are at the end of your file and try {3^B} sufficiently
  158. often, the editor will hang you in an infinite loop.  Please don't try it:
  159. take my word for it.
  160.  
  161. Section 28: {t} {T} {|}
  162.  
  163. Position the cursor on line 13 below:
  164.  
  165. Line 13: Four score and seven years ago, our forefathers brought ...
  166.  
  167. Note that {fv} moves the cursor on/over the 'v' in 'seven'.  Do a {0} to return
  168. to the beginning of the line and try a {tv}.  The cursor is now on/over the
  169. first 'e' in 'seven'.  The {f} command finds the next occurrence of the
  170. specified letter and moves the cursor to it.  The {t} command finds the
  171. specified letter and moves the cursor to the character immediately preceding
  172. it.  {T} searches backwards, as does {F}.
  173.  
  174. Now try {60|}: the cursor is now on the 'o' in 'brought', which is the
  175. sixtieth character on the line.
  176.  
  177. Section 29: {d} {c}
  178.  
  179. Due to their complexity we have delayed discussion of two of the most powerful
  180. operators in vi until now.  Effective use of these operators requires more
  181. explanation than was deemed appropriate for the first half of the tutorial.
  182.  
  183. {d} and {c} are called operators instead of commands because they consist of
  184. three parts: a count specification or a buffer specification (see section
  185. #BUFFERS), the {d} or {c}, and the object or range description.  We will not
  186. discuss buffers at this stage, but will limit ourselves to count
  187. specifications.  Examples speak louder than words: position the cursor at the
  188. beginning of line 14:
  189.  
  190. Line 14: Euclid alone has looked on beauty bear.
  191.  
  192. Obviously, there is something wrong with this quotation.  Type {2fb} to
  193. position the cursor on the 'b' of 'bear'.  Now, type {cwbare^[}
  194. and observe the results.  The {cw} specifies that the change command {c} is to
  195. operate on a word object.  More accurately, it specifies that the range of the
  196. change command includes the next word.
  197.  
  198. Position the cursor on the period in Line 14. (one way is to use {f.})
  199. Now, type {cbbeast^[}.  This specifies the range of the change command to be the
  200. previous word (the 'b' reminiscent of the {b} command).  If we had wished to
  201. delete the word rather than change it, we would have used the {d} operator,
  202. rather than the {c} operator.
  203.  
  204. Position the cursor at the beginning of the line with {0}.  Type
  205. {d/look/^M}.  The search string specified the range of the delete.
  206. Everything UP TO the word 'looking' was deleted from the line.
  207.  
  208. In general, almost any command that would move the cursor will specify a range
  209. for these commands.  The most confusing exception to this rule is when {dd} or
  210. {cc} is entered: they refer to the whole line.  Following is a summary of the
  211. suffixes (suffices? suffici?) and the ranges they specify:
  212.  
  213.     suffix        will delete{d}/change{c}
  214.     ------        ------------------------
  215.       ^[            cancels the command
  216.       w             the word to the right of the cursor
  217.       W             ditto, but ignoring punctuation
  218.       b             the word to the left of the cursor
  219.       B             ditto, but ignoring punctuation
  220.       e             see below.
  221.       E               ditto
  222.       (space)       a character
  223.       $             to the end of the line
  224.       ^             to the beginning of the line
  225.       / .. /        up to, but not including, the string
  226.       ? .. ?        back to and including the string
  227.       fc            up to and including the occurrence of c 
  228.       Fc            back to and including the occurrence of c
  229.       tc            up to but not including the occurrence of c
  230.       Tc            back to but not including the occurrence of c
  231.       ^M            TWO lines (that's right: two)
  232.       (number)^M    that many lines plus one
  233.       (number)G     up to and including line (number)
  234.       (             the previous sentence if you are at the beginning of
  235.                     the current sentence, or the current sentence up to where 
  236.                     you are if you are not at the beginning of the current 
  237.                     sentence.  Here, 'sentence' refers to the intuitive
  238.                     notion of an English sentence, ending with '!', '?',
  239.                     or '.' and followed by an end of line or two spaces.
  240.       )             the rest of the current sentence
  241.       {             analogous to '(', but in reference to paragraphs:
  242.                     sections of text surrounded by blank lines
  243.       }             analogous to ')', but in reference to paragraphs
  244.       [[            analogous to '(', but in reference to sections
  245.       ]]            analogous to ')', but in reference to sections
  246.       H             the first line on the screen
  247.       M             the middle line on the screen
  248.       L             the last line on the screen
  249.       3L            through the third line from the bottom of the screen
  250.       ^F            forward a screenful
  251.       ^B            backward a screenful
  252.       :
  253.       :  etc. etc. etc.
  254.  
  255. This list is not exhaustive, but it should be sufficient to get the idea
  256. across: after the {c} or {d} operator, you can specify a range with another
  257. move-the-cursor command, and that is the region of text over which the command
  258. will be effective.
  259.  
  260. Section 30: updating the screen {^R}
  261.  
  262. Vi tries to be very intelligent about the type of terminal you are working on
  263. and tries to use the in-terminal computing power (if any) of your terminal.
  264. Also if the terminal is running at a low baud rate (say 1200 or below), vi sets
  265. various parameters to make things easier for you.  For example, if you were
  266. running on a 300 baud terminal (that's 30 characters per second transmission
  267. rate) not all 24 lines of the screen would be used by vi.  In addition, there
  268. is a large portion of the editor keeping track of what your screen currently
  269. looks like, and what it would look like after a command has been executed.  Vi
  270. then compares the two, and updates only those portions of the screen that have
  271. changed.
  272.  
  273. Furthermore, some of you may have noticed (it depends on your terminal) that 
  274. deleting lines or changing large portions of text may leave some lines on the 
  275. screen looking like: 
  276. meaning that this line of the screen does not correspond to any line in your
  277. file. It would cost more to update the line than to leave it blank for the
  278. moment.  If you would like to see your screen fully up-to-date with the
  279. contents of your file, type {^R}.
  280.  
  281. To see it in action, delete several lines with {5dd}, type {^R}, and then type
  282. {u} to get the lines back.
  283.  
  284. Here is as good a place as any to mention that if the editor is displaying the
  285. end of your file, there may be lines on the screen that look like: 
  286. indicating that that screen line would not be affected by {^R}.  These lines
  287. simply indicate the end of the file.
  288.  
  289. Section 31: text buffers {"}
  290.  
  291. Vi gives you the ability to store text away in "buffers".  This feature is very
  292. convenient for moving text around in your file.  There are a total of thirty-
  293. five buffers available in vi.  There is the "unnamed" buffer that is used by all
  294. commands that delete text, including the change operator {c}, the substitute
  295. and replace commands {s} and {r}, as well as the delete operator {d} and delete
  296. commands {x} and {X}.  This buffer is filled each time any of these commands
  297. are used. However, the undo command {u} has no effect on the unnamed buffer.
  298.  
  299. There are twenty-six buffers named 'a' through 'z' which are available for the
  300. user.  If the name of the buffer is capitalized, then the buffer is not
  301. overwritten but appended to.  For example, the command {"qdd} will delete one
  302. line and store that line in the 'q' buffer, destroying the previous contents of
  303. the buffer.  However, {"Qdd} will delete one line of text and append that line
  304. to the current contents of the 'q' buffer.
  305.  
  306. Finally, there are nine buffers named '1' through '9' in which the last nine
  307. deletes are stored.  Buffer 1 is the default buffer for the modify commands and
  308. is sometimes called the unnamed buffer.
  309.  
  310. To reference a specific buffer, use the double-quote command {"} followed by
  311. the name of the buffer.  The next two sections show how buffers can be used to
  312. advantage.
  313.  
  314. Section 32: rearranging and duplicating text: {y} {Y} {p} {P}
  315.  
  316. Position yourself on line 15 below and {z^M}:
  317.  
  318. Line 15: A tree as lovely as a poem ...
  319. Line 16: I think that I shall never see
  320.  
  321. Type {dd}.  Line 15 has disappeared and been replaced with the empty line (one
  322. with the single character @ on it) or (again depending on your terminal) Line
  323. 16 has moved up and taken its place.  We could recover Line 15 with an undo
  324. {u} but that would simply return it to its original location.  Obviously, the
  325. two lines are reversed, so we want to put line 15 AFTER line 16.  This is
  326. simply done with the put command {p}, which you should type now.  What has
  327. happened is that {dd} put Line 15 into the unnamed buffer, and the {p} command
  328. retrieved the line from the unnamed buffer.
  329.  
  330. Now type {u} and observe that Line 15 disappears again (the put was undone
  331. without affecting the unnamed buffer).  Type {P} and see that the capital {P}
  332. puts the line BEFORE the cursor.
  333.  
  334. To get Line 15 where it belongs again type {dd}{p}.
  335.  
  336. Also in Line 15 note that the words 'tree' and 'poem' are reversed.  Using the
  337. unnamed buffer again: {ft}{dw}{ma}{fp}{P}{w}{dw}{`aP} will set things aright 
  338. (note the use of the reverse quote).
  339.  
  340. The put commands {p} and {P} do not affect the contents of the buffer.
  341. Therefore, multiple {p} or {P} will put multiple copies of the unnamed buffer
  342. into your file.
  343.  
  344. Experiment with {d} and {p} on words, paragraphs, etc.  Whatever {d}
  345. deletes, {p} can put.
  346.  
  347. Position the cursor on Line 17 and {z^M}:
  348.  
  349. Line 17: interest apple cat elephant boy dog girl hay farmer
  350.  
  351. Our task is to alphabetize the words on line 17.  With the named buffers (and a
  352. contrived example) it is quite easy:
  353.  
  354. {"idw}{"adw}{"cdw}{"edw}{"bdw}{"ddw}{"gdw}{"hdw}{"fdw}
  355.  
  356. stores each of the words in the named buffer corresponding to the first letter
  357. of each of the words ('interest' goes in buffer "i, 'apple' goes in buffer "a,
  358. etc.).  Now to put the words in order type:
  359.  
  360. {"ap$}{"bp$}{"cp$}{"dp$}{"ep$}{"fp$}{"gp$}{"hp$}{"ip$}
  361.  
  362. Notice that, because 'farmer' was at the end of the line, {dw} did not include
  363. a space after it, and that, therefore, there is no space between 'farmer' and
  364. 'girl'.  This is corrected with {Fg}{i ^[}.
  365.  
  366. This example could have been done just as easily with lines as with
  367. words.
  368.  
  369. You do not have to delete the text in order to put it into a buffer.  If all
  370. you wish to do is to copy the text somewhere else, don't use {d}, rather use
  371. the yank commands {y} or {Y}.  {y} is like {d} and {c} - an operator rather
  372. than a command.  It, too, takes a buffer specification and a range
  373. specification.  Therefore, instead of {dw}{P} to load the unnamed buffer with a
  374. word without deleting the word, use {yw} (yank a word).
  375.  
  376. {Y} is designed yank lines, and not arbitrary ranges.  That is, {Y} is
  377. equivalent to {yy} (remember that operators doubled means the current line),
  378. and {3Y} is equivalent to {3yy}.
  379.  
  380. If the text you yank or modify forms a part of a line, or is an object such as
  381. a sentence which partially spans more than one line, then when you put the text
  382. back, it will be placed after the cursor (or before if you use {P}).  If the
  383. yanked text forms whole lines, they will be put back as whole lines, without
  384. changing the current line.  In this case, the put acts much like the {o} or {O}
  385. command.
  386.  
  387. The named buffers "a through "z are not affected by changing edit files.
  388. However, the unnamed buffer is lost when you change files, so to move text from
  389. one file to another you should use a named buffer.
  390.  
  391. Section 33: recovering lost lines
  392.  
  393. Vi also keeps track of the last nine deletes, whether you ask for it or not.
  394. This is very convenient if you would like to recover some text that was
  395. accidentally deleted or modified.  Position the cursor on line 18 following,
  396. and {z^M}.
  397.  
  398.  
  399. Line 18: line 1
  400. Line 19: line 2
  401. Line 20: line 3
  402. Line 21: line 4
  403. Line 22: line 5
  404. Line 23: line 6
  405. Line 24: line 7
  406. Line 25: line 8
  407. Line 26: line 9
  408. Type {dd} nine times: now don't cheat with {9dd}!  That is totally different.
  409.  
  410. The command {"1p} will retrieve the last delete.  Furthermore, when the
  411. numbered buffers are used, the repeat-command command {.} will increment the
  412. buffer numbers before executing, so that subsequent {.} will recover all nine
  413. of the deleted lines, albeit in reverse order.  If you would like to review the
  414. last nine deletes without affecting the buffers or your file, do an undo {u}
  415. after each put {p} and {.}:
  416.  
  417. {"1p}{u}{.}{u}{.}{u}{.}{u}{.}{u}{.}{u}{.}{u}{.}{u}{.}
  418.  
  419. will show you all the buffers and leave them and your file intact.
  420.  
  421. If you had cheated above and deleted the nine lines with {9dd}, all nine lines
  422. would have been stored in both the unnamed buffer and in buffer number 1. 
  423. (Obviously, buffer number 1 IS the unnamed buffer and is just the default
  424. buffer for the modify commands.)
  425.  
  426. Section 34: advanced file manipulation: {:r} {:e} {:n} {:w} {!} {:!}
  427.  
  428. We've already looked at writing out the file you are editing with the
  429. {:w} command.  Now let's look at some other vi commands to make editing
  430. more efficient.
  431.  
  432. Section 34.1: more than one file at a time {:n} {:args}
  433.  
  434. Many times you will want to edit more than one file in an editing session.
  435. Instead of entering vi and editing the first file, exiting, entering vi and
  436. editing the second, etc., vi will allow you to specify ALL files that you wish
  437. to edit on the invocation line.  Therefore, if you wanted to edit file1 and
  438. file2:
  439.  
  440. % vi file1 file2
  441.  
  442. will set up file1 for editing.  When you are done editing file one, write it
  443. out {:w^M} and then type {:n^M} to get the next file on the list.  On large
  444. programming projects with many source files, it is often convenient just to
  445. specify all source files with, say:
  446.  
  447. % vi *.c
  448.  
  449. If {:n^M} brings in a file that does not need any editing, another {:n^M}
  450. will bring in the next file.
  451.  
  452. If you have made changes to the first file, but decide to discard these changes
  453. and proceed to the next file, {:n!^M} forces the editor to discard the current
  454. contents of the editor.
  455.  
  456. You can specify a new list of files after {:n}; e.g., {:n f1 f2 f3^M}.  This
  457. will replace the current list of files (if any).
  458.  
  459. You can see the current list of files being edited with {:args^M}.
  460.  
  461. Section 34.2: reading files and command output: {:r}
  462.  
  463. Typing {:r fname^M} will read the contents of file fname into the editor and
  464. put the contents AFTER the cursor line.
  465.  
  466. Typing {:r !cmd^M} will read the output of the command cmd and place that
  467. output after the cursor line.
  468.  
  469. Section 34.3: invoking vi from within vi: {:e} {:vi}
  470.  
  471. To edit another file not mentioned on the invocation line, type {:e filename^M}
  472. or {:vi filename^M}.  If you wish to discard the changes to the current file,
  473. use the exclamation point after the command, e.g. {:e! filename^M}.
  474.  
  475. Section 34.4: escaping to a shell: {:sh} {:!} {^Z}
  476.  
  477. Occasionally, it is useful to interrupt the current editing session to perform
  478. a UNIX task.  However, there is no need to write the current file out, exit
  479. the editor, perform the task, and then reinvoke the editor on the same file.
  480. One thing to do is to spin off another process.  If there are several UNIX
  481. commands you will need to execute, simply create another shell with {:sh^M}.
  482. At this point, the editor is put to sleep and will be reawakened when you log
  483. out of the shell.
  484.  
  485. If it is a single command that you want to execute, type {:!cmd^M}, where cmd
  486. is the command that you wish to run.  The output of the command will come to
  487. the terminal as normal, and will not be made part of your file.  The message
  488. "[Hit return to continue]" will be displayed by vi after the command is
  489. finished.  Hitting return will then repaint the screen.  Typing another
  490. {:!cmd^M} at this point is also acceptable.
  491.  
  492. However, there is a quicker, easier way: type {^Z}.  Now this is a little
  493. tricky, but hang in there.  When you logged into UNIX, the first program you
  494. began communicating with was a program that is called a "shell" (i.e. it 'lays
  495. over' the operating system protecting you from it, sort of like a considerate
  496. porcupine).  When you got your first prompt on the terminal (probably a '%'
  497. character) this was the shell telling you to type your first command.  When
  498. you typed {vi filename} for some file, the shell did not go away, it just went
  499. to sleep.  The shell is now the parent of vi.  When you type {^Z} the editor
  500. goes to sleep, the shell wakes up and says "you rang?" in the form of another
  501. prompt (probably '%').  At this point you are talking to the shell again and
  502. you can do anything that you could before including edit another file!  (The
  503. only thing you can't do is log out: you will get the message "There are
  504. stopped jobs.")
  505.  
  506. When your business with the shell is done, type {fg} for 'foreground' and the
  507. last process which you ^Z'd out of will be reawakened and the shell will go
  508. back to sleep.  I will refer you to the documentation for the Berkeley shell
  509. 'csh' for more information on this useful capability.
  510.  
  511. Section 34.5: writing parts of a file: {:w}
  512.  
  513. The {:w} command will accept a range specifier that will then write only a
  514. selected range of lines to a file.  To write this section to a file, position
  515. the cursor on the section line (e.g. {/^Section 34.5:/^M}) and {z^M}.  Now type
  516. {^G} to find out the line number (it will be something like "line 513").  Now
  517. {/^Section 34.6:/-1^M} to find the last line of this section, and {^G} to find
  518. its line number (it will be something like 542).  To write out this section of
  519. text by itself to a separate file which we will call "sepfile", type
  520. {:510,542w sepfile^M}.  If sepfile already exists, you will have to use the
  521. exclamation point: {:1147,1168w! sepfile^M} or write to a different, non-
  522. existent file.
  523.  
  524. {:!cat sepfile^M} will display the file just written, and it should be the
  525. contents of this section.
  526.  
  527. There is an alternate method of determining the line numbers for the write.
  528. {:set number^M} will repaint the screen with each line numbered.  When the file
  529. is written and the numbers no longer needed, {:set nonumber^M} will remove the
  530. numbers, and {^R} will adjust the screen.
  531.  
  532. Or, if you remember your earlier lessons about marking lines of text,
  533. mark the beginning and ending lines.  Suppose we had used {ma} to mark the
  534. first line of the section and {mb} to mark the last.  Then the command
  535. {:'a,'bw sepfile^M} will write the section into "sepfile".  In general,
  536. you can replace a line number with the 'name' of a marked line (a single-quote
  537. followed by the letter used to mark the line)
  538.  
  539.  
  540. Section 34.6: filtering portions of text: {!}
  541.  
  542. {!} is an operator like {c} and {d}.  That is, it consists of a repetition
  543. count, {!}, and a range specifier.  Once the {!} operator is entered in its
  544. entirety, a prompt will be given at the bottom of the screen for a UNIX
  545. command.  The text specified by the {!} operator is then deleted and
  546. passed/filtered/piped to the UNIX command you type.  The output of the UNIX
  547. command is then placed in your file.  For example, place the cursor at the
  548. beginning of the following line and {z^M}:
  549.  
  550. ls -l vi.tutorial
  551. ********* marks the bottom of the output from the ls command **********
  552.  
  553. Now type {!!csh^M}.  The line will be replaced with the output from the ls
  554. command.  The {u} command works on {!}, also.
  555.  
  556. Here is an extended exercise to display some of these capabilities.  When this
  557. tutorial was prepared, certain auxiliary programs were created to aid in its
  558. development.  Of major concern was the formatting of sections of the tutorial
  559. to fit on a single screen, particularly the first few sections.  What was
  560. needed was a vi command that would 'format' a paragraph; that is, fill out
  561. lines with as many words as would fit in eighty columns.  There is no such vi
  562. command.  Therefore, another method had to be found.
  563.  
  564. Of course, nroff was designed to do text formatting.  However, it produces a
  565. 'page'; meaning that there may be many blank lines at the end of a formatted
  566. paragraph from nroff.  The awk program was used to strip these blank lines from
  567. the output from nroff.  Below are the two files used for this purpose: I refer
  568. you to documentation on nroff and awk for a full explanation of their function.
  569. Position the cursor on the next line and {z^M}.
  570.  
  571. ******** contents of file f **********
  572. #
  573. nroff -i form.mac | awk "length != 0 { print }"
  574. ***** contents of file form.mac ******
  575. .na
  576. .nh
  577. .ll 79
  578. .ec 
  579. .c2 
  580. .cc 
  581. **************************************
  582.  
  583. Determine the line numbers of the two lines of file f.  They should be
  584. something like 574 and 575, although you better double check: this file is
  585. under constant revision and the line numbers may change inadvertently.  Then
  586. {:574,575w f^M}.  Do the same for the lines of file form.mac.  They will be
  587. approximately 577 and 582.  Then {:577,582w form.mac^M}.  File f must have
  588. execute privileges as a shell file: {:!chmod 744 f^M}.
  589.  
  590. Observe that this paragraph is
  591. rather ratty in appearance.  With our newly created files we can
  592. clean it up dramatically.  Position the cursor at the beginning
  593. of this paragraph and type the following sequence of
  594. characters 
  595. (note that we must abandon temporarily our convention
  596. of curly braces since the command itself contains a curly brace - we 
  597. will use square brackets for the nonce): [!}f^M].
  598.  
  599. Here is a brief explanation of what has happened.  By typing [!}f^M] we
  600. specified that the paragraph (all text between the cursor and the first blank
  601. line) will be removed from the edit file and piped to a UNIX program called
  602. "f".  This is a shell command file that we have created.  This shell file runs
  603. nroff, pipes its output to awk to remove blank lines, and the output from awk
  604. is then read back into our file in the place of the old, ratty paragraph.  The
  605. file form.mac is a list of commands to nroff to get it to produce paragraphs
  606. to our taste (the right margin is not justified, the line is 79 characters
  607. long, words are not hyphenated, and three nroff characters are renamed to
  608. avoid conflict: note that in this file, the {^G} you see there is vi's display
  609. of the control-G character, and not the two separate characters ^ up-arrow and
  610. G upper-case g).
  611.  
  612. This example was created before the existence of the fmt program.  I now type
  613. [!}fmt^M] to get the same effect much faster.  Actually, I don't type those
  614. six keys each time: I have an abbreviation (which see).
  615.  
  616. Section 35: searching with magic patterns
  617.  
  618. The documentation available for "magic patterns" (i.e. regular expressions) is
  619. very scanty.  The following should explain this possibly very confusing feature
  620. of the editor.  This section assumes that the magic option is on.  To make
  621. sure, you might want to type {:set magic^M}.
  622.  
  623. By "magic pattern" we mean a general description of a piece of text that the
  624. editor attempts to find during a search.  Most search patterns consist of
  625. strings of characters that must be matched exactly, e.g.  {/card/^M} searches
  626. for a specific string of four characters.  Let us suppose that you have
  627. discovered that you consistently have mistyped this simple word as either ccrd
  628. or czrd (this is not so far-fetched for touch typists).  You could {/ccrd/^M}
  629. and {n} until there are no more of this spelling, followed by {/czrd/^M} and
  630. {n} until there are no more of these.  Or you could {/c.rd/^M} and catch all of
  631. them on the first pass.  Try typing {/c.rd/^M} followed by several {n} and
  632. observe the effect.
  633.  
  634. Line 27: card cord curd ceard
  635.  
  636. When '.' is used in a search string, it has the effect of matching any single
  637. character.
  638.  
  639. The character '^' (up-arrow) used at the beginning of a search string means
  640. the beginning of the line.  {/^Line 27/^M} will find the example line above,
  641. while {/Line 27/^M} will find an occurrence of this string anywhere in the
  642. line.
  643.  
  644. Similarly, {/ the$/^M} will find all occurrences of the word 'the' occurring
  645. at the end of a line.  There are several of them in this file.
  646.  
  647. Note that {:set nomagic^M} will turn off the special meaning of these magic
  648. characters EXCEPT for '^' and '$' which retain their special meanings at the
  649. beginning and end of a search string.  Within the search string they hold no
  650. special meaning.  Try {/\/ the$\//^M} and note that the dollar-sign is not the
  651. last character in the search string.  Let the dollar-sign be the last
  652. character in the search string, as in {/\/ the$/^M} and observe the result.
  653.  
  654. Observe the result of {/back.*file/^M}.  This command, followed by sufficient
  655. {n}, will show you all lines in the file that contain both the words 'back'
  656. and 'file' on the same line.  The '*' magic character specifies that the
  657. previous regular expression (the '.' in our example) is to be repeatedly
  658. matched zero or more times.  In our example we specified that the words 'back'
  659. and 'file' must appear on the same line (they may be parts of words such as
  660. 'backwards' or 'workfile') separated by any number (including zero) of
  661. characters.
  662.  
  663. We could have specified that 'back' and 'file' are to be words by themselves by
  664. using the magic sequences '\<' or '\>'.  E.g.  {/\<back\>.*\<file\>/^M}.  The
  665. sequence '\<' specifies that this point of the search string must match the
  666. beginning of a word, while '\>' specifies a match at the end of a word.  By
  667. surrounding a string with these characters we have specified that they must be
  668. words.
  669.  
  670. To find all words that begin with an 'l' or a 'w', followed by an 'a' or an
  671. 'e', and ending in 'ing', try {/\<[lw][ea][a-z]*ing\>/^M}.  This will match
  672. words like 'learning', 'warning', and 'leading'.  The '[..]' notation matches
  673. exactly ONE character.  The character matched will be one of the characters
  674. enclosed in the square brackets.  The characters may be specified individually
  675. as in [abcd] or a '-' may be used to specify a range of characters as in [a-d].
  676. That is, [az] will match the letter 'a' OR the letter 'z', while [a-z] will
  677. match any of the lower case letters from 'a' through 'z'.  If you would like to
  678. match either an 'a', a '-', or a 'z', then the '-' must be escaped: [a\-z] will
  679. match ONE of the three characters 'a', '-', or 'z'.
  680.  
  681. If you wish to find all Capitalized words, try {/\<[A-Z][a-z]*\>/^M}.  The
  682. following will find all character sequences that do NOT begin with an
  683. uncapitalized letter by applying a special meaning to the '^' character in
  684. square brackets: {/\<[^a-z][a-z]*\>/^M}.  When '^' is the first character of a
  685. square-bracket expression, it specifies "all but these characters".  (No
  686. one claimed vi was consistent.)
  687.  
  688. To find all variable names (the first character is alphabetic, the remaining
  689. characters are alphanumeric):  try {/\<[A-Za-z][A-Za-z0-9]*\>/^M}.
  690.  
  691. In summary, here are the primitives for building regular expressions:
  692.  
  693.      ^      at beginning of pattern, matches beginning of line
  694.      $      at end of pattern, matches end of line
  695.      .      matches any single character
  696.      \<     matches the beginning of a word
  697.      \>     matches the end of a word
  698.      [str]  matches any single character in str
  699.      [^str] matches any single character NOT in str
  700.      [x-y]  matches any character in the ASCII range between x and y
  701.      *      matches any number (including zero) of the preceding pattern
  702.  
  703. Section 36: advanced substitution: {:s} 
  704.  
  705. The straightforward colon-substitute command looks like the substitute
  706. command of most line-oriented editors.  Indeed, vi is nothing more than a
  707. superstructure on the line-oriented editor ex and the colon commands are
  708. simply a way of accessing commands within ex (see section #EX).  This gives us
  709. a lot of global file processing not usually found in visual oriented editors.
  710.  
  711. The colon-substitute command looks like: {:s/ .. / .. /^M} and will find the
  712. pattern specified after the first slash (this is called the search pattern),
  713. and replace it with the pattern specified after the second slash (called,
  714. obviously enough, the replacement pattern).  E.g. position the cursor on line
  715. 28 below and {:s/esample/example/^M}:
  716.  
  717. Line 28: This is an esample.
  718.  
  719. The {u} and {U} commands work for {:s}.  The first pattern (the search pattern)
  720. may be a regular expression just as for the search command (after all, it IS a
  721. search, albeit limited to the current line).  Do an {u} on the above line, and
  722. try the following substitute, which will do almost the same thing: 
  723. {:s/s[^ ]/x/^M}.  
  724. Better undo it with {u}.  The first pattern {s[^ ]} matches an 's'
  725. NOT followed by a blank: the search therefore ignores the 's'es in 'This' and
  726. 'is'.  However, the character matched by {[^ ]} must appear in the replacement
  727. pattern.  But, in general, we do not know what that character is!  (In this
  728. particular example we obviously do, but more complicated examples will follow.)
  729. Therefore, vi (really ex) has a duplication mechanism to copy patterns matched
  730. in the search string into the replacement string.  Line 29 below is a copy of
  731. line 28 above so you can adjust your screen.
  732.  
  733. Line 29: This is an esample.
  734.  
  735. In general, you can nest parts of the search pattern in \( .. \) and refer to
  736. it in the replacement pattern as \n, where n is a digit.  The problem outlined
  737. in the previous paragraph is solved with {:s/s\([^ ]\)/x\1/^M}: try it.  Here
  738. \1 refers to the first pattern grouping \( .. \) in the search string.
  739.  
  740. Obviously, for a single line, this is rather tedious.  Where it becomes
  741. powerful, if not necessary, is in colon-substitutes that cover a range of
  742. lines.  (See the next section for a particularly comprehensive example.)
  743.  
  744. If the entire character sequence matched by the search pattern is needed in
  745. the replacement pattern, then the unescaped character '&' can be used.  On
  746. Line 29 above, try {:s/an e.ample/not &/^M}.  If another line is to have the
  747. word 'not' prepended to a pattern, then '~' can save you from re-typing the
  748. replacement pattern.  E.g. {:s/some pattern/~/^M} after the previous example
  749. would be equivalent to {:s/some pattern/not &/^M}.
  750.  
  751. One other useful replacement pattern allows you to change the case of
  752. individual letters.  The sequences {\u} and {\l} cause the immediately
  753. following character in the replacement to be converted to upper- or lower-case,
  754. respectively, if this character is a letter.  The sequences {\U} and {\L} turn
  755. such conversion on, either until {\E} or {\e} is encountered, or until the end
  756. of the replacement pattern.
  757.  
  758. For example, position the cursor on a line: pick a line, any line.  Type
  759. {:s/.*/\U&/^M} and observe the result.  You can undo it with {u}.
  760.  
  761. The search pattern may actually match more than once on a single line.
  762. However, only the first pattern is substituted.  If you would like ALL
  763. patterns matched on the line to be substituted, append a 'g' after the
  764. replacement pattern: {:s/123/456/g^M} will substitute EVERY occurrence
  765. on the line of 123 with 456.
  766.  
  767. Section 37: advanced line addressing: {:p} {:g} {:v}
  768.  
  769. Ex (available through the colon command in vi) offers several methods for
  770. specifying the lines on which a set of commands will act.  For example, if you
  771. would like to see lines 50 through 100 of your file: {:50,100p^M} will display
  772. them, wait for you to [Hit return to continue], and leave you on line 100.
  773. Obviously, it would be easier just to do {100G} from within vi.  But
  774. what if you would like to make changes to just those lines?  Then the
  775. addressing is important and powerful.
  776.  
  777. Line 30: This is a text.
  778. Line 31: Here is another text.
  779. Line 32: One more text line.
  780.  
  781. The lines above contain a typing error that the author of this tutorial tends
  782. to make every time he attempts to type the word 'test'.  To change all of these
  783. 'text's into 'test's, try the following:
  784. {:/^Line 30/,/^Line 32/s/text/test/^M}.  This finds the beginning and end of
  785. the portion of text to be changed, and limits the substitution to each of the
  786. lines in that range.  The {u} command applies to ALL of the substitutions as 
  787. a group.
  788.  
  789. This provides a mechanism for powerful text manipulations.
  790. And very complicated examples.
  791.  
  792. Line 33: This test is a.
  793. Line 34: Here test is another.
  794. Line 35: One line more test.
  795.  
  796. The above three lines have the second word out of order.  The following command
  797. string will put things right.  Be very careful when typing this: it is very
  798. long, full of special characters, and easy to mess up.  You may want to
  799. consider reading the following section to understand it before trying the
  800. experiment.  Don't worry about messing up the rest of the file, though: the
  801. address range is specified.
  802.  
  803. {:/^Line 33/,/^Line 35/s/\([^:]*\): \([^ ]*\) \([^ ]*\) \([^.]*\)/\1: \2 \4 \3/^M}
  804.  
  805. There are several things to note about this command string.  First of all, the
  806. range of the substitute was limited by the address specification {/^Line
  807. 33/,/^Line 35/^M}.  It might have been simpler to do {:set number^M} to see the
  808. line numbers directly, and then, in place of the two searches, typed
  809. the line numbers, e.g. {1396,1398}.  Or to mark the lines with {ma} and {mb}
  810. and use {'a,'b}.
  811.  
  812. Then follows the substitute pattern itself.  To make it easier to understand
  813. what the substitute is doing, the command is duplicated below with the various
  814. patterns named for easier reference:
  815.  
  816.      s/\([^:]*\): \([^ ]*\) \([^ ]*\) \([^.]*\)/\1: \2 \4 \3/
  817.        |--\1---|  |--\2---| |--\3---| |--\4---|
  818.       |--------search pattern------------------|-replacement|
  819.                                                |--pattern---|
  820.  
  821. In overview, the substitute looks for a particular pattern made up of 
  822. sub-patterns, which are named \1, \2, \3, and \4.  These patterns are specified
  823. by stating what they are NOT.  Pattern \1 is the sequence of characters that
  824. are NOT colons: in the search string, {[^:]} will match exactly one character
  825. that is not a colon, while appending the asterisk {[^:]*} specifies that the
  826. 'not a colon' pattern is to be repeated until no longer satisfied, and
  827. {\([^:]*\)} then gives the pattern its name, in this case \1.  Outside of the
  828. specification of \1 comes {: }, specifying that the next two characters must be
  829. a colon followed by a blank.
  830.  
  831. Patterns \2 and \3 are similar, specifying character sequences that are
  832. not blanks.  Pattern \4 matches up to the period at the end of the line.
  833.  
  834. The replacement pattern then consists of specifying the new order of the
  835. patterns.
  836.  
  837. This is a particularly complicated example, perhaps the most complicated
  838. in this tutorial/reference.  For our small examples, it is obviously
  839. tedious and error prone.  For large files, however, it may be the most
  840. efficient way to make the desired modifications.
  841.  
  842. (The reader is advised to look at the documentation for awk.  This tool is very
  843. powerful and slightly simpler to use than vi for this kind of file
  844. manipulation.  But, it is another command language to learn.)
  845.  
  846. Many times, you will not want to operate on every line in a certain
  847. range.  Rather you will want to make changes on lines that satisfy
  848. certain patterns; e.g. for every line that has the string 'NPS' on it,
  849. change 'NPS' to 'Naval Postgraduate School'.  The {:g} addressing
  850. command was designed for this purpose.  The example of this paragraph
  851. could be typed as {:g/NPS/s//Naval Postgraduate School/^M}.
  852.  
  853. The general format of the command is {:g/(pattern)/cmds^M} and it
  854. works in the following way: all lines that match the pattern
  855. following the {:g} are 'tagged' in a special way.  Then each of these
  856. lines have the commands following the pattern executed over them.
  857.  
  858. Line 36: ABC rhino george farmer Dick jester lest
  859. Line 37: george farmer rhino lest jester ABC
  860. Line 38: rhino lest george Dick farmer ABC jester 
  861.  
  862. Type:
  863.  
  864. {:g/^Line.*ABC/s/Dick/Harry Binswanger/|s/george farmer/gentleman george/p^M}
  865.  
  866. There are several things of note here.  First, lines 36, 37, and 38 above are
  867. tagged by the {:g}.  Type {:g/^Line.*ABC/p^M} to verify this.  Second, there
  868. are two substitutes on the same line separated by '|'.  In general, any colon
  869. commands can be strung together with '|'.  Third, both substitutes operate on
  870. all three lines, even though the first stubstitute works on only two of the
  871. lines (36 and 38).  Fourth, the second substitute works on only two lines (36
  872. and 37) and those are the two lines printed by the trailing 'p'.
  873.  
  874. The {:v} command works similarly to the {:g} command, except that the sense of
  875. the test for 'tagging' the lines is reversed: all lines NOT matching the search
  876. pattern are tagged and operated on by the commands.
  877.  
  878. Using {^V} to quote carriage return (see section 39) can be used in global
  879. substitutions to split two lines.  For example, the command 
  880. {:g/\.  /s//.^V^M/g^M} will change your file so that each sentence is on a 
  881. separate line.  (Note that we have to 'escape' the '.', because '.' by itself
  882. matches any character.  Our command says to find any line which contains a 
  883. period followed by 2 spaces, and inserts a carriage return after the period.)
  884.  
  885. Caveat:  In some of the documentation for ex and vi you may find the
  886. comment to the effect that {\^M} can be used between commands following
  887. {:g}.  The author of this tutorial has never gotten this to work and has
  888. crashed the editor trying.
  889.  
  890. Section 38: higher level text objects and nroff: {(} {)} [{] [}] {[[} {]]}
  891.  
  892. (Note: this section may be a little confusing because of our command
  893. notation.  Using curly braces to surround command strings works fine as
  894. long as the command string does not contain any curly braces itself.
  895. However, the curly braces are legitimate commands in vi.  Therefore, for
  896. any command sequence that contains curly braces, we will surround that
  897. sequence with SQUARE braces, as on the previous Section line.)
  898.  
  899. In working with a document, particularly if using the text formatting
  900. programs nroff or troff, it is often advantageous to work in terms of
  901. sentences, paragraphs, and sections.  The operations {(} and {)} move to
  902. the beginning of the previous and next sentences, respectively.  Thus
  903. the command {d)} will delete the rest of the current sentence; likewise
  904. {d(} will delete the previous sentence if you are at the beginning of
  905. the current sentence, or, if you are not at the beginning of a sentence,
  906. it will delete the current sentence from the beginning 
  907. up to where you are.
  908.  
  909. A sentence is defined to end at a '.', '!', or '?' which is followed
  910. by either the end of a line, or by two spaces.  Any number of closing
  911. ')', ']', '"', and ''' characters may appear after the '.', '!', or '?'
  912. before the spaces or end of line.  Therefore, the {(} and {)} commands
  913. would recognize only one sentence in the following line, but two
  914. sentences on the second following line.
  915.  
  916. Line 39: This is one sentence. Even though it looks like two.
  917. Line 40: This is two sentences.  Because it has two spaces after the '.'.
  918.  
  919. The operations [{] and [}] move over paragraphs and the operations {[[}
  920. and {]]} move over sections.
  921.  
  922. A paragraph begins after each empty line, and also at each of a set of nroff
  923. paragraph macros.  A section begins after each line with a form-feed ^L in the
  924. first column, and at each of a set of nroff section macros.  When preparing a
  925. text file as input to nroff, you will probably be using a set of nroff macros
  926. to make the formatting specifications easier, or more to your taste.  These
  927. macros are invoked by beginning a line with a period followed by the one or two
  928. letter macro name. Vi has been programmed to recognize these nroff macros, and
  929. if it doesn't recognize your particular macro you can use the {:set paragraphs}
  930. or {:set sections} commands so that it will.
  931.  
  932. Section 39: more about inserting text
  933.  
  934. There are a number of characters which you can use to make correnctions
  935. during input mode.  These are summarized in the following table.
  936.  
  937.     ^H      deletes the last input character
  938.     ^W      deletes the last input word
  939.     (erase) same as ^H; each terminal can define its own erase character; 
  940.             for some it is ^H, for others it is the DELETE key, and for
  941.             others it is '@'.
  942.     (kill)  deletes the input on this line; each terminal can define its
  943.             own line-kill character; for some it is ^U, for others it is
  944.             '@'; you will need to experiment on your terminal to find
  945.             out what your line-kill and erase characters are.
  946.     \       escapes a following ^H, (kill), and (erase) characters: i.e.
  947.             this is how to put these characters in your file.
  948.     ^[      escape key; ends insertion mode
  949.     ^?      the delete key; interrupts an insertion, terminating it
  950.             abnormally.
  951.     ^M      the return key; starts a new line.
  952.     ^D      backtabs over the indentation set by the autoindent option
  953.     0^D     backtabs over all indentation back to the beginning of the line
  954.     ^^D     (up-arrow followed by control-d)same as 0^D, except the indentation 
  955.         will be restored at the beginning of the next line.
  956.     ^V      quotes the next non-printing character into the file
  957.  
  958. If you wish to type in your erase or kill character (say # or @ or ^U) then you
  959. must precede it with a \, just as you would do at the normal system command
  960. level.  A more general way of typing non-printing characters into the file is
  961. to precede them with a ^V.  The ^V echoes as a ^ character on which the cursor
  962. rests.  This indicates that the editor expects you to type a control character
  963. and it will be inserted into the file at that point.  There are a few
  964. exceptions to note.  The implementation of the editor does not allow the null
  965. character ^@ to appear in files.  Also the linefeed character ^J is used by the
  966. editor to separate lines in the file, so it cannot appear in the middle of a
  967. line.  (Trying to insert a ^M into a file, or putting it in the replacement 
  968. part of a substitution string will result in the matched line being split in 
  969. two.  This, in effect, is how to split lines by using a substitution.)  You can 
  970. insert any other character, however, if you wait for the editor to echo the ^ 
  971. before you type the character.  In fact, the editor will treat a following 
  972. letter as a request for the corresponding control character.  This is the only 
  973. way to type ^S or ^Q, since the system normally uses them to suspend and resume 
  974. output and never gives them to the editor to process.
  975.  
  976. If you are using the autoindent option you can backtab over the indent which it
  977. supplies by typing a ^D.  This backs up to the boundary specified by the
  978. shiftwidth option.  This only works immediately after the supplied autoindent.
  979.  
  980. When you are using the autoindent option you may wish to place a label at the
  981. left margin of a line.  The way to do this easily is to type ^ (up-arrow) and
  982. then ^D.  The editor will move the cursor to the left margin for one line, and
  983. restore the previous indent on the next.  You can also type a 0 followed
  984. immediately by a ^D if you wish to kill all indentation and not have it resume
  985. on the next line.
  986.  
  987. Section 40: more on operators: {d} {c} {<} {>} {!} {=} {y}
  988.  
  989. Below is a non-exhaustive list of commands that can follow the operators
  990. to affect the range over which the operators will work.  However, note
  991. that the operators {<}, {>}, {!}, and {=} do not operate on any object
  992. less than a line.  Try {!w} and you will get a beep.  To get the
  993. operator to work on just the current line, double it.  E.g. {<<}.
  994.  
  995.     suffix        will operate on
  996.     ------        ------------------------
  997.       ^[            cancels the command
  998.       w             the word to the right of the cursor
  999.       W             ditto, but ignoring punctuation
  1000.       b             the word to the left of the cursor
  1001.       B             ditto, but ignoring punctuation
  1002.       e             see below.
  1003.       E               ditto
  1004.       (space)       a character
  1005.       $             to the end of the line
  1006.       ^             to the beginning of the line
  1007.       / .. /        up to, but not including, the string
  1008.       ? .. ?        back to and including the string
  1009.       fc            up to and including the occurrence of c 
  1010.       Fc            back to and including the occurrence of c
  1011.       tc            up to but not including the occurrence of c
  1012.       Tc            back to but not including the occurrence of c
  1013.       ^M            TWO lines (that's right: two)
  1014.       (number)^M    that many lines plus one
  1015.       (number)G     up to and including line (number)
  1016.       (             the previous sentence if you are at the beginning of
  1017.                     the current sentence, or the current sentence up to where 
  1018.                     you are if you are not at the beginning of the current 
  1019.                     sentence.  Here, 'sentence' refers to the intuitive
  1020.                     notion of an English sentence, ending with '!', '?',
  1021.                     or '.' and followed by an end of line or two spaces.
  1022.       )             the rest of the current sentence
  1023.       {             analogous to '(', but in reference to paragraphs:
  1024.                     sections of text surrounded by blank lines
  1025.       }             analogous to ')', but in reference to paragraphs
  1026.       [[            analogous to '(', but in reference to sections
  1027.       ]]            analogous to ')', but in reference to sections
  1028.       H             the first line on the screen
  1029.       M             the middle line on the screen
  1030.       L             the last line on the screen
  1031.       3L            through the third line from the bottom of the screen
  1032.       ^F            forward a screenful
  1033.       ^B            backward a screenful
  1034.       :
  1035.       :  etc. etc. etc.
  1036.  
  1037. This list is not exhaustive, but it should be sufficient to get the idea
  1038. across: after the operator, you can specify a range with a move-the-cursor
  1039. command, and that is the region of text over which the operator will be
  1040. effective.
  1041.  
  1042. Section 41: abbreviations: {:ab}
  1043.  
  1044. When typing large documents you may find yourself typing a large phrase
  1045. over and over.  Vi gives you the ability to specify an abbreviation for
  1046. a long string such that typing the abbreviation will automatically
  1047. expand into the longer phrase.
  1048.  
  1049. Type {:ab nps Naval Postgraduate School^M}.  Now type:
  1050.  
  1051. {iThis is to show off the nps's UNIX editor.^M^[}
  1052.  
  1053. Section 42: vi's relationship with the ex editor: {:}
  1054.  
  1055. Vi is actually one mode of editing within the editor ex.  When you are
  1056. running vi you can escape to the line oriented editor of ex by giving
  1057. the command {Q}.  All of the colon-commands which were introduced above
  1058. are available in ex.  Likewise, most ex commands can be invoked from vi
  1059. using {:}.   
  1060.  
  1061. In rare instances, an internal error may occur in vi.  In this case you
  1062. will get a diagnostic and will be left in the command mode of ex.  You can
  1063. then save your work and quit if you wish by giving the command {x} after
  1064. the colon prompt of ex.  Or you can reenter vi (if you are brave) by
  1065. giving ex the command {vi}.
  1066.  
  1067. Section 43: vi on hardcopy terminals and dumb terminals: open mode
  1068.  
  1069. (The author has not checked the following documentation for accuracy.  It is
  1070. abstracted from the Introduction to Vi Editing document.)
  1071.  
  1072. If you are on a hardcopy terminal or a terminal which does not have a cursor
  1073. which can move off the bottom line, you can still use the command set of vi,
  1074. but in a different mode.  When you give the vi command to UNIX, the editor will
  1075. tell you that it is using open mode.  This name comes from the open command in
  1076. ex, which is used to get into the same mode.
  1077.  
  1078. The only difference between visual mode (normal vi) and open mode is the way in
  1079. which the text is displayed.
  1080.  
  1081. In open mode the editor uses a single line window into the file, and moving
  1082. backward and forward in the file causes new lines to be displayed, always below
  1083. the current line.  Two commands of vi work differently in open: {z} and {^R}.
  1084. The {z} command does not take parameters, but rather draws a window of context
  1085. around the current line and then returns you to the current line.
  1086.  
  1087. If you are on a hardcopy terminal, the {^R} command will retype the current
  1088. line.  On such terminals, the editor normally uses two lines to represent the
  1089. current line.  The first line is a copy of the line as you started to edit it,
  1090. and you work on the line below this line.  When you delete characters, the
  1091. editor types a number of \'s to show you the characters which are deleted.  The
  1092. editor also reprints the current line soon after such changes so that you can
  1093. see what the line looks like again.
  1094.  
  1095. It is sometimes useful to use this mode on very slow terminals which can
  1096. support vi in the full screen mode.  You can do this by entering ex and using
  1097. an {open} command.
  1098.  
  1099. *********************************************************************
  1100. Section 44: options: {:set} {setenv EXINIT}
  1101.  
  1102. You will discover options as you need them.  Do not worry about them very much
  1103. on the first pass through this document.  My advice is to glance through them,
  1104. noting the ones that look interesting, ignoring the ones you don't understand,
  1105. and try re-scanning them in a couple of weeks.
  1106.  
  1107. If you decide that you have a favorite set of options and would like to change
  1108. the default values for the editor, place a {setenv EXINIT} command in your
  1109. .login file.  When you are given an account under UNIX your directory has
  1110. placed in it a file that is executed each time you log in.  If one of the
  1111. commands in this file sets the environment variable EXINIT to a string of vi
  1112. commands, you can have many things done for you each time you invoke vi.  For
  1113. example, if you decide that you don't like tabstops placed every eight columns
  1114. but prefer every four columns, and that you wish the editor to insert linefeeds
  1115. for you when your typing gets you close to column 72, and you want
  1116. autoindentation, then include the following line in your .login file:
  1117.  
  1118. setenv EXINIT='set tabstop=4 wrapmargin=8 autoindent'
  1119.  
  1120. or equivalently
  1121.  
  1122. setenv EXINIT='se ts=4 wm=8 ai'
  1123.  
  1124. Each time you bring up vi, this command will be executed and the options set.
  1125.  
  1126. There are forty options in the vi/ex editor that the user can set for his/her
  1127. own convenience.  They are described in more detail in individual sections
  1128. below.  The section line will show the full spelling of the option name, the
  1129. abbreviation, and the default value of the option.  The text itself
  1130. comes from the ex reference manual and is not the epitome of clarity.
  1131.  
  1132. Section 44.1: {autoindent}, {ai} default: noai
  1133.  
  1134. Can be used to ease the preparation of structured program text.  At the
  1135. beginning of each append, change or insert command or when a new line is opened
  1136. or created by an append, change, insert, or substitute operation within open or
  1137. visual mode, ex looks at the line being appended after, the first line changed
  1138. or the line inserted before and calculates the amount of white space at the
  1139. start of the line.  It then aligns the cursor at the level of indentation so
  1140. determined.
  1141.  
  1142. If the user then types lines of text in, they will continue to be justified at
  1143. the displayed indenting level.  If more white space is typed at the beginning
  1144. of a line, the following line will start aligned with the first non-white
  1145. character of the previous line.  To back the cursor up to the preceding tab
  1146. stop one can hit {^D}.  The tab stops going backwards are defined at multiples
  1147. of the shiftwidth option.  You cannot backspace over the indent, except by
  1148. sending an end-of-file with a {^D}.  A line with no characters added to it
  1149. turns into a completely blank line (the white space provided for the autoindent
  1150. is discarded). Also specially processed in this mode are lines beginning with
  1151. an up-arrow `^' and immediately followed by a {^D}.  This causes the input to
  1152. be repositioned at the beginning of the line, but retaining the previous indent
  1153. for the next line.  Similarly, a `0' followed by a {^D} repositions at the
  1154. beginning but without retaining the previous indent.  Autoindent doesn't happen
  1155. in global commands or when the input is not a terminal.
  1156.  
  1157. Section 44.2: {autoprint}, {ap} default: ap
  1158.  
  1159. Causes the current line to be printed after each delete, copy, join, move,
  1160. substitute, t, undo or shift command.  This has the same effect as supplying a
  1161. trailing `p' to each such command.  Autoprint is suppressed in globals, and
  1162. only applies to the last of many commands on a line.
  1163.  
  1164. Section 44.3: {autowrite}, {aw} default: noaw
  1165.  
  1166. Causes the contents of the buffer to be written to the current file if you have
  1167. modified it and give a next, rewind, stop, tag, or {!} command, or a control-
  1168. up-arrow {^^} (switch files) or {^]} (tag goto) command in visual.  Note, that
  1169. the edit and ex commands do not autowrite.  In each case, there is an
  1170. equivalent way of switching when autowrite is set to avoid the autowrite
  1171. ({edit} for next, rewind!  for rewind, stop!  for stop, tag!  for tag, shell
  1172. for {!}, and {:e #} and a {:ta!} command from within visual).
  1173.  
  1174. Section 44.4: {beautify}, {bf} default: nobeautify
  1175.  
  1176. Causes all control characters except tab ^I, newline ^M and form-feed ^L to be
  1177. discarded from the input.  A complaint is registered the first time a backspace
  1178. character is discarded.  Beautify does not apply to command input.
  1179.  
  1180. Section 44.5: {directory}, {dir} default: dir=/tmp 
  1181.  
  1182. Specifies the directory in which ex places its buffer file.  If this directory
  1183. in not writable, then the editor will exit abruptly when it fails to be able to
  1184. create its buffer there.
  1185.  
  1186. Section 44.6: {edcompatible} default: noedcompatible
  1187.  
  1188. Causes the presence or absence of g and c suffixes on substitute commands to be
  1189. remembered, and to be toggled by repeating the suffices.  The suffix r makes
  1190. the substitution be as in the {~} command, instead of like {&}.
  1191.  
  1192. [Author's note: this should not concern users of vi.]
  1193.  
  1194. Section 44.7: {errorbells}, {eb} default: noeb
  1195.  
  1196. Error messages are preceded by a bell.  However, bell ringing in open and
  1197. visual modes on errors is not suppressed by setting noeb.  If possible the
  1198. editor always places the error message in a standout mode of the terminal (such
  1199. as inverse video) instead of ringing the bell.
  1200.  
  1201. Section 44.8: {hardtabs}, {ht} default: ht=8
  1202.  
  1203. Gives the boundaries on which terminal hardware tabs are set (or on which the
  1204. system expands tabs).
  1205.  
  1206. Section 44.9: {ignorecase}, {ic} default: noic
  1207.  
  1208. All upper case characters in the text are mapped to lower case in regular
  1209. expression matching.  In addition, all upper case characters in regular
  1210. expressions are mapped to lower case except in character class specifications
  1211. (that is, character in square brackets).
  1212.  
  1213. Section 44.10: {lisp} default: nolisp
  1214.  
  1215. Autoindent indents appropriately for lisp code, and the {(}, {)}, [{], [}],
  1216. {[[}, and {]]} commands in open and visual modes are modified in a
  1217. striaghtforward, intuitive fashion to have meaning for lisp.
  1218.  
  1219. [Author's note: but don't ask me to define them precisely.]
  1220.  
  1221. Section 44.11: {list} default: nolist
  1222.  
  1223. All printed lines will be displayed (more) unambiguously, showing tabs as ^I
  1224. and end-of-lines with `$'.  This is the same as in the ex command {list}.
  1225.  
  1226. Section 44.12: {magic} default: magic for {ex} and {vi}, nomagic for edit.
  1227.  
  1228. If nomagic is set, the number of regular expression metacharacters is greatly
  1229. reduced, with only up-arrow `^' and `$' having special effects.  In addition
  1230. the metacharacters `~' and `&' of the replacement pattern are treated as normal
  1231. characters.  All the normal metacharacters may be made magic when nomagic is
  1232. set by preceding them with a `\'.
  1233.  
  1234. [Author's note: In other words, if magic is set a back-slant turns the magic
  1235. off for the following character, and if nomagic is set a back-slant turns the
  1236. magic ON for the following character.  And, no, we are not playing Dungeons and
  1237. Dragons, although I think the writers of these option notes must have played it
  1238. all the time.]
  1239.  
  1240. Section 44.13: {mesg} default: mesg
  1241.  
  1242. Causes write permission to be turned off to the terminal while you are in
  1243. visual mode, if nomesg is set.
  1244.  
  1245. [Author's note: I don't know if anyone could have made any one sentence
  1246. paragraph more confusing than this one.  What it says is: mesg allows people to
  1247. write to you even if you are in visual or open mode; nomesg locks your terminal
  1248. so they can't write to you and mess up your screen.]
  1249.  
  1250. Section 44.14: {number, nu} default: nonumber
  1251.  
  1252. Causes all output lines to be printed with their line numbers.  In addition
  1253. each input line will be prompted with its line number.
  1254.  
  1255. Section 44.15: {open} default: open
  1256.  
  1257. If {noopen}, the commands open and visual are not permitted.  This is set for
  1258. edit to prevent confusion resulting from accidental entry to open or visual
  1259. mode.
  1260.  
  1261. [Author's note: As you may have guessed by now, there are actually three
  1262. editors available under Berkeley UNIX that are in reality the same
  1263. program, ex, with different options set: ex itself, vi, and edit.]
  1264.  
  1265. Section 44.16: {optimize, opt} default: optimize
  1266.  
  1267. Throughput of text is expedited by setting the terminal to not do automatic
  1268. carriage returns when printing more than one (logical) line of output, greatly
  1269. speeding output on terminals without addressable cursors when text with leading
  1270. white space is printed.
  1271.  
  1272. [Author's note: I still don't know what this option does.]
  1273.  
  1274. Section 44.17: {paragraphs, para} default: para=IPLPPPQPP LIbp
  1275.  
  1276. Specifies the paragraphs for the [{] and [}] operations in open and visual.
  1277. The pairs of characters in the option's value are the names of the nroff macros
  1278. which start paragraphs.
  1279.  
  1280. Section 44.18: {prompt} default: prompt
  1281.  
  1282. Command mode input is prompted for with a `:'.
  1283.  
  1284. [Author's note: Doesn't seem to have any effect on vi.]
  1285.  
  1286. Section 44.19: {readonly}, {ro} default: noro, unless invoked with -R 
  1287.                                          or insufficient privileges on file
  1288.  
  1289. This option allows you to guarantee that you won't clobber your file by
  1290. accident.  You can set the option and writes will fail unless you use an `!'
  1291. after the write.  Commands such as {x}, {ZZ}, the autowrite option, and in
  1292. general anything that writes is affected.  This option is turned on if you
  1293. invoke the editor with the -R flag.
  1294.  
  1295. Section 44.20: {redraw} default: noredraw
  1296.  
  1297. The editor simulates (using great amounts of output), an intelligent terminal
  1298. on a dumb terminal (e.g. during insertions in visual the characters to the
  1299. right of the cursor position are refreshed as each input character is typed).
  1300. Useful only at very high baud rates, and should be used only if the system is
  1301. not heavily loaded: you will notice the performance degradation yourself.
  1302.  
  1303. Section 44.21: {remap} default: remap
  1304.  
  1305. If on, macros are repeatedly tried until they are unchanged.  For example, if o
  1306. is mapped to O, and O is mapped to I, then if remap is set, o will map to I,
  1307. but if noremap is set, it will map to O .
  1308.  
  1309. Section 44.22: {report} default: report=5 for ex and vi, 2 for edit
  1310.  
  1311. Specifies a threshold for feedback from commands.  Any command which modifies
  1312. more than the specified number of lines will provide feedback as to the scope
  1313. of its changes.  For commands such as global, open, undo, and visual which have
  1314. potentially more far reaching scope, the net change in the number of lines in
  1315. the buffer is presented at the end of the command, subject to this same
  1316. threshold.  Thus notification is suppressed during a global command on the
  1317. individual commands performed.
  1318.  
  1319. Section 44.23: {scroll} default: scroll=1/2 window
  1320.  
  1321. Determines the number of logical lines scrolled when a {^D} is received from a
  1322. terminal in command mode, and determines the number of lines printed by a
  1323. command mode z command (double the value of scroll).
  1324.  
  1325. [Author's note: Doesn't seem to affect {^D} and {z} in visual (vi) mode.]
  1326.  
  1327. Section 44.24: sections {sections} default: sections=SHNHH HU 
  1328.  
  1329. Specifies the section macros from nroff for the {[[} and {]]} operations in
  1330. open and visual.  The pairs of characters in the options's value are the names
  1331. of the macros which start paragraphs.
  1332.  
  1333. Section 44.25: {shell}, {sh} default: sh=/bin/sh 
  1334.  
  1335. Gives the path name of the shell forked for the shell escape command `!', and
  1336. by the shell command.  The default is taken from SHELL in the environment, if
  1337. present.
  1338.  
  1339. [Editor's note: I would suggest that you place the following line in
  1340. your .login file:
  1341. setenv SHELL '/bin/csh'
  1342. ]
  1343.  
  1344. Section 44.26: {shiftwidth}, {sw} default: sw=8 
  1345.  
  1346. Used in reverse tabbing with {^D} when using autoindent to append text, and
  1347. used by the shift commands.  Should probably be the same value as the tabstop
  1348. option.
  1349.  
  1350. Section 44.27: {showmatch}, {sm} default: nosm 
  1351.  
  1352. In open and visual mode, when a `)' or `}' is typed, if the matching `(' or `{'
  1353. is on the screen, move the cursor to it for one second.  Extremely useful with
  1354. complicated nested expressions, or with lisp.
  1355.  
  1356. Section 44.28: {slowopen}, {slow} default: terminal dependent
  1357.  
  1358. Affects the display algorithm used in visual mode, holding off display updating
  1359. during input of new text to improve throughput when the terminal in use is both
  1360. slow and unintelligent.  See "An Introduction to Display Editing with Vi" for
  1361. more details.
  1362.  
  1363. Section 44.29: {tabstop}, {ts} default: ts=8
  1364.  
  1365. The editor expands tabs ^I to tabstop boundaries in the display.
  1366.  
  1367. Section 44.30: {taglength}, {tl} default: tl=0
  1368.  
  1369. Tags are not significant beyond this many characters.
  1370. A value of zero (the default) means that all characters are significant.
  1371.  
  1372. Section 44.31: {tags} default: tags=tags /usr/lib/tags
  1373.  
  1374. A path of files to be used as tag files for the tag command.  A requested tag
  1375. is searched for in the specified files, sequentially.  By default files called
  1376. tags are searched for in the current directory and in /usr/lib (a master file
  1377. for the entire system).
  1378.  
  1379. [Author's note: The author of this tutorial has never used this option, nor
  1380. seen it used.  I'm not even sure I know what they are talking about.]
  1381.  
  1382. Section 44.32: {term} default: from environment variable TERM
  1383.  
  1384. The terminal type of the output device.
  1385.  
  1386. Section 44.33: {terse} default: noterse
  1387.  
  1388. Shorter error diagnostics are produced for the experienced user.
  1389.  
  1390. Section 44.34: {timeout} default: timeout
  1391.  
  1392. Causes macros to time out after one second.  Turn it off and they will
  1393. wait forever.  This is useful if you want multi-character macros, but if
  1394. your terminal sends escape sequences for arrow keys, it will be
  1395. necessary to hit escape twice to get a beep.
  1396.  
  1397. [Editor's note: Another paragraph which requires a cryptographer.]
  1398.  
  1399. Section 44.35: ttytype
  1400.  
  1401. [Editor's note: I have found no documentation for this option at all.]
  1402.  
  1403. Section 44.36: {warn} default: warn
  1404.  
  1405. Warn if there has been `[No write since last change]' before a `!' command
  1406. escape.
  1407.  
  1408. Section 44.37: {window} default: window=speed dependent
  1409.  
  1410. The number of lines in a text window in the visual command.  The default is 8
  1411. at slow speeds (600 baud or less), 16 at medium speed (1200 baud), and the full
  1412. screen (minus one line) at higher speeds.
  1413.  
  1414. Section 44.38: {wrapscan}, {ws} default: ws
  1415.  
  1416. Searches using the regular expressions in addressing will wrap around past the
  1417. end of the file.
  1418.  
  1419. Section 44.39: {wrapmargin}, {wm} default: wm=0
  1420.  
  1421. Defines a margin for automatic wrapover of text during input in open and visual
  1422. modes.  The numeric value is the number of columns from the right edge of the
  1423. screen around which vi looks for a convenient place to insert a new-line
  1424. character (wm=0 is OFF).  This is very convenient for touch typists.
  1425. Wrapmargin behaves much like fill/nojustify mode does in nroff.
  1426.  
  1427. Section 44.40: {writeany}, {wa} default: nowa
  1428.  
  1429. Inhibit the checks normally made before write commands, allowing a write to any
  1430. file which the system protection mechanism will allow.
  1431.  
  1432. Section 44.41: {w300}, {w1200}, {w9600} defaults: w300=8
  1433.                                                  w1200=16
  1434.                                                  w9600=full screen minus one
  1435.  
  1436. These are not true options but set the default size of the window for when the
  1437. speed is slow (300), medium (1200), or high (9600), respectively.  They are
  1438. suitable for an EXINIT and make it easy to change the 8/16/full screen rule.
  1439.  
  1440. Section 45: Limitations
  1441.  
  1442. Here are some editor limits that the user is likely to encounter:
  1443.        1024   characters per line
  1444.        256    characters per global command list
  1445.        128    characters per file name
  1446.        128    characters in the previous inserted and deleted text in open or 
  1447.               visual
  1448.        100    characters in a shell escape command
  1449.        63     characters in a string valued option
  1450.        30     characters in a tag name
  1451.        250000 lines in the file (this is silently enforced).
  1452.  
  1453. The visual implementation limits the number of macros defined with map to 32,
  1454. and the total number of characters in macros to be less than 512.
  1455.  
  1456. [Editor's note: these limits may not apply to versions after 4.1BSD.]
  1457.