home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / riscbsd / datafile / usd / 15_vi / vi_in_txt < prev    next >
Encoding:
Text File  |  1996-10-12  |  76.9 KB  |  2,179 lines

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