home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / riscbsd / 1_1_contri / usd / 15_vi / vi_in < prev    next >
Encoding:
Text File  |  1986-05-30  |  75.9 KB  |  2,038 lines

  1. .\" Copyright (c) 1980 Regents of the University of California.
  2. .\" All rights reserved.  The Berkeley software License Agreement
  3. .\" specifies the terms and conditions for redistribution.
  4. .\"
  5. .\"    @(#)vi.in    6.1 (Berkeley) 5/30/86
  6. .\"
  7. .EH 'USD:15-%''An Introduction to Display Editing with Vi'
  8. .OH 'An Introduction to Display Editing with Vi''USD:15-%'
  9. .bd S 3
  10. .if t .ds dg \(dg
  11. .if n .ds dg +
  12. .if t .ds dd \(dd
  13. .if n .ds dd ++
  14. .\".RP
  15. .TL
  16. An Introduction to Display Editing with Vi
  17. .AU
  18. William Joy
  19. .AU
  20. Mark Horton
  21. .AI
  22. Computer Science Division
  23. Department of Electrical Engineering and Computer Science
  24. University of California, Berkeley
  25. Berkeley, Ca.  94720
  26. .AB
  27. .PP
  28. .I Vi
  29. (visual) is a display oriented interactive text editor.
  30. When using
  31. .I vi
  32. the screen of your terminal acts as a window into the file which you
  33. are editing.  Changes which you make to the file are reflected
  34. in what you see.
  35. .PP
  36. Using
  37. .I vi
  38. you can insert new text any place in the file quite easily.
  39. Most of the commands to
  40. .I vi
  41. move the cursor around in the file.
  42. There are commands to move the cursor
  43. forward and backward in units of characters, words,
  44. sentences and paragraphs.
  45. A small set of operators, like
  46. .B d
  47. for delete and
  48. .B c
  49. for change, are combined with the motion commands to form operations
  50. such as delete word or change paragraph, in a simple and natural way.
  51. This regularity and the mnemonic assignment of commands to keys makes the
  52. editor command set easy to remember and to use.
  53. .PP
  54. .I Vi
  55. will work on a large number of display terminals,
  56. and new terminals are easily driven after editing a terminal description file.
  57. While it is advantageous to have an intelligent terminal which can locally
  58. insert and delete lines and characters from the display, the editor will
  59. function quite well on dumb terminals over slow phone lines.
  60. The editor makes allowance for the low bandwidth in these situations
  61. and uses smaller window sizes and
  62. different display updating algorithms to make best use of the
  63. limited speed available.
  64. .PP
  65. It is also possible to use the command set of
  66. .I vi
  67. on hardcopy terminals, storage tubes and ``glass tty's'' using a one line
  68. editing window; thus
  69. .I vi's
  70. command set is available on all terminals.
  71. The full command set of the more traditional, line
  72. oriented editor
  73. .I ex
  74. is available within
  75. .I vi;
  76. it is quite simple to switch between the two modes of editing.
  77. .AE
  78. .NH 1
  79. Getting started
  80. .PP
  81. .FS
  82. The financial support of an \s-2IBM\s0 Graduate Fellowship and the
  83. National Science Foundation under grants MCS74-07644-A03 and MCS78-07291
  84. is gratefully acknowledged.
  85. .FE
  86. This document provides a quick introduction to
  87. .I vi.
  88. (Pronounced \fIvee-eye\fP.)
  89. You should be running
  90. .I vi
  91. on a file you are familiar with while you are reading this.
  92. The first part of this document (sections 1 through 5)
  93. describes the basics of using
  94. .I vi.
  95. Some topics of special interest are presented in section 6, and 
  96. some nitty-gritty details of how the editor functions are saved for section
  97. 7 to avoid cluttering the presentation here.
  98. .PP
  99. There is also a short appendix here, which gives for each character the
  100. special meanings which this character has in \fIvi\fR.  Attached to
  101. this document should be a quick reference card.
  102. This card summarizes the commands of
  103. .I vi
  104. in a very compact format.  You should have the card handy while you are
  105. learning
  106. .I vi.
  107. .NH 2
  108. Specifying terminal type
  109. .PP
  110. Before you can start
  111. .I vi
  112. you must tell the system what kind of terminal you are using.
  113. Here is a (necessarily incomplete) list of terminal type codes.
  114. If your terminal does not appear here, you should consult with one of
  115. the staff members on your system to find out the code for your terminal.
  116. If your terminal does not have a code, one can be assigned and a description
  117. for the terminal can be created.
  118. .LP
  119. .TS
  120. center;
  121. ab ab ab
  122. a a a.
  123. Code    Full name    Type
  124. _
  125. 2621    Hewlett-Packard 2621A/P    Intelligent
  126. 2645    Hewlett-Packard 264x    Intelligent
  127. act4    Microterm ACT-IV    Dumb
  128. act5    Microterm ACT-V    Dumb
  129. adm3a    Lear Siegler ADM-3a    Dumb
  130. adm31    Lear Siegler ADM-31    Intelligent
  131. c100    Human Design Concept 100    Intelligent
  132. dm1520    Datamedia 1520    Dumb
  133. dm2500    Datamedia 2500    Intelligent
  134. dm3025    Datamedia 3025    Intelligent
  135. fox    Perkin-Elmer Fox    Dumb
  136. h1500    Hazeltine 1500    Intelligent
  137. h19    Heathkit h19    Intelligent
  138. i100    Infoton 100    Intelligent
  139. mime    Imitating a smart act4    Intelligent
  140. t1061    Teleray 1061    Intelligent
  141. vt52    Dec VT-52    Dumb
  142. .TE
  143. .PP
  144. Suppose for example that you have a Hewlett-Packard HP2621A
  145. terminal.  The code used by the system for this terminal is `2621'.
  146. In this case you can use one of the following commands to tell the system
  147. the type of your terminal:
  148. .DS
  149. % \fBsetenv TERM\fP 2621
  150. .DE
  151. This command works with the
  152. .I csh
  153. shell.
  154. If you are using the standard Bourne shell
  155. .I sh
  156. then you should give the commands
  157. .DS
  158. $ \fBTERM=\fP2621
  159. $ \fBexport TERM\fP
  160. .DE
  161. .PP
  162. If you want to arrange to have your terminal type set up automatically
  163. when you log in, you can use the
  164. .I tset
  165. program.
  166. If you dial in on a
  167. .I mime ,
  168. but often use hardwired ports, a typical line for your
  169. .I .login
  170. file (if you use csh) would be
  171. .DS
  172. \fBsetenv TERM \(gatset\fP \- \-d mime\(ga
  173. .DE
  174. or for your
  175. .I .profile
  176. file (if you use sh)
  177. .DS
  178. \fBTERM=\(gatse\fPt \- \-d mime\(ga
  179. .DE
  180. .I Tset
  181. knows which terminals are hardwired to each port
  182. and needs only to be told that when you dial in you
  183. are probably on a
  184. .I mime .
  185. .I Tset
  186. is usually used to change the erase and kill characters, too.
  187. .NH 2
  188. Editing a file
  189. .PP
  190. After telling the system which kind of terminal you have, you should
  191. make a copy of a file you are familiar with, and run
  192. .I vi
  193. on this file, giving the command
  194. .DS
  195. % \fBvi\fR \fIname\fR
  196. .DE
  197. replacing \fIname\fR with the name of the copy file you just created.
  198. The screen should clear and the text of your file should appear on the
  199. screen.  If something else happens refer to the footnote.\*(dd
  200. .FS
  201. \*(dd If you gave the system an incorrect terminal type code then the
  202. editor may have just made a mess out of your screen.  This happens when
  203. it sends control codes for one kind of terminal to some other
  204. kind of terminal.  In this case hit
  205. the keys \fB:q\fR (colon and the q key) and then hit the \s-2RETURN\s0 key.
  206. This should get you back to the command level interpreter.
  207. Figure out what you did wrong (ask someone else if necessary) and try again.
  208.      Another thing which can go wrong is that you typed the wrong file name and
  209. the editor just printed an error diagnostic.  In this case you should
  210. follow the above procedure for getting out of the editor, and try again
  211. this time spelling the file name correctly.
  212.      If the editor doesn't seem to respond to the commands which you type
  213. here, try sending an interrupt to it by hitting the \s-2DEL\s0 or \s-2RUB\s0
  214. key on your terminal, and then hitting the \fB:q\fR command again followed
  215. by a carriage return.
  216. .sp
  217. .FE
  218. .NH 2
  219. The editor's copy: the buffer
  220. .PP
  221. The editor does not directly modify the file which you are editing. 
  222. Rather, the editor makes a copy of this file, in a place called the
  223. .I buffer,
  224. and remembers the file's
  225. name.  You do not affect the contents of the file unless and until you
  226. write the changes you make back into the original file.
  227. .NH 2
  228. Notational conventions
  229. .PP
  230. In our examples, input which must be typed as is will be presented in
  231. \fBbold face\fR. Text which should be replaced with appropriate input
  232. will be given in \fIitalics\fR.  We will represent special characters
  233. in \s-2SMALL CAPITALS\s0.
  234. .NH 2
  235. Arrow keys
  236. .PP
  237. The editor command set is independent of the terminal
  238. you are using.  On most terminals with cursor positioning keys, these keys
  239. will also work within the editor.
  240. If you don't have cursor positioning keys, or even if you do, you can use
  241. the \fBh j k\fR and \fBl\fR keys as cursor positioning
  242. keys (these are labelled with arrows on an
  243. .I adm3a).*
  244. .PP
  245. (Particular note for the HP2621: on this terminal the function keys
  246. must be \fIshifted\fR (ick) to send to the machine, otherwise they
  247. only act locally.  Unshifted use will leave the cursor positioned
  248. incorrectly.)
  249. .FS
  250. * As we will see later,
  251. .I h
  252. moves back to the left (like control-h which is a backspace),
  253. .I j
  254. moves down (in the same column),
  255. .I k
  256. moves up (in the same column),
  257. and
  258. .I l
  259. moves to the right.
  260. .FE
  261. .NH 2
  262. Special characters: \s-2ESC\s0, \s-2CR\s0 and \s-2DEL\s0
  263. .PP
  264. Several of these special characters are very important, so be sure to
  265. find them right now.  Look on your keyboard for a key labelled \s-2ESC\s0
  266. or \s-2ALT\s0.  It should be near the upper left corner of your terminal.
  267. Try hitting this key a few times.  The editor will ring the bell
  268. to indicate that it is in a quiescent state.\*(dd
  269. .FS
  270. \*(dd On smart terminals where it is possible, the editor will quietly
  271. flash the screen rather than ringing the bell.
  272. .FE
  273. Partially formed commands are cancelled by \s-2ESC\s0, and when you insert
  274. text in the file you end the text insertion
  275. with \s-2ESC\s0.  This key is a fairly
  276. harmless one to hit, so you can just hit it if you don't know
  277. what is going on until the editor rings the bell.
  278. .PP
  279. The \s-2CR\s0 or \s-2RETURN\s0 key is important because it is used
  280. to terminate certain commands.
  281. It is usually at the right side of the keyboard,
  282. and is the same command used at the end of each shell command.
  283. .PP
  284. Another very useful key is the \s-2DEL\s0 or \s-2RUB\s0 key, which generates
  285. an interrupt, telling the editor to stop what it is doing.
  286. It is a forceful way of making the editor listen
  287. to you, or to return it to the quiescent state if you don't know or don't
  288. like what is going on.  Try hitting the `/' key on your terminal.  This
  289. key is used when you want to specify a string to be searched for.  The
  290. cursor should now be positioned at the bottom line of the terminal after
  291. a `/' printed as a prompt.  You can get the cursor back to the current
  292. position by hitting the \s-2DEL\s0 or \s-2RUB\s0 key; try this now.*
  293. .FS
  294. * Backspacing over the `/' will also cancel the search.
  295. .FE
  296. From now on we will simply refer to hitting the \s-2DEL\s0 or \s-2RUB\s0
  297. key as ``sending an interrupt.''**
  298. .FS
  299. ** On some systems, this interruptibility comes at a price: you cannot type
  300. ahead when the editor is computing with the cursor on the bottom line.
  301. .FE
  302. .PP
  303. The editor often echoes your commands on the last line of the terminal.
  304. If the cursor is on the first position of this last line, then the editor
  305. is performing a computation, such as computing a new position in the
  306. file after a search or running a command to reformat part of the buffer.
  307. When this is happening you can stop the editor by
  308. sending an interrupt.
  309. .NH 2
  310. Getting out of the editor
  311. .PP
  312. After you have worked with this introduction for a while, and you wish
  313. to do something else, you can give the command \fBZZ\fP
  314. to the editor.
  315. This will write the contents of the editor's buffer back into
  316. the file you are editing, if you made any changes, and then quit from
  317. the editor.  You can also end an editor
  318. session by giving the command \fB:q!\fR\s-2CR\s0;\*(dg
  319. .FS
  320. \*(dg All commands which read from the last display line can also be
  321. terminated with a \s-2ESC\s0 as well as an \s-2CR\s0.
  322. .FE
  323. this is a dangerous but occasionally essential
  324. command which ends the editor session and discards all your changes.
  325. You need to know about this command in case you change the editor's
  326. copy of a file you wish only to look at.  Be very careful
  327. not to give this command when you really want to save
  328. the changes you have made.
  329. .NH 1
  330. Moving around in the file
  331. .NH 2
  332. Scrolling and paging
  333. .PP
  334. The editor has a number of commands for moving around in the file.
  335. The most useful of these is generated by hitting the control and D keys
  336. at the same time, a control-D or `^D'.  We will use this two character
  337. notation for referring to these control keys from now on.  You may have
  338. a key labelled `^' on your terminal.  This key will be represented as `\(ua'
  339. in this document; `^' is exclusively used as part of the `^x' notation
  340. for control characters.\*(dd
  341. .FS
  342. \*(dd If you don't have a `^' key on your terminal
  343. then there is probably a key labelled `\(ua'; in any case these characters
  344. are one and the same.
  345. .FE
  346. .PP
  347. As you know now if you tried hitting \fB^D\fR, this command scrolls down in
  348. the file.  The \fBD\fR thus stands for down.  Many editor commands are mnemonic
  349. and this makes them much easier to remember.  For instance the command
  350. to scroll up is \fB^U\fR.  Many dumb terminals can't scroll up at all, in which
  351. case hitting \fB^U\fR clears the screen and refreshes it
  352. with a line which is farther back in the file at the top.
  353. .PP
  354. If you want to see more of the file below where you are, you can
  355. hit \fB^E\fR to expose one more line at the bottom of the screen,
  356. leaving the cursor where it is.
  357. The command \fB^Y\fR (which is hopelessly non-mnemonic, but next to \fB^U\fR
  358. on the keyboard) exposes one more line at the top of the screen.
  359. .PP
  360. There are other ways to move around in the file; the keys \fB^F\fR and \fB^B\fR
  361. move forward and backward a page,
  362. keeping a couple of lines of continuity between screens
  363. so that it is possible to read through a file using these rather than
  364. \fB^D\fR and \fB^U\fR if you wish.
  365. .PP
  366. Notice the difference between scrolling and paging.  If you are trying
  367. to read the text in a file, hitting \fB^F\fR to move forward a page
  368. will leave you only a little context to look back at.  Scrolling on the
  369. other hand leaves more context, and happens more smoothly.  You can continue
  370. to read the text as scrolling is taking place.
  371. .NH 2
  372. Searching, goto, and previous context
  373. .PP
  374. Another way to position yourself in the file is by giving the editor a string
  375. to search for.  Type the character \fB/\fR followed by a string of characters
  376. terminated by \s-2CR\s0.  The editor will position the cursor
  377. at the next occurrence of this string.
  378. Try hitting \fBn\fR to then go to the next occurrence of this string.
  379. The character \fB?\fR will search backwards from where you are, and is
  380. otherwise like \fB/\fR.\*(dg
  381. .FS
  382. \*(dg These searches will normally wrap around the end of the file, and thus
  383. find the string even if it is not on a line in the direction you search
  384. provided it is anywhere else in the file.  You can disable this wraparound
  385. in scans by giving the command \fB:se nowrapscan\fR\s-2CR\s0,
  386. or more briefly \fB:se nows\fR\s-2CR\s0.
  387. .FE
  388. .PP
  389. If the search string you give the editor is not present in the
  390. file the editor will print
  391. a diagnostic on the last line of the screen, and the cursor will be returned
  392. to its initial position.
  393. .PP
  394. If you wish the search to match only at the beginning of a line, begin
  395. the search string with an \fB\(ua\fR.  To match only at the end of
  396. a line, end the search string with a \fB$\fR.
  397. Thus \fB/\(uasearch\fR\s-2CR\s0 will search for the word `search' at
  398. the beginning of a line, and \fB/last$\fR\s-2CR\s0 searches for the
  399. word `last' at the end of a line.*
  400. .FS
  401. *Actually, the string you give to search for here can be a
  402. .I "regular expression"
  403. in the sense of the editors
  404. .I ex (1)
  405. and
  406. .I ed (1).
  407. If you don't wish to learn about this yet, you can disable this more
  408. general facility by doing
  409. \fB:se\ nomagic\fR\s-2CR\s0;
  410. by putting this command in
  411. EXINIT
  412. in your environment, you can have this always be in effect (more
  413. about
  414. .I EXINIT
  415. later.)
  416. .FE
  417. .PP
  418. The command \fBG\fR, when preceded by a number will position the cursor
  419. at that line in the file.
  420. Thus \fB1G\fR will move the cursor to
  421. the first line of the file.  If you give \fBG\fR no count, then it moves
  422. to the end of the file.
  423. .PP
  424. If you are near the end of the file, and the last line is not at the bottom
  425. of the screen, the editor will place only the character `~' on each remaining
  426. line.  This indicates that the last line in the file is on the screen;
  427. that is, the `~' lines are past the end of the file.
  428. .PP
  429. You can find out the state of the file you are editing by typing a \fB^G\fR.
  430. The editor will show you the name of the file you are editing, the number
  431. of the current line, the number of lines in the buffer, and the percentage
  432. of the way through the buffer which you are.
  433. Try doing this now, and remember the number of the line you are on.
  434. Give a \fBG\fR command to get to the end and then another \fBG\fR command
  435. to get back where you were.
  436. .PP
  437. You can also get back to a previous position by using the command
  438. \fB\(ga\(ga\fR (two back quotes).
  439. This is often more convenient than \fBG\fR because it requires no advance
  440. preparation.
  441. Try giving a \fBG\fR or a search with \fB/\fR or \fB?\fR and then a
  442. \fB\(ga\(ga\fR to get back to where you were.  If you accidentally hit
  443. \fBn\fR or any command which moves you far away from a context of interest, you
  444. can quickly get back by hitting \fB\(ga\(ga\fR.
  445. .NH 2
  446. Moving around on the screen
  447. .PP
  448. Now try just moving the cursor around on the screen.
  449. If your terminal has arrow keys (4 or 5 keys with arrows
  450. going in each direction) try them and convince yourself
  451. that they work.
  452. If you don't have working arrow keys, you can always use
  453. .B h ,
  454. .B j ,
  455. .B k ,
  456. and
  457. .B l .
  458. Experienced users of
  459. .I vi
  460. prefer these keys to arrow keys,
  461. because they are usually right underneath their fingers.
  462. .PP
  463. Hit the \fB+\fR key.  Each time you do, notice that the cursor
  464. advances to the next line in the file, at the first non-white position
  465. on the line.  The \fB\-\fR key is like \fB+\fR but goes the other way.
  466. .PP
  467. These are very common keys for moving up and down lines in the file.
  468. Notice that if you go off the bottom or top with these keys then the
  469. screen will scroll down (and up if possible) to bring a line at a time
  470. into view.  The \s-2RETURN\s0 key has the same effect as the \fB+\fR
  471. key.
  472. .PP
  473. .I Vi
  474. also has commands to take you to the top, middle and bottom of the screen.
  475. \fBH\fR will take you to the top (home) line on the screen.
  476. Try preceding it with a
  477. number as in \fB3H\fR.
  478. This will take you to the third line on the screen.
  479. Many
  480. .I vi
  481. commands take preceding numbers and do interesting things with them.
  482. Try \fBM\fR,
  483. which takes you to the middle line on the screen,
  484. and \fBL\fR,
  485. which takes you to the last line on the screen.
  486. \fBL\fR also takes counts, thus
  487. \fB5L\fR will take you to the fifth line from the bottom.
  488. .NH 2
  489. Moving within a line
  490. .PP
  491. Now try picking a word on some line on the screen, not the
  492. first word on the line.
  493. move the cursor using \s-2RETURN\s0 and \fB\-\fR to be on the line where
  494. the word is.
  495. Try hitting the \fBw\fR key.  This will advance the cursor to the
  496. next word on the line.
  497. Try hitting the \fBb\fR key to back up words
  498. in the line.
  499. Also try the \fBe\fR key which advances you to the end of the current
  500. word rather than to the beginning of the next word.
  501. Also try \s-2SPACE\s0 (the space bar) which moves right one character
  502. and the \s-2BS\s0 (backspace or \fB^H\fR) key which moves left one character.
  503. The key \fBh\fR works as \fB^H\fR does and is useful if you don't have
  504. a \s-2BS\s0 key.
  505. (Also, as noted just above, \fBl\fR will move to the right.)
  506. .PP
  507. If the line had punctuation in it you may have noticed that
  508. that the \fBw\fR and \fBb\fR
  509. keys stopped at each group of punctuation.  You can also go back and
  510. forwards words without stopping at punctuation by using \fBW\fR and \fBB\fR
  511. rather than the lower case equivalents.  Think of these as bigger words.
  512. Try these on a few lines with punctuation to see how they differ from
  513. the lower case \fBw\fR and \fBb\fR.
  514. .PP
  515. The word keys wrap around the end of line,
  516. rather than stopping at the end.  Try moving to a word on a line below
  517. where you are by repeatedly hitting \fBw\fR.
  518. .NH 2
  519. Summary
  520. .IP
  521. .TS
  522. lw(.50i)b a.
  523. \fR\s-2SPACE\s0\fP    advance the cursor one position
  524. ^B    backwards to previous page
  525. ^D    scrolls down in the file
  526. ^E    exposes another line at the bottom
  527. ^F    forward to next page
  528. ^G    tell what is going on
  529. ^H    backspace the cursor
  530. ^N    next line, same column
  531. ^P    previous line, same column
  532. ^U    scrolls up in the file
  533. ^Y    exposes another line at the top
  534. +    next line, at the beginning
  535. \-    previous line, at the beginning
  536. /    scan for a following string forwards
  537. ?    scan backwards
  538. B    back a word, ignoring punctuation
  539. G    go to specified line, last default
  540. H    home screen line
  541. M    middle screen line
  542. L    last screen line
  543. W    forward a word, ignoring punctuation
  544. b    back a word
  545. e    end of current word
  546. n    scan for next instance of \fB/\fR or \fB?\fR pattern
  547. w    word after this word
  548. .TE
  549. .NH 2
  550. View
  551. .PP
  552. If you want to use the editor to look at a file,
  553. rather than to make changes,
  554. invoke it as
  555. .I view
  556. instead of
  557. .I vi .
  558. This will set the
  559. .I readonly
  560. option which will prevent you from
  561. accidently overwriting the file.
  562. .NH 1
  563. Making simple changes
  564. .NH 2
  565. Inserting
  566. .PP
  567. One of the most useful commands is the
  568. \fBi\fR (insert) command.
  569. After you type \fBi\fR, everything you type until you hit \s-2ESC\s0
  570. is inserted into the file.
  571. Try this now; position yourself to some word in the file and try inserting
  572. text before this word.
  573. If you are on an dumb terminal it will seem, for a minute,
  574. that some of the characters in your line have been overwritten, but they will
  575. reappear when you hit \s-2ESC\s0.
  576. .PP
  577. Now try finding a word which can, but does not, end in an `s'.
  578. Position yourself at this word and type \fBe\fR (move to end of word), then
  579. \fBa\fR for append and then `s\s-2ESC\s0' to terminate the textual insert.
  580. This sequence of commands can be used to easily pluralize a word.
  581. .PP
  582. Try inserting and appending a few times to make sure you understand how
  583. this works; \fBi\fR placing text to the left of the cursor, \fBa\fR to
  584. the right.
  585. .PP
  586. It is often the case that you want to add new lines to the file you are
  587. editing, before or after some specific line in the file.  Find a line
  588. where this makes sense and then give the command \fBo\fR to create a
  589. new line after the line you are on, or the command \fBO\fR to create
  590. a new line before the line you are on.  After you create a new line in
  591. this way, text you type up to an \s-2ESC\s0 is inserted on the new line.
  592. .PP
  593. Many related editor commands
  594. are invoked by the same letter key and differ only in that one is given
  595. by a lower
  596. case key and the other is given by
  597. an upper case key.  In these cases, the
  598. upper case key often differs from the lower case key in its sense of
  599. direction, with
  600. the upper case key working backward and/or up, while the lower case
  601. key moves forward and/or down.
  602. .PP
  603. Whenever you are typing in text, you can give many lines of input or
  604. just a few characters.
  605. To type in more than one line of text,
  606. hit a \s-2RETURN\s0 at the middle of your input.  A new line will be created
  607. for text, and you can continue to type.  If you are on a slow
  608. and dumb terminal the editor may choose to wait to redraw the
  609. tail of the screen, and will let you type over the existing screen lines.
  610. This avoids the lengthy delay which would occur if the editor attempted
  611. to keep the tail of the screen always up to date.  The tail of the screen will
  612. be fixed up, and the missing lines will reappear, when you hit \s-2ESC\s0.
  613. .PP
  614. While you are inserting new text, you can use the characters you normally use
  615. at the system command level (usually \fB^H\fR or \fB#\fR) to backspace
  616. over the last
  617. character which you typed, and the character which you use to kill input lines
  618. (usually \fB@\fR, \fB^X\fR, or \fB^U\fR)
  619. to erase the input you have typed on the current line.\*(dg
  620. .FS
  621. \*(dg In fact, the character \fB^H\fR (backspace) always works to erase the
  622. last input character here, regardless of what your erase character is.
  623. .FE
  624. The character \fB^W\fR
  625. will erase a whole word and leave you after the space after the previous
  626. word; it is useful for quickly backing up in an insert.
  627. .PP
  628. Notice that when you backspace during an insertion the characters you
  629. backspace over are not erased; the cursor moves backwards, and the characters
  630. remain on the display.  This is often useful if you are planning to type
  631. in something similar.  In any case the characters disappear when when
  632. you hit \s-2ESC\s0; if you want to get rid of them immediately, hit an
  633. \s-2ESC\s0 and then \fBa\fR again.
  634. .PP
  635. Notice also that you can't erase characters which you didn't insert, and that
  636. you can't backspace around the end of a line.  If you need to back up
  637. to the previous line to make a correction, just hit \s-2ESC\s0 and move
  638. the cursor back to the previous line.  After making the correction you
  639. can return to where you were and use the insert or append command again.
  640. .NH 2
  641. Making small corrections
  642. .PP
  643. You can make small corrections in existing text quite easily.
  644. Find a single character which is wrong or just pick any character.
  645. Use the arrow keys to find the character, or
  646. get near the character with the word motion keys and then either
  647. backspace (hit the \s-2BS\s0 key or \fB^H\fR or even just \fBh\fR) or 
  648. \s-2SPACE\s0 (using the space bar)
  649. until the cursor is on the character which is wrong.
  650. If the character is not needed then hit the \fBx\fP key; this deletes
  651. the character from the file.  It is analogous to the way you \fBx\fP
  652. out characters when you make mistakes on a typewriter (except it's not
  653. as messy).
  654. .PP
  655. If the character
  656. is incorrect, you can replace it with the correct character by giving
  657. the command \fBr\fR\fIc\fR,
  658. where \fIc\fR is replaced by the correct character.
  659. Finally if the character which is incorrect should be replaced
  660. by more than one character, give the command \fBs\fR which substitutes
  661. a string of characters, ending with \s-2ESC\s0, for it.
  662. If there are a small number of characters
  663. which are wrong you can precede \fBs\fR with a count of the number of
  664. characters to be replaced.  Counts are also useful with \fBx\fR to specify
  665. the number of characters to be deleted.
  666. .NH 2
  667. More corrections: operators
  668. .PP
  669. You already know almost enough to make changes at a higher level.
  670. All you need to know now is that the 
  671. .B d
  672. key acts as a delete operator.  Try the command
  673. .B dw
  674. to delete a word.
  675. Try hitting \fB.\fR a few times.  Notice that this repeats the effect
  676. of the \fBdw\fR.  The command \fB.\fR repeats the last command which
  677. made a change.  You can remember it by analogy with an ellipsis `\fB...\fR'.
  678. .PP
  679. Now try
  680. \fBdb\fR.
  681. This deletes a word backwards, namely the preceding word.
  682. Try 
  683. \fBd\fR\s-2SPACE\s0.  This deletes a single character, and is equivalent
  684. to the \fBx\fR command.
  685. .PP
  686. Another very useful operator is
  687. .B c
  688. or change.  The command 
  689. .B cw
  690. thus changes the text of a single word.
  691. You follow it by the replacement text ending with an \s-2ESC\s0.
  692. Find a word which you can change to another, and try this
  693. now.
  694. Notice that the end of the text to be changed was marked with the character
  695. `$' so that you can see this as you are typing in the new material.
  696. .NH 2
  697. Operating on lines
  698. .PP
  699. It is often the case that you want to operate on lines.
  700. Find a line which you want to delete, and type 
  701. \fBdd\fR,
  702. the
  703. .B d
  704. operator twice.  This will delete the line.
  705. If you are on a dumb terminal, the editor may just erase the line on
  706. the screen, replacing it with a line with only an @ on it.  This line
  707. does not correspond to any line in your file, but only acts as a place
  708. holder.  It helps to avoid a lengthy redraw of the rest of the screen
  709. which would be necessary to close up the hole created by the deletion
  710. on a terminal without a delete line capability.
  711. .PP
  712. Try repeating the
  713. .B c
  714. operator twice; this will change a whole line, erasing its previous contents and
  715. replacing them with text you type up to an \s-2ESC\s0.\*(dg
  716. .FS
  717. \*(dg The command \fBS\fR is a convenient synonym for for \fBcc\fR, by
  718. analogy with \fBs\fR.  Think of \fBS\fR as a substitute on lines, while
  719. \fBs\fR is a substitute on characters.
  720. .FE
  721. .PP
  722. You can delete or change more than one line by preceding the
  723. .B dd
  724. or
  725. .B cc
  726. with a count, i.e. \fB5dd\fR deletes 5 lines.
  727. You can also give a command like \fBdL\fR to delete all the lines up to
  728. and including
  729. the last line on the screen, or \fBd3L\fR to delete through the third from
  730. the bottom line.  Try some commands like this now.*
  731. .FS
  732. * One subtle point here involves using the \fB/\fR search after a \fBd\fR.
  733. This will normally delete characters from the current position to the
  734. point of the match.  If what is desired is to delete whole lines
  735. including the two points, give the pattern as \fB/pat/+0\fR, a line address.
  736. .FE
  737. Notice that the editor lets you know when you change a large number of
  738. lines so that you can see the extent of the change.
  739. The editor will also always tell you when a change you make affects text which
  740. you cannot see.
  741. .NH 2
  742. Undoing
  743. .PP
  744. Now suppose that the last change which you made was incorrect;
  745. you could use the insert, delete and append commands to put the correct
  746. material back.  However, since it is often the case that we regret a
  747. change or make a change incorrectly, the editor provides a
  748. .B u
  749. (undo) command to reverse the last change which you made.
  750. Try this a few times, and give it twice in a row to notice that an
  751. .B u
  752. also undoes a
  753. .B u.
  754. .PP
  755. The undo command lets you reverse only a single change.  After you make
  756. a number of changes to a line, you may decide that you would rather have
  757. the original state of the line back.  The
  758. .B U
  759. command restores the current line to the state before you started changing
  760. it.
  761. .PP
  762. You can recover text which you delete, even if
  763. undo will not bring it back; see the section on recovering lost text
  764. below.
  765. .NH 2
  766. Summary
  767. .IP
  768. .TS
  769. lw(.50i)b a.
  770. \fR\s-2SPACE\s0\fP    advance the cursor one position
  771. ^H    backspace the cursor
  772. ^W    erase a word during an insert
  773. \fRerase\fP    your erase (usually ^H or #), erases a character during an insert
  774. \fRkill\fP    your kill (usually @, ^X, or ^U), kills the insert on this line
  775. \&\fB.\fP    repeats the changing command
  776. O    opens and inputs new lines, above the current
  777. U    undoes the changes you made to the current line
  778. a    appends text after the cursor
  779. c    changes the object you specify to the following text
  780. d    deletes the object you specify
  781. i    inserts text before the cursor
  782. o    opens and inputs new lines, below the current
  783. u    undoes the last change
  784. .TE
  785. .NH 1
  786. Moving about; rearranging and duplicating text
  787. .NH 2
  788. Low level character motions
  789. .PP
  790. Now move the cursor to a line where there is a punctuation or a bracketing
  791. character such as a parenthesis or a comma or period.  Try the command
  792. \fBf\fR\fIx\fR where \fIx\fR is this character.  This command finds
  793. the next \fIx\fR character to the right of the cursor in the current
  794. line.  Try then hitting a \fB;\fR, which finds the next instance of the
  795. same character.  By using the \fBf\fR command and then a sequence of
  796. \fB;\fR's you can often
  797. get to a particular place in a line much faster than with a sequence
  798. of word motions or \s-2SPACE\s0s.
  799. There is also a \fBF\fR command, which is like \fBf\fR, but searches 
  800. backward.  The \fB;\fR command repeats \fBF\fR also.
  801. .PP
  802. When you are operating on the text in a line it is often desirable to
  803. deal with the characters up to, but not including, the first instance of
  804. a character.  Try \fBdf\fR\fIx\fR for some \fIx\fR now and
  805. notice that the \fIx\fR character is deleted.  Undo this with \fBu\fR
  806. and then try \fBdt\fR\fIx\fR;  the \fBt\fR here stands for to, i.e.
  807. delete up to the next \fIx\fR, but not the \fIx\fR.  The command \fBT\fR
  808. is the reverse of \fBt\fR.
  809. .PP
  810. When working with the text of a single line, an \fB\(ua\fR moves the
  811. cursor to the first non-white position on the line, and a
  812. \fB$\fR moves it to the end of the line.  Thus \fB$a\fR will append new
  813. text at the end of the current line.
  814. .PP
  815. Your file may have tab (\fB^I\fR) characters in it.  These
  816. characters are represented as a number of spaces expanding to a tab stop,
  817. where tab stops are every 8 positions.*
  818. .FS
  819. * This is settable by a command of the form \fB:se ts=\fR\fIx\fR\s-2CR\s0,
  820. where \fIx\fR is 4 to set tabstops every four columns.  This has
  821. effect on the screen representation within the editor.
  822. .FE
  823. When the cursor is at a tab, it sits on the last of the several spaces
  824. which represent that tab.  Try moving the cursor back and forth over
  825. tabs so you understand how this works.
  826. .PP
  827. On rare occasions, your file may have nonprinting characters in it. 
  828. These characters are displayed in the same way they are represented in
  829. this document, that is with a two character code, the first character
  830. of which is `^'.  On the screen non-printing characters resemble a `^'
  831. character adjacent to another, but spacing or backspacing over the character
  832. will reveal that the two characters are, like the spaces representing
  833. a tab character, a single character.
  834. .PP
  835. The editor sometimes discards control characters,
  836. depending on the character and the setting of the
  837. .I beautify
  838. option,
  839. if you attempt to insert them in your file.
  840. You can get a control character in the file by beginning
  841. an insert and then typing a \fB^V\fR before the control
  842. character.  The
  843. \fB^V\fR quotes the following character, causing it to be
  844. inserted directly into the file.
  845. .PP
  846. .NH 2
  847. Higher level text objects
  848. .PP
  849. In working with a document it is often advantageous to work in terms
  850. of sentences, paragraphs, and sections.  The operations \fB(\fR and \fB)\fR
  851. move to the beginning of the previous and next sentences respectively.
  852. Thus the command \fBd)\fR will delete the rest of the current sentence;
  853. likewise \fBd(\fR will delete the previous sentence if you are at the
  854. beginning of the current sentence, or the current sentence up to where
  855. you are if you are not at the beginning of the current sentence.
  856. .PP
  857. A sentence is defined to end at a `.', `!' or `?' which is followed by
  858. either the end of a line, or by two spaces.  Any number of closing `)',
  859. `]', `"' and `\(aa' characters may appear after the `.', `!' or `?' before
  860. the spaces or end of line.
  861. .PP
  862. The operations \fB{\fR and \fB}\fR move over paragraphs and the operations
  863. \fB[[\fR and \fB]]\fR move over sections.\*(dg
  864. .FS
  865. \*(dg The \fB[[\fR and \fB]]\fR operations
  866. require the operation character to be doubled because they can move the
  867. cursor far from where it currently is.  While it is easy to get back
  868. with the command \fB\(ga\(ga\fP,
  869. these commands would still be frustrating
  870. if they were easy to hit accidentally.
  871. .FE
  872. .PP
  873. A paragraph begins after each empty line, and also
  874. at each of a set of paragraph macros, specified by the pairs of characters
  875. in the definition of the string valued option \fIparagraphs\fR.
  876. The default setting for this option defines the paragraph macros of the
  877. \fI\-ms\fR and \fI\-mm\fR macro packages, i.e. the `.IP', `.LP', `.PP'
  878. and `.QP', `.P' and `.LI' macros.\*(dd
  879. .FS
  880. \*(dd You can easily change or extend this set of macros by assigning a
  881. different string to the \fIparagraphs\fR option in your EXINIT.
  882. See section 6.2 for details.
  883. The `.bp' directive is also considered to start a paragraph.
  884. .FE
  885. Each paragraph boundary is also a sentence boundary.  The sentence
  886. and paragraph commands can
  887. be given counts to operate over groups of sentences and paragraphs.
  888. .PP
  889. Sections in the editor begin after each macro in the \fIsections\fR option,
  890. normally `.NH', `.SH', `.H' and `.HU', and each line with a formfeed \fB^L\fR
  891. in the first column.
  892. Section boundaries are always line and paragraph boundaries also.
  893. .PP
  894. Try experimenting with the sentence and paragraph commands until you are
  895. sure how they work.  If you have a large document, try looking through
  896. it using the section commands.
  897. The section commands interpret a preceding count as a different window size in
  898. which to redraw the screen at the new location, and this window size
  899. is the base size for newly drawn windows until another size is specified.
  900. This is very useful
  901. if you are on a slow terminal and are looking for a particular section. 
  902. You can give the first section command a small count to then see each successive
  903. section heading in a small window.
  904. .NH 2
  905. Rearranging and duplicating text
  906. .PP
  907. The editor has a single unnamed buffer where the last deleted or
  908. changed away text is saved, and a set of named buffers \fBa\fR\-\fBz\fR
  909. which you can use to save copies of text and to move text around in
  910. your file and between files.
  911. .PP
  912. The operator
  913. .B y
  914. yanks a copy of the object which follows into the unnamed buffer.
  915. If preceded by a buffer name, \fB"\fR\fIx\fR\|\fBy\fR, where
  916. \fIx\fR here is replaced by a letter \fBa\-z\fR, it places the text in the named
  917. buffer.  The text can then be put back in the file with the commands
  918. .B p
  919. and
  920. .B P;
  921. \fBp\fR puts the text after or below the cursor, while \fBP\fR puts the text
  922. before or above the cursor.
  923. .PP
  924. If the text which you
  925. yank forms a part of a line, or is an object such as a sentence which
  926. partially spans more than one line, then when you put the text back,
  927. it will be placed after the cursor (or before if you
  928. use \fBP\fR).  If the yanked text forms whole lines, they will be put
  929. back as whole lines, without changing the current line.  In this case,
  930. the put acts much like a \fBo\fR or \fBO\fR command.
  931. .PP
  932. Try the command \fBYP\fR.  This makes a copy of the current line and
  933. leaves you on this copy, which is placed before the current line.
  934. The command \fBY\fR is a convenient abbreviation for \fByy\fR.
  935. The command \fBYp\fR will also make a copy of the current line, and place
  936. it after the current line.  You can give \fBY\fR a count of lines to
  937. yank, and thus duplicate several lines; try \fB3YP\fR.
  938. .PP
  939. To move text within the buffer, you need to delete it in one place, and
  940. put it back in another.  You can precede a delete operation by the
  941. name of a buffer in which the text is to be stored as in \fB"a5dd\fR
  942. deleting 5 lines into the named buffer \fIa\fR.  You can then move the
  943. cursor to the eventual resting place of the these lines and do a \fB"ap\fR
  944. or \fB"aP\fR to put them back.
  945. In fact, you can switch and edit another file before you put the lines
  946. back, by giving a command of the form \fB:e \fR\fIname\fR\s-2CR\s0 where
  947. \fIname\fR is the name of the other file you want to edit.  You will
  948. have to write back the contents of the current editor buffer (or discard
  949. them) if you have made changes before the editor will let you switch
  950. to the other file.
  951. An ordinary delete command saves the text in the unnamed buffer,
  952. so that an ordinary put can move it elsewhere.
  953. However, the unnamed buffer is lost when you change files,
  954. so to move text from one file to another you should use an unnamed buffer.
  955. .NH 2
  956. Summary.
  957. .IP
  958. .TS
  959. lw(.50i)b a.
  960. \(ua    first non-white on line
  961. $    end of line
  962. )    forward sentence
  963. }    forward paragraph
  964. ]]    forward section
  965. (    backward sentence
  966. {    backward paragraph
  967. [[    backward section
  968. f\fIx\fR    find \fIx\fR forward in line
  969. p    put text back, after cursor or below current line
  970. y    yank operator, for copies and moves
  971. t\fIx\fR    up to \fIx\fR forward, for operators
  972. F\fIx\fR    f backward in line
  973. P    put text back, before cursor or above current line
  974. T\fIx\fR    t backward in line
  975. .TE
  976. .NH 1
  977. High level commands
  978. .NH 2
  979. Writing, quitting, editing new files
  980. .PP
  981. So far we have seen how to enter
  982. .I vi
  983. and to write out our file using either
  984. \fBZZ\fR or \fB:w\fR\s-2CR\s0. The first exits from
  985. the editor,
  986. (writing if changes were made),
  987. the second writes and stays in the editor.
  988. .PP
  989. If you have changed the editor's copy of the file but do not wish to
  990. save your changes, either because you messed up the file or decided that the
  991. changes are not an improvement to the file, then you can give the command
  992. \fB:q!\fR\s-2CR\s0 to quit from the editor without writing the changes.
  993. You can also reedit the same file (starting over) by giving the command
  994. \fB:e!\fR\s-2CR\s0.  These commands should be used only rarely, and with
  995. caution, as it is not possible to recover the changes you have made after
  996. you discard them in this manner.
  997. .PP
  998. You can edit a different file without leaving the editor by giving the
  999. command \fB:e\fR\ \fIname\fR\s-2CR\s0.  If you have not written out
  1000. your file before you try to do this, then the editor will tell you this,
  1001. and delay editing the other file.  You can then give the command
  1002. \fB:w\fR\s-2CR\s0 to save your work and then the \fB:e\fR\ \fIname\fR\s-2CR\s0
  1003. command again, or carefully give the command \fB:e!\fR\ \fIname\fR\s-2CR\s0,
  1004. which edits the other file discarding the changes you have made to the
  1005. current file.
  1006. To have the editor automatically save changes,
  1007. include
  1008. .I "set autowrite"
  1009. in your EXINIT,
  1010. and use \fB:n\fP instead of \fB:e\fP.
  1011. .NH 2
  1012. Escaping to a shell
  1013. .PP
  1014. You can get to a shell to execute a single command by giving a
  1015. .I vi
  1016. command of the form \fB:!\fIcmd\fR\s-2CR\s0.
  1017. The system will run the single command
  1018. .I cmd
  1019. and when the command finishes, the editor will ask you to hit a \s-2RETURN\s0
  1020. to continue.  When you have finished looking at the output on the screen,
  1021. you should hit \s-2RETURN\s0 and the editor will clear the screen and
  1022. redraw it.  You can then continue editing.
  1023. You can also give another \fB:\fR command when it asks you for a \s-2RETURN\s0;
  1024. in this case the screen will not be redrawn.
  1025. .PP
  1026. If you wish to execute more than one command in the shell, then you can
  1027. give the command \fB:sh\fR\s-2CR\s0.
  1028. This will give you a new shell, and when you finish with the shell, ending
  1029. it by typing a \fB^D\fR, the editor will clear the screen and continue.
  1030. .PP
  1031. On systems which support it, \fB^Z\fP will suspend the editor
  1032. and return to the (top level) shell.
  1033. When the editor is resumed, the screen will be redrawn.
  1034. .NH 2
  1035. Marking and returning
  1036. .PP
  1037. The command \fB\(ga\(ga\fR returned to the previous place
  1038. after a motion of the cursor by a command such as \fB/\fR, \fB?\fR or
  1039. \fBG\fR.  You can also mark lines in the file with single letter tags
  1040. and return to these marks later by naming the tags.  Try marking the
  1041. current line with the command \fBm\fR\fIx\fR, where you should pick some
  1042. letter for \fIx\fR, say `a'.  Then move the cursor to a different line
  1043. (any way you like) and hit \fB\(gaa\fR.  The cursor will return to the
  1044. place which you marked.
  1045. Marks last only until you edit another file.
  1046. .PP
  1047. When using operators such as
  1048. .B d
  1049. and referring to marked lines, it is often desirable to delete whole lines
  1050. rather than deleting to the exact position in the line marked by \fBm\fR.
  1051. In this case you can use the form \fB\(aa\fR\fIx\fR rather than
  1052. \fB\(ga\fR\fIx\fR.  Used without an operator, \fB\(aa\fR\fIx\fR will move to
  1053. the first non-white character of the marked line; similarly \fB\(aa\(aa\fR
  1054. moves to the first non-white character of the line containing the previous
  1055. context mark \fB\(ga\(ga\fR.
  1056. .NH 2
  1057. Adjusting the screen
  1058. .PP
  1059. If the screen image is messed up because of a transmission error to your
  1060. terminal, or because some program other than the editor wrote output
  1061. to your terminal, you can hit a \fB^L\fR, the \s-2ASCII\s0 form-feed
  1062. character, to cause the screen to be refreshed.
  1063. .PP
  1064. On a dumb terminal, if there are @ lines in the middle of the screen
  1065. as a result of line deletion, you may get rid of these lines by typing
  1066. \fB^R\fR to cause the editor to retype the screen, closing up these holes.
  1067. .PP
  1068. Finally, if you wish to place a certain line on the screen at the top
  1069. middle or bottom of the screen, you can position the cursor to that line,
  1070. and then give a \fBz\fR command.
  1071. You should follow the \fBz\fR command with a \s-2RETURN\s0 if you want
  1072. the line to appear at the top of the window, a \fB.\fR if you want it
  1073. at the center, or a \fB\-\fR if you want it at the bottom.
  1074. .NH 1
  1075. Special topics
  1076. .NH 2
  1077. Editing on slow terminals
  1078. .PP
  1079. When you are on a slow terminal, it is important to limit the amount
  1080. of output which is generated to your screen so that you will not suffer
  1081. long delays, waiting for the screen to be refreshed.  We have already
  1082. pointed out how the editor optimizes the updating of the screen during
  1083. insertions on dumb terminals to limit the delays, and how the editor erases
  1084. lines to @ when they are deleted on dumb terminals.
  1085. .PP
  1086. The use of the slow terminal insertion mode is controlled by the
  1087. .I slowopen
  1088. option.  You can force the editor to use this mode even on faster terminals
  1089. by giving the command \fB:se slow\fR\s-2CR\s0.  If your system is sluggish
  1090. this helps lessen the amount of output coming to your terminal.
  1091. You can disable this option by \fB:se noslow\fR\s-2CR\s0.
  1092. .PP
  1093. The editor can simulate an intelligent terminal on a dumb one.  Try
  1094. giving the command \fB:se redraw\fR\s-2CR\s0.  This simulation generates
  1095. a great deal of output and is generally tolerable only on lightly loaded
  1096. systems and fast terminals.  You can disable this by giving the command
  1097.  \fB:se noredraw\fR\s-2CR\s0.
  1098. .PP
  1099. The editor also makes editing more pleasant at low speed by starting
  1100. editing in a small window, and letting the window expand as you edit.
  1101. This works particularly well on intelligent terminals.  The editor can
  1102. expand the window easily when you insert in the middle of the screen
  1103. on these terminals.  If possible, try the editor on an intelligent terminal
  1104. to see how this works.
  1105. .PP
  1106. You can control the size of the window which is redrawn each time the
  1107. screen is cleared by giving window sizes as argument to the commands
  1108. which cause large screen motions:
  1109. .DS
  1110. .B ":  /  ?  [[  ]]  \(ga  \(aa"
  1111. .DE
  1112. Thus if you are searching for a particular instance of a common string
  1113. in a file you can precede the first search command by a small number,
  1114. say 3, and the editor will draw three line windows around each instance
  1115. of the string which it locates.
  1116. .PP
  1117. You can easily expand or contract the window, placing the current line
  1118. as you choose, by giving a number on a \fBz\fR command, after the \fBz\fR
  1119. and before the following \s-2RETURN\s0, \fB.\fR or \fB\-\fR.  Thus the
  1120. command \fBz5.\fR redraws the screen with the current line in the center
  1121. of a five line window.\*(dg
  1122. .FS
  1123. \*(dg Note that the command \fB5z.\fR has an entirely different effect,
  1124. placing line 5 in the center of a new window.
  1125. .FE
  1126. .PP
  1127. If the editor is redrawing or otherwise updating large portions of the
  1128. display, you can interrupt this updating by hitting a \s-2DEL\s0 or \s-2RUB\s0
  1129. as usual.  If you do this you may partially confuse the editor about
  1130. what is displayed on the screen.  You can still edit the text on
  1131. the screen if you wish; clear up the confusion
  1132. by hitting a \fB^L\fR; or move or search again, ignoring the
  1133. current state of the display.
  1134. .PP
  1135. See section 7.8 on \fIopen\fR mode for another way to use the
  1136. .I vi
  1137. command set on slow terminals.
  1138. .NH 2
  1139. Options, set, and editor startup files
  1140. .PP
  1141. The editor has a set of options, some of which have been mentioned above.
  1142. The most useful options are given in the following table.
  1143. .KF
  1144. .TS
  1145. lb lb lb lb
  1146. l l l a.
  1147. Name    Default    Description
  1148. _
  1149. autoindent    noai    Supply indentation automatically
  1150. autowrite    noaw    Automatic write before \fB:n\fR, \fB:ta\fR, \fB^\(ua\fR, \fB!\fR
  1151. ignorecase    noic    Ignore case in searching
  1152. lisp    nolisp    \fB( { ) }\fR commands deal with S-expressions
  1153. list    nolist    Tabs print as ^I; end of lines marked with $
  1154. magic    nomagic    The characters . [ and * are special in scans
  1155. number    nonu    Lines are displayed prefixed with line numbers
  1156. paragraphs    para=IPLPPPQPbpP LI    Macro names which start paragraphs
  1157. redraw    nore    Simulate a smart terminal on a dumb one
  1158. sections    sect=NHSHH HU    Macro names which start new sections
  1159. shiftwidth    sw=8    Shift distance for <, > and input \fB^D\fP and \fB^T\fR
  1160. showmatch    nosm    Show matching \fB(\fP or \fB{\fP as \fB)\fP or \fB}\fR is typed
  1161. slowopen    slow    Postpone display updates during inserts
  1162. term    dumb    The kind of terminal you are using.
  1163. .TE
  1164. .KE
  1165. .PP
  1166. The options are of three kinds:  numeric options, string options, and
  1167. toggle options.  You can set numeric and string options by a statement
  1168. of the form
  1169. .DS
  1170. \fBset\fR \fIopt\fR\fB=\fR\fIval\fR
  1171. .DE
  1172. and toggle options can be set or unset by statements of one of the forms
  1173. .DS
  1174. \fBset\fR \fIopt\fR
  1175. \fBset\fR \fBno\fR\fIopt\fR
  1176. .DE
  1177. These statements can be placed in your EXINIT in your environment,
  1178. or given while you are running
  1179. .I vi
  1180. by preceding them with a \fB:\fR and following them with a \s-2CR\s0.
  1181. .PP
  1182. You can get a list of all options which you have changed by the
  1183. command \fB:set\fR\s-2CR\s0, or the value of a single option by the
  1184. command \fB:set\fR \fIopt\fR\fB?\fR\s-2CR\s0.
  1185. A list of all possible options and their values is generated by
  1186. \fB:set all\fP\s-2CR\s0.
  1187. Set can be abbreviated \fBse\fP.
  1188. Multiple options can be placed on one line, e.g.
  1189. \fB:se ai aw nu\fP\s-2CR\s0.
  1190. .PP
  1191. Options set by the \fBset\fP command only last
  1192. while you stay in the editor.
  1193. It is common to want to have certain options set whenever you
  1194. use the editor.
  1195. This can be accomplished by creating a list of \fIex\fP commands\*(dg
  1196. .FS
  1197. \*(dg
  1198. All commands which start with
  1199. .B :
  1200. are \fIex\fP commands.
  1201. .FE
  1202. which are to be run every time you start up \fIex\fP, \fIedit\fP,
  1203. or \fIvi\fP.
  1204. A typical list includes a \fBset\fP command, and possibly a few
  1205. \fBmap\fP commands.
  1206. Since it is advisable to get these commands on one line, they can
  1207. be separated with the | character, for example:
  1208. .DS
  1209. \fBset\fP ai aw terse|\fBmap\fP @ dd|\fBmap\fP # x
  1210. .DE
  1211. which sets the options \fIautoindent\fP, \fIautowrite\fP, \fIterse\fP,
  1212. (the
  1213. .B set
  1214. command),
  1215. makes @ delete a line,
  1216. (the first
  1217. .B map ),
  1218. and makes # delete a character,
  1219. (the second
  1220. .B map ).
  1221. (See section 6.9 for a description of the \fBmap\fP command)
  1222. This string should be placed in the variable EXINIT in your environment.
  1223. If you use the shell \fIcsh\fP,
  1224. put this line in the file
  1225. .I .login
  1226. in your home directory:
  1227. .DS
  1228. setenv EXINIT \(aa\fBset\fP ai aw terse|\fBmap\fP @ dd|\fBmap\fP # x\(aa
  1229. .DE
  1230. If you use the standard shell \fIsh\fP,
  1231. put these lines in the file
  1232. .I .profile
  1233. in your home directory:
  1234. .DS
  1235. EXINIT=\(aa\fBset\fP ai aw terse|\fBmap\fP @ dd|\fBmap\fP # x\(aa
  1236. export EXINIT
  1237. .DE
  1238. Of course, the particulars of the line would depend on which options
  1239. you wanted to set.
  1240. .NH 2
  1241. Recovering lost lines
  1242. .PP
  1243. You might have a serious problem if you delete a number of lines and then
  1244. regret that they were deleted.  Despair not, the editor saves the last
  1245. 9 deleted blocks of text in a set of numbered registers 1\-9.
  1246. You can get the \fIn\fR'th previous deleted text back in your file by
  1247. the command
  1248. "\fR\fIn\fR\|\fBp\fR.
  1249. The "\fR here says that a buffer name is to follow,
  1250. \fIn\fR is the number of the buffer you wish to try
  1251. (use the number 1 for now),
  1252. and
  1253. .B p
  1254. is the put command, which puts text in the buffer after the cursor.
  1255. If this doesn't bring back the text you wanted, hit
  1256. .B u
  1257. to undo this and then
  1258. \fB\&.\fR
  1259. (period)
  1260. to repeat the put command.
  1261. In general the
  1262. \fB\&.\fR
  1263. command will repeat the last change you made.
  1264. As a special case, when the last command refers to a numbered text buffer,
  1265. the \fB.\fR command increments the number of the buffer before repeating
  1266. the command.  Thus a sequence of the form
  1267. .DS
  1268. \fB"1pu.u.u.\fR
  1269. .DE
  1270. will, if repeated long enough, show you all the deleted text which has
  1271. been saved for you.
  1272. You can omit the
  1273. .B u
  1274. commands here to gather up all this text in the buffer, or stop after any
  1275. \fB\&.\fR command to keep just the then recovered text.
  1276. The command
  1277. .B P
  1278. can also be used rather than
  1279. .B p
  1280. to put the recovered text before rather than after the cursor.
  1281. .NH 2
  1282. Recovering lost files
  1283. .PP
  1284. If the system crashes, you can recover the work you were doing
  1285. to within a few changes.  You will normally receive mail when you next
  1286. login giving you the name of the file which has been saved for you. 
  1287. You should then change to the directory where you were when the system
  1288. crashed and give a command of the form:
  1289. .DS
  1290. % \fBvi \-r\fR \fIname\fR
  1291. .DE
  1292. replacing \fIname\fR with the name of the file which you were editing.
  1293. This will recover your work to a point near where you left off.\*(dg
  1294. .FS
  1295. \*(dg In rare cases, some of the lines of the file may be lost.  The
  1296. editor will give you the numbers of these lines and the text of the lines
  1297. will be replaced by the string `LOST'.  These lines will almost always
  1298. be among the last few which you changed.  You can either choose to discard
  1299. the changes which you made (if they are easy to remake) or to replace
  1300. the few lost lines by hand.
  1301. .FE
  1302. .PP
  1303. You can get a listing of the files which are saved for you by giving
  1304. the command:
  1305. .DS
  1306. % \fBvi \-r\fR
  1307. .DE
  1308. If there is more than one instance of a particular file saved, the editor
  1309. gives you the newest instance each time you recover it.  You can thus
  1310. get an older saved copy back by first recovering the newer copies.
  1311. .PP
  1312. For this feature to work,
  1313. .I vi
  1314. must be correctly installed by a super user on your system,
  1315. and the
  1316. .I mail
  1317. program must exist to receive mail.
  1318. The invocation ``\fIvi -r\fP'' will not always list all saved files,
  1319. but they can be recovered even if they are not listed.
  1320. .NH 2
  1321. Continuous text input
  1322. .PP
  1323. When you are typing in large amounts of text it is convenient to have
  1324. lines broken near the right margin automatically.  You can cause this
  1325. to happen by giving the command
  1326. \fB:se wm=10\fR\s-2CR\s0.
  1327. This causes all lines to be broken at a space at least 10 columns
  1328. from the right hand edge of the screen.
  1329. .PP
  1330. If the editor breaks an input line and you wish to put it back together
  1331. you can tell it to join the lines with \fBJ\fR.  You can give \fBJ\fR
  1332. a count of the number of lines to be joined as in \fB3J\fR to join 3
  1333. lines.  The editor supplies white space, if appropriate,
  1334. at the juncture of the joined
  1335. lines, and leaves the cursor at this white space.
  1336. You can kill the white space with \fBx\fR if you don't want it.
  1337. .NH 2
  1338. Features for editing programs
  1339. .PP
  1340. The editor has a number of commands for editing programs.
  1341. The thing that most distinguishes editing of programs from editing of text
  1342. is the desirability of maintaining an indented structure to the body of
  1343. the program.  The editor has a
  1344. .I autoindent
  1345. facility for helping you generate correctly indented programs.
  1346. .PP
  1347. To enable this facility you can give the command \fB:se ai\fR\s-2CR\s0.
  1348. Now try opening a new line with \fBo\fR and type some characters on the
  1349. line after a few tabs.  If you now start another line, notice that the
  1350. editor supplies white space at the beginning of the line to line it up
  1351. with the previous line.  You cannot backspace over this indentation,
  1352. but you can use \fB^D\fR key to backtab over the supplied indentation.
  1353. .PP
  1354. Each time you type \fB^D\fR you back up one position, normally to an
  1355. 8 column boundary.  This amount is settable; the editor has an option
  1356. called
  1357. .I shiftwidth
  1358. which you can set to change this value.
  1359. Try giving the command \fB:se sw=4\fR\s-2CR\s0
  1360. and then experimenting with autoindent again.
  1361. .PP
  1362. For shifting lines in the program left and right, there are operators
  1363. .B <
  1364. and
  1365. .B >.
  1366. These shift the lines you specify right or left by one
  1367. .I shiftwidth.
  1368. Try
  1369. .B <<
  1370. and
  1371. .B >>
  1372. which shift one line left or right, and
  1373. .B <L
  1374. and
  1375. .B >L
  1376. shifting the rest of the display left and right.
  1377. .PP
  1378. If you have a complicated expression and wish to see how the parentheses
  1379. match, put the cursor at a left or right parenthesis and hit \fB%\fR.
  1380. This will show you the matching parenthesis.
  1381. This works also for braces { and }, and brackets [ and ].
  1382. .PP
  1383. If you are editing C programs, you can use the \fB[[\fR and \fB]]\fR keys
  1384. to advance or retreat to a line starting with a \fB{\fR, i.e. a function
  1385. declaration at a time.  When \fB]]\fR is used with an operator it stops
  1386. after a line which starts with \fB}\fR; this is sometimes useful with
  1387. \fBy]]\fR.
  1388. .NH 2
  1389. Filtering portions of the buffer
  1390. .PP
  1391. You can run system commands over portions of the buffer using the operator
  1392. \fB!\fR.
  1393. You can use this to sort lines in the buffer, or to reformat portions
  1394. of the buffer with a pretty-printer.
  1395. Try typing in a list of random words, one per line and ending them
  1396. with a blank line.  Back up to the beginning of the list, and then give
  1397. the command \fB!}sort\fR\s-2CR\s0.  This says to sort the next paragraph
  1398. of material, and the blank line ends a paragraph.
  1399. .NH 2
  1400. Commands for editing \s-2LISP\s0
  1401. .PP
  1402. If you are editing a \s-2LISP\s0 program you should set the option
  1403. .I lisp
  1404. by doing
  1405. \fB:se\ lisp\fR\s-2CR\s0.
  1406. This changes the \fB(\fR and \fB)\fR commands to move backward and forward
  1407. over s-expressions.
  1408. The \fB{\fR and \fB}\fR commands are like \fB(\fR and \fB)\fR but don't
  1409. stop at atoms.  These can be used to skip to the next list, or through
  1410. a comment quickly.
  1411. .PP
  1412. The
  1413. .I autoindent
  1414. option works differently for \s-2LISP\s0, supplying indent to align at
  1415. the first argument to the last open list.  If there is no such argument
  1416. then the indent is two spaces more than the last level.
  1417. .PP
  1418. There is another option which is useful for typing in \s-2LISP\s0, the
  1419. .I showmatch
  1420. option.
  1421. Try setting it with
  1422. \fB:se sm\fR\s-2CR\s0
  1423. and then try typing a `(' some words and then a `)'.  Notice that the
  1424. cursor shows the position of the `(' which matches the `)' briefly. 
  1425. This happens only if the matching `(' is on the screen, and the cursor
  1426. stays there for at most one second.
  1427. .PP
  1428. The editor also has an operator to realign existing lines as though they
  1429. had been typed in with
  1430. .I lisp
  1431. and
  1432. .I autoindent
  1433. set.  This is the \fB=\fR operator.
  1434. Try the command \fB=%\fR at the beginning of a function.  This will realign
  1435. all the lines of the function declaration.
  1436. .PP
  1437. When you are editing \s-2LISP\s0,, the \fB[[\fR and \fR]]\fR advance
  1438. and retreat to lines beginning with a \fB(\fR, and are useful for dealing
  1439. with entire function definitions.
  1440. .NH 2
  1441. Macros
  1442. .PP
  1443. .I Vi
  1444. has a parameterless macro facility, which lets you set it up so that
  1445. when you hit a single keystroke, the editor will act as though
  1446. you had hit some longer sequence of keys.  You can set this up if
  1447. you find yourself typing the same sequence of commands repeatedly.
  1448. .PP
  1449. Briefly, there are two flavors of macros:
  1450. .IP a)
  1451. Ones where you put the macro body in a buffer register, say \fIx\fR.
  1452. You can then type \fB@x\fR to invoke the macro.  The \fB@\fR may be followed
  1453. by another \fB@\fR to repeat the last macro.
  1454. .IP b)
  1455. You can use the
  1456. .I map
  1457. command from
  1458. .I vi
  1459. (typically in your
  1460. .I EXINIT )
  1461. with a command of the form:
  1462. .DS
  1463. :map \fIlhs\fR \fIrhs\fR\s-2CR\f0
  1464. .DE
  1465. mapping
  1466. .I lhs
  1467. into
  1468. .I rhs.
  1469. There are restrictions:
  1470. .I lhs
  1471. should be one keystroke (either 1 character or one function key)
  1472. since it must be entered within one second
  1473. (unless
  1474. .I notimeout
  1475. is set, in which case you can type it as slowly as you wish,
  1476. and
  1477. .I vi
  1478. will wait for you to finish it before it echoes anything).
  1479. The
  1480. .I lhs
  1481. can be no longer than 10 characters, the
  1482. .I rhs
  1483. no longer than 100.
  1484. To get a space, tab or newline into
  1485. .I lhs
  1486. or
  1487. .I rhs
  1488. you should escape them with a \fB^V\fR.
  1489. (It may be necessary to double the \fB^V\fR if the map
  1490. command is given inside
  1491. .I vi,
  1492. rather than in
  1493. .I ex.)
  1494. Spaces and tabs inside the
  1495. .I rhs
  1496. need not be escaped.
  1497. .PP
  1498. Thus to make the \fBq\fR key write and exit the editor, you can give
  1499. the command
  1500. .DS
  1501. :map q :wq\fB^V^V\fP\s-2CR CR\s0
  1502. .DE
  1503. which means that whenever you type \fBq\fR, it will be as though you
  1504. had typed the four characters \fB:wq\fR\s-2CR\s0.
  1505. A \fB^V\fR's is needed because without it the \s-2CR\s0 would end the
  1506. \fB:\fR command, rather than becoming part of the
  1507. .I map
  1508. definition.
  1509. There are two
  1510. .B ^V 's
  1511. because from within
  1512. .I vi ,
  1513. two
  1514. .B ^V 's
  1515. must be typed to get one.
  1516. The first \s-2CR\s0 is part of the
  1517. .I rhs ,
  1518. the second terminates the : command.
  1519. .PP
  1520. Macros can be deleted with
  1521. .DS
  1522. unmap lhs
  1523. .DE
  1524. .PP
  1525. If the
  1526. .I lhs
  1527. of a macro is ``#0'' through ``#9'', this maps the particular function key
  1528. instead of the 2 character ``#'' sequence.  So that terminals without
  1529. function keys can access such definitions, the form ``#x'' will mean function
  1530. key
  1531. .I x
  1532. on all terminals (and need not be typed within one second.)
  1533. The character ``#'' can be changed by using a macro in the usual way:
  1534. .DS
  1535. :map \fB^V^V^I\fP #
  1536. .DE
  1537. to use tab, for example.  (This won't affect the
  1538. .I map
  1539. command, which still uses
  1540. .B #,
  1541. but just the invocation from visual mode.
  1542. .PP
  1543. The undo command reverses an entire macro call as a unit,
  1544. if it made any changes.
  1545. .PP
  1546. Placing a `!' after the word
  1547. .B map
  1548. causes the mapping to apply
  1549. to input mode, rather than command mode.
  1550. Thus, to arrange for \fB^T\fP to be the same as 4 spaces in input mode,
  1551. you can type:
  1552. .DS
  1553. :map \fB^T\fP \fB^V\fP\o'b/'\o'b/'\o'b/'\o'b/'
  1554. .DE
  1555. where
  1556. .B \o'b/'
  1557. is a blank.
  1558. The \fB^V\fP is necessary to prevent the blanks from being taken as
  1559. white space between the
  1560. .I lhs
  1561. and
  1562. .I rhs .
  1563. .NH
  1564. Word Abbreviations
  1565. .PP
  1566. A feature similar to macros in input mode is word abbreviation.
  1567. This allows you to type a short word and have it expanded into
  1568. a longer word or words.
  1569. The commands are
  1570. .B :abbreviate
  1571. and
  1572. .B :unabbreviate
  1573. (\fB:ab\fP
  1574. and
  1575. .B :una )
  1576. and have the same syntax as
  1577. .B :map .
  1578. For example:
  1579. .DS
  1580. :ab eecs Electrical Engineering and Computer Sciences
  1581. .DE
  1582. causes the word `eecs' to always be changed into the
  1583. phrase `Electrical Engineering and Computer Sciences'.
  1584. Word abbreviation is different from macros in that
  1585. only whole words are affected.
  1586. If `eecs' were typed as part of a larger word, it would
  1587. be left alone.
  1588. Also, the partial word is echoed as it is typed.
  1589. There is no need for an abbreviation to be a single keystroke,
  1590. as it should be with a macro.
  1591. .NH 2
  1592. Abbreviations
  1593. .PP
  1594. The editor has a number of short
  1595. commands which abbreviate longer commands which we
  1596. have introduced here.  You can find these commands easily
  1597. on the quick reference card.
  1598. They often save a bit of typing and you can learn them as convenient.
  1599. .NH 1
  1600. Nitty-gritty details
  1601. .NH 2
  1602. Line representation in the display
  1603. .PP
  1604. The editor folds long logical lines onto many physical lines in the display.
  1605. Commands which advance lines advance logical lines and will skip
  1606. over all the segments of a line in one motion.  The command \fB|\fR moves
  1607. the cursor to a specific column, and may be useful for getting near the
  1608. middle of a long line to split it in half.  Try \fB80|\fR on a line which
  1609. is more than 80 columns long.\*(dg
  1610. .FS
  1611. \*(dg You can make long lines very easily by using \fBJ\fR to join together
  1612. short lines.
  1613. .FE
  1614. .PP
  1615. The editor only puts full lines on the display; if there is not enough
  1616. room on the display to fit a logical line, the editor leaves the physical
  1617. line empty, placing only an @ on the line as a place holder.  When you
  1618. delete lines on a dumb terminal, the editor will often just clear the
  1619. lines to @ to save time (rather than rewriting the rest of the screen.)
  1620. You can always maximize the information on the screen by giving the \fB^R\fR
  1621. command.
  1622. .PP
  1623. If you wish, you can have the editor place line numbers before each line
  1624. on the display.  Give the command \fB:se nu\fR\s-2CR\s0 to enable
  1625. this, and the command \fB:se nonu\fR\s-2CR\s0 to turn it off.
  1626. You can have tabs represented as \fB^I\fR and the ends of lines indicated
  1627. with `$' by giving the command \fB:se list\fR\s-2CR\s0;
  1628. \fB:se nolist\fR\s-2CR\s0 turns this off.
  1629. .PP
  1630. Finally, lines consisting of only the character `~' are displayed when
  1631. the last line in the file is in the middle of the screen.  These represent
  1632. physical lines which are past the logical end of file.
  1633. .NH 2
  1634. Counts
  1635. .PP
  1636. Most
  1637. .I vi
  1638. commands will use a preceding count to affect their behavior in some way.
  1639. The following table gives the common ways in which the counts are used:
  1640. .DS
  1641. .TS
  1642. l lb.
  1643. new window size    :  /  ?  [[  ]]  \`  \'
  1644. scroll amount    ^D  ^U
  1645. line/column number    z  G  |
  1646. repeat effect    \fRmost of the rest\fP
  1647. .TE
  1648. .DE
  1649. .PP
  1650. The editor maintains a notion of the current default window size.
  1651. On terminals which run at speeds greater than 1200 baud
  1652. the editor uses the full terminal screen.
  1653. On terminals which are slower than 1200 baud
  1654. (most dialup lines are in this group)
  1655. the editor uses 8 lines as the default window size.
  1656. At 1200 baud the default is 16 lines.
  1657. .PP
  1658. This size is the size used when the editor clears and refills the screen
  1659. after a search or other motion moves far from the edge of the current window.
  1660. The commands which take a new window size as count all often cause the
  1661. screen to be redrawn.  If you anticipate this, but do not need as large
  1662. a window as you are currently using, you may wish to change the screen
  1663. size by specifying the new size before these commands.
  1664. In any case, the number of lines used on the screen will expand if you
  1665. move off the top with a \fB\-\fR or similar command or off the bottom
  1666. with a command such as \s-2RETURN\s0 or \fB^D\fR.
  1667. The window will revert to the last specified size the next time it is
  1668. cleared and refilled.\*(dg
  1669. .FS
  1670. \*(dg But not by a \fB^L\fR which just redraws the screen as it is.
  1671. .FE
  1672. .PP
  1673. The scroll commands \fB^D\fR and \fB^U\fR likewise remember the amount
  1674. of scroll last specified, using half the basic window size initially.
  1675. The simple insert commands use a count to specify a repetition of the
  1676. inserted text.  Thus \fB10a+\-\-\-\-\fR\s-2ESC\s0 will insert a grid-like
  1677. string of text.
  1678. A few commands also use a preceding count as a line or column number.
  1679. .PP
  1680. Except for a few commands which ignore any counts (such as \fB^R\fR),
  1681. the rest of the editor commands use a count to indicate a simple repetition
  1682. of their effect.  Thus \fB5w\fR advances five words on the current line,
  1683. while \fB5\fR\s-2RETURN\s0 advances five lines.  A very useful instance
  1684. of a count as a repetition is a count given to the \fB.\fR command, which
  1685. repeats the last changing command.  If you do \fBdw\fR and then \fB3.\fR,
  1686. you will delete first one and then three words.  You can then delete
  1687. two more words with \fB2.\fR.
  1688. .NH 2
  1689. More file manipulation commands
  1690. .PP
  1691. The following table lists the file manipulation commands which you can
  1692. use when you are in
  1693. .I vi.
  1694. .KF
  1695. .DS
  1696. .TS
  1697. lb l.
  1698. :w    write back changes
  1699. :wq    write and quit
  1700. :x    write (if necessary) and quit (same as ZZ).
  1701. :e \fIname\fP    edit file \fIname\fR
  1702. :e!    reedit, discarding changes
  1703. :e + \fIname\fP    edit, starting at end
  1704. :e +\fIn\fP    edit, starting at line \fIn\fP
  1705. :e #    edit alternate file
  1706. :w \fIname\fP    write file \fIname\fP
  1707. :w! \fIname\fP    overwrite file \fIname\fP
  1708. :\fIx,y\fPw \fIname\fP    write lines \fIx\fP through \fIy\fP to \fIname\fP
  1709. :r \fIname\fP    read file \fIname\fP into buffer
  1710. :r !\fIcmd\fP    read output of \fIcmd\fP into buffer
  1711. :n    edit next file in argument list
  1712. :n!    edit next file, discarding changes to current
  1713. :n \fIargs\fP    specify new argument list
  1714. :ta \fItag\fP    edit file containing tag \fItag\fP, at \fItag\fP
  1715. .TE
  1716. .DE
  1717. .KE
  1718. All of these commands are followed by a \s-2CR\s0 or \s-2ESC\s0.
  1719. The most basic commands are \fB:w\fR and \fB:e\fR.
  1720. A normal editing session on a single file will end with a \fBZZ\fR command.
  1721. If you are editing for a long period of time you can give \fB:w\fR commands
  1722. occasionally after major amounts of editing, and then finish
  1723. with a \fBZZ\fR.   When you edit more than one file, you can finish
  1724. with one with a \fB:w\fR and start editing a new file by giving a \fB:e\fR
  1725. command,
  1726. or set
  1727. .I autowrite
  1728. and use \fB:n\fP <file>.
  1729. .PP
  1730. If you make changes to the editor's copy of a file, but do not wish to
  1731. write them back, then you must give an \fB!\fR after the command you
  1732. would otherwise use; this forces the editor to discard any changes
  1733. you have made.  Use this carefully.
  1734. .PP
  1735. The \fB:e\fR command can be given a \fB+\fR argument to start at the
  1736. end of the file, or a \fB+\fR\fIn\fR argument to start at line \fIn\fR\^.
  1737. In actuality, \fIn\fR may be any editor command not containing a space,
  1738. usefully a scan like \fB+/\fIpat\fR or \fB+?\fIpat\fR.
  1739. In forming new names to the \fBe\fR command, you can use the character
  1740. \fB%\fR which is replaced by the current file name, or the character
  1741. \fB#\fR which is replaced by the alternate file name.
  1742. The alternate file name is generally the last name you typed other than
  1743. the current file.  Thus if you try to do a \fB:e\fR and get a diagnostic
  1744. that you haven't written the file, you can give a \fB:w\fR command and
  1745. then a \fB:e #\fR command to redo the previous \fB:e\fR.
  1746. .PP
  1747. You can write part of the buffer to a file by finding out the lines
  1748. that bound the range to be written using \fB^G\fR, and giving these
  1749. numbers after the \fB:\fR
  1750. and before the \fBw\fP, separated by \fB,\fR's.
  1751. You can also mark these lines with \fBm\fR and
  1752. then use an address of the form \fB\(aa\fR\fIx\fR\fB,\fB\(aa\fR\fIy\fR
  1753. on the \fBw\fR command here.
  1754. .PP
  1755. You can read another file into the buffer after the current line by using
  1756. the \fB:r\fR command.
  1757. You can similarly read in the output from a command, just use \fB!\fR\fIcmd\fR
  1758. instead of a file name.
  1759. .PP
  1760. If you wish to edit a set of files in succession, you can give all the
  1761. names on the command line, and then edit each one in turn using the command
  1762. \fB:n\fR.  It is also possible to respecify the list of files to be edited
  1763. by giving the \fB:n\fR command a list of file names, or a pattern to
  1764. be expanded as you would have given it on the initial
  1765. .I vi
  1766. command.
  1767. .PP
  1768. If you are editing large programs, you will find the \fB:ta\fR command
  1769. very useful.  It utilizes a data base of function names and their locations,
  1770. which can be created by programs such as
  1771. .I ctags,
  1772. to quickly find a function whose name you give.
  1773. If the \fB:ta\fR command will require the editor to switch files, then
  1774. you must \fB:w\fR or abandon any changes before switching.  You can repeat
  1775. the \fB:ta\fR command without any arguments to look for the same tag
  1776. again.
  1777. .NH 2
  1778. More about searching for strings
  1779. .PP
  1780. When you are searching for strings in the file with \fB/\fR and \fB?\fR,
  1781. the editor normally places you at the next or previous occurrence
  1782. of the string.  If you are using an operator such as \fBd\fR,
  1783. \fBc\fR or \fBy\fR, then you may well wish to affect lines up to the
  1784. line before the line containing the pattern.  You can give a search of
  1785. the form \fB/\fR\fIpat\fR\fB/\-\fR\fIn\fR to refer to the \fIn\fR'th line
  1786. before the next line containing \fIpat\fR, or you can use \fB\+\fR instead
  1787. of \fB\-\fR to refer to the lines after the one containing \fIpat\fR.
  1788. If you don't give a line offset, then the editor will affect characters
  1789. up to the match place, rather than whole lines; thus use ``+0'' to affect
  1790. to the line which matches.
  1791. .PP
  1792. You can have the editor ignore the case of words in the searches it does
  1793. by giving the command \fB:se ic\fR\s-2CR\s0.
  1794. The command \fB:se noic\fR\s-2CR\s0 turns this off.
  1795. .PP
  1796. Strings given to searches may actually be regular expressions.
  1797. If you do not want or need this facility, you should
  1798. .DS
  1799. set nomagic
  1800. .DE
  1801. in your EXINIT.
  1802. In this case, 
  1803. only the characters \fB\(ua\fR and \fB$\fR are special in patterns.
  1804. The character \fB\e\fR is also then special (as it is most everywhere in
  1805. the system), and may be used to get at the
  1806. an extended pattern matching facility.
  1807. It is also necessary to use a \e before a
  1808. \fB/\fR in a forward scan or a \fB?\fR in a backward scan, in any case.
  1809. The following table gives the extended forms when \fBmagic\fR is set.
  1810. .DS
  1811. .TS
  1812. bl l.
  1813. \(ua    at beginning of pattern, matches beginning of line
  1814. $    at end of pattern, matches end of line
  1815. \fB\&.\fR    matches any character
  1816. \e<    matches the beginning of a word
  1817. \e>    matches the end of a word
  1818. [\fIstr\fP]    matches any single character in \fIstr\fP
  1819. [\(ua\fIstr\fP]    matches any single character not in \fIstr\fP
  1820. [\fIx\fP\-\fIy\fP]    matches any character between \fIx\fP and \fIy\fP
  1821. *    matches any number of the preceding pattern
  1822. .TE
  1823. .DE
  1824. If you use \fBnomagic\fR mode, then
  1825. the \fB. [\fR and \fB*\fR primitives are given with a preceding
  1826. \e.
  1827. .NH 2
  1828. More about input mode
  1829. .PP
  1830. There are a number of characters which you can use to make corrections
  1831. during input mode.  These are summarized in the following table.
  1832. .DS
  1833. .TS
  1834. lb l.
  1835. ^H    deletes the last input character
  1836. ^W    deletes the last input word, defined as by \fBb\fR
  1837. erase    your erase character, same as \fB^H\fP
  1838. kill    your kill character, deletes the input on this line
  1839. \e    escapes a following \fB^H\fP and your erase and kill
  1840. \s-2ESC\s0    ends an insertion
  1841. \s-2DEL\s0    interrupts an insertion, terminating it abnormally
  1842. \s-2CR\s0    starts a new line
  1843. ^D    backtabs over \fIautoindent\fP
  1844. 0^D    kills all the \fIautoindent\fP
  1845. \(ua^D    same as \fB0^D\fP, but restores indent next line
  1846. ^V    quotes the next non-printing character into the file
  1847. .TE
  1848. .DE
  1849. .PP
  1850. The most usual way of making corrections to input is by typing \fB^H\fR
  1851. to correct a single character, or by typing one or more \fB^W\fR's to
  1852. back over incorrect words.  If you use \fB#\fR as your erase character
  1853. in the normal system, it will work like \fB^H\fR.
  1854. .PP
  1855. Your system kill character, normally \fB@\fR, \fB^X\fP or \fB^U\fR,
  1856. will erase all
  1857. the input you have given on the current line.
  1858. In general, you can neither
  1859. erase input back around a line boundary nor can you erase characters
  1860. which you did not insert with this insertion command.  To make corrections
  1861. on the previous line after a new line has been started you can hit \s-2ESC\s0
  1862. to end the insertion, move over and make the correction, and then return
  1863. to where you were to continue.  The command \fBA\fR which appends at the
  1864. end of the current line is often useful for continuing.
  1865. .PP
  1866. If you wish to type in your erase or kill character (say # or @) then
  1867. you must precede it with a \fB\e\fR, just as you would do at the normal
  1868. system command level.  A more general way of typing non-printing characters
  1869. into the file is to precede them with a \fB^V\fR.  The \fB^V\fR echoes
  1870. as a \fB\(ua\fR character on which the cursor rests.  This indicates that
  1871. the editor expects you to type a control character.  In fact you may
  1872. type any character and it will be inserted into the file at that point.*
  1873. .FS
  1874. * This is not quite true.  The implementation of the editor does
  1875. not allow the \s-2NULL\s0 (\fB^@\fR) character to appear in files.  Also
  1876. the \s-2LF\s0 (linefeed or \fB^J\fR) character is used by the editor
  1877. to separate lines in the file, so it cannot appear in the middle of a
  1878. line.  You can insert any other character, however, if you wait for the
  1879. editor to echo the \fB\(ua\fR before you type the character.  In fact,
  1880. the editor will treat a following letter as a request for the corresponding
  1881. control character.  This is the only way to type \fB^S\fR or \fB^Q\fP,
  1882. since the system normally uses them to suspend and resume output
  1883. and never gives them to the editor to process.
  1884. .FE
  1885. .PP
  1886. If you are using \fIautoindent\fR you can backtab over the indent which
  1887. it supplies by typing a \fB^D\fR.  This backs up to a \fIshiftwidth\fR
  1888. boundary.
  1889. This only works immediately after the supplied \fIautoindent\fR.
  1890. .PP
  1891. When you are using \fIautoindent\fR you may wish to place a label at
  1892. the left margin of a line.  The way to do this easily is to type \fB\(ua\fR
  1893. and then \fB^D\fR.  The editor will move the cursor to the left margin
  1894. for one line, and restore the previous indent on the next.  You can also
  1895. type a \fB0\fR followed immediately by a \fB^D\fR if you wish to kill
  1896. all the indent and not have it come back on the next line.
  1897. .NH 2
  1898. Upper case only terminals
  1899. .PP
  1900. If your terminal has only upper case, you can still use
  1901. .I vi
  1902. by using the normal
  1903. system convention for typing on such a terminal.
  1904. Characters which you normally type are converted to lower case, and you
  1905. can type upper case letters by preceding them with a \e.
  1906. The characters { ~ } | \(ga are not available on such terminals, but you
  1907. can escape them as \e( \e\(ua \e) \e! \e\(aa.
  1908. These characters are represented on the display in the same way they
  1909. are typed.\*(dd
  1910. .FS
  1911. \*(dd The \e character you give will not echo until you type another
  1912. key.
  1913. .FE
  1914. .NH 2
  1915. Vi and ex
  1916. .PP
  1917. .I Vi
  1918. is actually one mode of editing within the editor
  1919. .I ex.
  1920. When you are running
  1921. .I vi
  1922. you can escape to the line oriented editor of
  1923. .I ex
  1924. by giving the command
  1925. \fBQ\fR.
  1926. All of the
  1927. .B :
  1928. commands which were introduced above are available in
  1929. .I ex.
  1930. Likewise, most
  1931. .I ex
  1932. commands can be invoked from
  1933. .I vi
  1934. using :.
  1935. Just give them without the \fB:\fR and follow them with a \s-2CR\s0.
  1936. .PP
  1937. In rare instances, an internal error may occur in
  1938. .I vi.
  1939. In this case you will get a diagnostic and be left in the command mode of
  1940. .I ex.
  1941. You can then save your work and quit if you wish by giving a command
  1942. \fBx\fR after the \fB:\fR which \fIex\fR prompts you with, or you can
  1943. reenter \fIvi\fR by giving
  1944. .I ex
  1945. a
  1946. .I vi
  1947. command.
  1948. .PP
  1949. There are a number of things which you can do more easily in
  1950. .I ex
  1951. than in
  1952. .I vi.
  1953. Systematic changes in line oriented material are particularly easy.
  1954. You can read the advanced editing documents for the editor
  1955. .I ed
  1956. to find out a lot more about this style of editing.
  1957. Experienced
  1958. users often mix their use of
  1959. .I ex
  1960. command mode and
  1961. .I vi
  1962. command mode to speed the work they are doing.
  1963. .NH 2
  1964. Open mode: vi on hardcopy terminals and ``glass tty's''
  1965. \(dd
  1966. .PP
  1967. If you are on a hardcopy terminal or a terminal which does not have a cursor
  1968. which can move off the bottom line, you can still use the command set of
  1969. .I vi,
  1970. but in a different mode.
  1971. When you give a
  1972. .I vi
  1973. command, the editor will tell you that it is using
  1974. .I open
  1975. mode.
  1976. This name comes from the
  1977. .I open
  1978. command in
  1979. .I ex,
  1980. which is used to get into the same mode.
  1981. .PP
  1982. The only difference between
  1983. .I visual
  1984. mode
  1985. and
  1986. .I open
  1987. mode is the way in which the text is displayed.
  1988. .PP
  1989. In
  1990. .I open
  1991. mode the editor uses a single line window into the file, and moving backward
  1992. and forward in the file causes new lines to be displayed, always below the
  1993. current line.
  1994. Two commands of
  1995. .I vi
  1996. work differently in
  1997. .I open:
  1998. .B z
  1999. and
  2000. \fB^R\fR.
  2001. The
  2002. .B z
  2003. command does not take parameters, but rather draws a window of context around
  2004. the current line and then returns you to the current line.
  2005. .PP
  2006. If you are on a hardcopy terminal,
  2007. the
  2008. .B ^R
  2009. command will retype the current line.
  2010. On such terminals, the editor normally uses two lines to represent the
  2011. current line.
  2012. The first line is a copy of the line as you started to edit it, and you work
  2013. on the line below this line.
  2014. When you delete characters, the editor types a number of \e's to show
  2015. you the characters which are deleted.  The editor also reprints the current
  2016. line soon after such changes so that you can see what the line looks
  2017. like again.
  2018. .PP
  2019. It is sometimes useful to use this mode on very slow terminals which
  2020. can support
  2021. .I vi
  2022. in the full screen mode.
  2023. You can do this by entering
  2024. .I ex
  2025. and using an
  2026. .I open
  2027. command.
  2028. .LP
  2029. .SH
  2030. Acknowledgements
  2031. .PP
  2032. Bruce Englar encouraged the early development of this display editor.
  2033. Peter Kessler helped bring sanity to version 2's command layout.
  2034. Bill Joy wrote versions 1 and 2.0 through 2.7,
  2035. and created the framework that users see in the present editor.
  2036. Mark Horton added macros and other features and made the
  2037. editor work on a large number of terminals and Unix systems.
  2038.