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