Contents of /xml/htdocs/doc/en/vi-guide.xml

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1.11 - (hide annotations) (download) (as text)
Tue May 18 07:03:00 2004 UTC (14 years, 9 months ago) by cam
Branch: MAIN
Changes since 1.10: +40 -29 lines
File MIME type: application/xml
#51292 Default editor instructions + some style added

1 swift 1.8 <?xml version="1.0" encoding="UTF-8"?>
2 cam 1.11 <!-- $Header: /home/cvsroot/gentoo/xml/htdocs/doc/en/vi-guide.xml,v 1.10 2003/12/30 15:23:55 aaby Exp $ -->
3 swift 1.8 <!DOCTYPE guide SYSTEM "/dtd/guide.dtd">
5 cam 1.11 <guide link="/doc/en/vi-guide.xml">
6 drobbins 1.1 <title>Learning vi -- the "cheatsheet" technique</title>
7 aaby 1.10 <author title="Author">
8     <mail link="drobbins@gentoo.org">Daniel Robbins</mail>
9     </author>
10     <author title="Author">
11     <mail link="stocke2@gentoo.org">Eric Stockbridge</mail>
12     </author>
13     <author title="Editor">
14     <mail link="bennyc@gentoo.org">Benny Chuang</mail>
15     </author>
17     <abstract>
18     This guide will teach you how to use vi, using a cheat sheet method to
19     accelerate the learning process. This will be the first guide for vi,
20     catering to beginners.
21     </abstract>
23 cam 1.11 <version>1.1.4</version>
24     <date>May 18, 2004</date>
25 drobbins 1.1
26     <chapter>
27     <title>Getting Started</title>
28     <section>
29     <title>Introduction</title>
30     <body>
31 aaby 1.10
32 drobbins 1.1 <p>
33 aaby 1.10 This tutorial will show you how to use vi, a powerful visual editor.
34 cam 1.11 Using a special accelerated <e>cheat sheet</e> method, this tutorial is
35 aaby 1.10 designed to make you a proficient vi user without requiring a huge
36     time commitment. In this vi tutorial, you'll learn how to move
37     around, edit text, use insert mode, copy and paste text, and use
38     important vim extensions like visual mode and multi-window editing.
39 vapier 1.5 </p>
40 aaby 1.10
41 vapier 1.5 <p>
42 aaby 1.10 If you either don't know or aren't comfortable using vi, then you owe
43     it to yourself to take this tutorial and get up to speed with one of
44     the most popular and powerful Linux/UNIX visual editing programs.
45 vapier 1.5 </p>
46 cam 1.11
47 drobbins 1.1 </body>
48     </section>
49     <section>
50     <title>About the guide</title>
51     <body>
52 aaby 1.10
53 drobbins 1.1 <p>
54 aaby 1.10 There's one thing in particular that makes it difficult to learn vi --
55     vi has lots of commands. In order to use vi effectively, you need to
56     memorize quite a few. This can take a long time, and one of the goals
57     of this tutorial is not to take up a lot of your time. So, initially,
58     we have a challenge -- how exactly do I help you to memorize lots of
59     commands in a short period of time?
60 drobbins 1.1 </p>
61 aaby 1.10
62 drobbins 1.1 <p>
63 aaby 1.10 To tackle this challenge, as we proceed through this tutorial, we're
64 cam 1.11 going to gradually put together a vi cheat sheet. This sheet will
65 aaby 1.10 contain all the important vi commands. After you've completed this
66     tutorial, you'll be able to refer to this cheat sheet if you forget a
67     particular command. Over time, as you memorize commands, you'll
68     gradually become less and less dependent on the cheat sheet. By using
69     the cheat-sheet technique, you'll be able to learn how to use vi
70     faster than ever possible before!
71 drobbins 1.1 </p>
72 aaby 1.10
73 drobbins 1.1 </body>
74     </section>
75     <section>
76     <title>The learning process</title>
77     <body>
79 aaby 1.10 <p>
80     In this guide, I'm going to use several techniques to help you
81     learn. First, I'm going to describe how a particular command works,
82     as you'd expect. Then, I'm going to ask you to try to use the command
83     in vi (for practice), and then I'm going to ask you to transcribe the
84     command to the cheat sheet (for later reference.) If you want to
85     learn vi quickly, it's important that you perform all these steps.
86     Trying out a command in vi and transcribing the command onto your
87     cheat sheet will help you to memorize the command.
88 drobbins 1.1 </p>
89 aaby 1.10
90 drobbins 1.1 </body>
91     </section>
92     <section>
93     <title>Introducing vim</title>
94     <body>
96 aaby 1.10 <p>
97     There are many versions of vi, and I'm going to be showing you how to
98 cam 1.11 use a version of vi called <c>vim</c>. vim is very popular and has a
99 aaby 1.10 number of extensions that make vi a lot nicer (whenever I demonstrate
100     a vim-specific command, I'll make a note of it.) If you need to
101     install vim, you can get it from <uri
102     link="http://www.vim.org/">www.vim.org</uri>. In addition to an
103 cam 1.11 enhanced command-line vi, vim also comes with <c>gvim</c>, a nice graphical
104 aaby 1.10 editor which can be configured to use the excellent GTK+ gui library.
105     Here's a gvim screenshot from my system:
106     </p>
107 drobbins 1.1
108 aaby 1.10 <figure link="http://www.ibiblio.org/web-gentoo/images/vishot.png"
109     short="screenshot" caption="VIM screenshot"/>
110 drobbins 1.1
111 aaby 1.10 <p>
112     If you're a vi newbie, try to get gvim running on your system. Using
113     vi from a gui can make things a bit easier for beginners.
114     </p>
115 drobbins 1.1
116     </body>
117     </section>
118     </chapter>
119 aaby 1.10
120 drobbins 1.1 <chapter>
121     <title>First Steps</title>
122     <section>
123     <title>Pick a file</title>
124     <body>
125 aaby 1.10
126     <p>
127     Before using vi to edit files, you need to know how to use vi to move
128     around in a file. vi has a lot of movement commands, and we're going
129     to take a look at many of them. For this part of the tutorial, find
130     an unimportant text file and load it into vi by typing:
131     </p>
133     <pre caption="Loading a file into vi">
134 cam 1.11 $ <i>vi myfile.txt</i>
135 aaby 1.10 </pre>
137     <p>
138 cam 1.11 If you have vim installed, type <c>vim myfile.txt</c>. If you'd prefer to
139     use gvim, type <c>gvim myfile.txt</c>. <path>myfile.txt</path> should be the name of a
140 aaby 1.10 text file on your system.
141 drobbins 1.1 </p>
142 aaby 1.10
143 drobbins 1.1 </body>
144     </section>
145     <section>
146     <title>Inside vi</title>
147     <body>
149 aaby 1.10 <p>
150     After vi loads, you should see a part of the text file you loaded
151     on your screen. Congratulations -- you're in vi! Unlike many
152 cam 1.11 editors, when vi starts up, it is in a special mode called <e>command
153     mode</e>. This means that if you press <c>l</c>on the keyboard, instead of
154     inserting an <c>l</c> into the file at the current cursor position, the
155 aaby 1.10 cursor will move one character to the right instead. In command mode,
156     the characters on your keyboard are used to send commands to vi rather
157     than insert literal characters into the text. One of the most
158     essential types of commands are movement commands; let's take a look
159     at some.
160     </p>
161 drobbins 1.1
162     </body>
163     </section>
164     </chapter>
165 aaby 1.10
166 drobbins 1.1 <chapter>
167     <title>Moving around</title>
168     <section>
169     <title>Moving in vi, part 1</title>
170     <body>
172 aaby 1.10 <p>
173     When in command mode, you can use the <c>h</c>,<c>j</c>,<c>k</c> and
174     <c>l</c> keys to move the cursor left, down, up and right
175     respectively. If you're using a modern version of vi, you can also
176     use the arrow keys for this purpose. The <c>h</c>,<c>j</c>,<c>k</c>
177     and <c>l</c> keys are handy because once you're comfortable with them,
178     you'll be able to move around in the file without moving your fingers
179     from the home keyboard row. Try using <c>h</c>,<c>j</c>,<c>k</c> and
180     <c>l</c> (and the arrow keys) to move around in the text file. Try
181     using <c>h</c> until you get to the beginning of a line. Notice that
182     vi doesn't allow you to "wrap around" to the previous line by hitting
183     <c>h</c> while you're on the first character. Likewise, you can't
184     "wrap around" to the next line by hitting <c>l</c> at the end of a
185     line.
186 drobbins 1.1 </p>
188     </body>
189     </section>
190     <section>
191     <title>Moving in vi, part 2</title>
192     <body>
194 aaby 1.10 <p>
195     vi offers special shortcuts for jumping to the beginning or end of the
196     current line. You can press <c>0</c> (zero) to jump to the first
197     character of a line, and <c>$</c> to jump to the last character of the
198     line. Try 'em and see. Since vi has so many handy movement commands,
199     it makes a great "pager" (like the more or less commands.) Using vi
200     as a pager will also help you to learn all the movement commands very
201     quickly.
202     </p>
203 drobbins 1.1
204 aaby 1.10 <p>
205     You can also use <c>&lt;CTR&gt;F</c> and <c>&lt;CTR&gt;B</c> to move
206     forwards and backwards a page at a time. Modern versions of vi (like
207 cam 1.11 vim) will also allow you to use the <c>PGUP</c> and <c>PGDOWN</c> keys for this
208 aaby 1.10 purpose.
209 drobbins 1.1 </p>
211     </body>
212     </section>
213     <section>
214     <title>Word moves, part 1</title>
215     <body>
216 aaby 1.10
217     <p>
218     vi also allows you to move to the left or right by word increments.
219     To move to the <e>first</e> character of the next word, press
220     <c>w</c>. To move to the <e>last</e> character of the next word,
221     press <c>e</c>. To move to the first character of the <e>previous</e>
222     word, press <c>b</c>. Test 'em out.
223     </p>
225 drobbins 1.1 </body>
226     </section>
227     <section>
228     <title>Word moves, part 2</title>
229     <body>
230 aaby 1.10
231     <p>
232     After playing around with the word movement commands, you may have
233 cam 1.11 noticed that vi considers words like <c>foo-bar-oni</c> as five separate
234 aaby 1.10 words! This is because by default, vi delimits words by spaces
235 cam 1.11 <e>or</e> punctuation. <c>foo-bar-oni</c> is therefore considered five
236     words: <c>foo</c>, <c>-</c>, <c>bar</c>, <c>-</c> and <c>oni</c>.
237 aaby 1.10 </p>
239     <p>
240     Sometimes, this is what you want, and sometimes it isn't.
241     Fortunately, vi also understands the concept of a "bigword". vi
242     delimits bigwords by <e>spaces or newlines only</e>. This means that
243 cam 1.11 while <c>foo-bar-oni</c> is considered five vi words, it's considered only
244 aaby 1.10 one vi bigword.
245     </p>
246 drobbins 1.1
247     </body>
248     </section>
249     <section>
250     <title>Word moves, part 3</title>
251     <body>
252 aaby 1.10
253     <p>
254     To jump around to the next and previous bigword, you can use a
255     <e>capitalized</e> word move command. Use <c>W</c> to jump to the
256     first character of the next bigword, <c>E</c> to jump to the last
257     character of the next bigword, and <c>B</c> to jump to the first
258     character of the previous bigword. Test 'em out, and compare the
259     matching word and bigword movement commands until you understand their
260     differences.
261 drobbins 1.1 </p>
263     </body>
264     </section>
265     <section>
266     <title>Bigger moves</title>
267     <body>
268 aaby 1.10
269     <p>
270     We just have a few more commands to cover before it's time to start
271     puting together our cheat sheet. You can use the <c>(</c> and
272     <c>)</c> characters to move to the beginning of the previous and next
273     sentence. In addition, you can hit <c>{</c> or <c>}</c> to jump to
274     the beginning of the current paragraph, and the beginning of the next.
275     Test 'em out.
276     </p>
277 drobbins 1.1
278     </body>
279     </section>
280     </chapter>
281 aaby 1.10
282 drobbins 1.1 <chapter>
283 aaby 1.10 <title>Quitting</title>
284 drobbins 1.1 <section>
285     <body>
286 aaby 1.10
287     <p>
288     We've covered the basic movement commands, but there are another
289     couple of commands that you need to know. Typing <c>:q</c> will quit
290     vi. If this doesn't work, then you probably accidentally modified the
291     file in some way. To tell vi to quit, throwing away any changes, type
292     <c>:q!</c>. You should now be at the command prompt.
293     </p>
295     <p>
296 cam 1.11 In vi, any command that begins with a <c>:</c> is said to be an
297 aaby 1.10 <e>ex-mode</e> command. This is because vi has a built-in non-visual
298     editor called <e>ex</e>. It can be used similarly to sed to perform
299     line-based editing operations. In addition, it can also be used to
300     quit, as we've just seen. If you ever hit the <c>Q</c> key while in
301     command mode, you'll be transported to ex mode. If this ever happens
302     to you , you'll be confronted with a : prompt, and hitting enter will
303     scroll the entire screen upwards. To get back to good 'ol vi mode,
304     simply type vi and hit enter.
305     </p>
306 drobbins 1.1
307     </body>
308     </section>
309     </chapter>
310 aaby 1.10
311 drobbins 1.1 <chapter>
312     <title>The Cheat Sheet</title>
313     <section>
314     <title>The beginnings of the cheat sheet</title>
315     <body>
317 aaby 1.10 <p>
318     We've covered a lot of commands, and it's time to transcribe them to
319     our cheat sheet. For the cheat sheet, you'll need a US letter or A4
320     sized piece of paper (we're going to pack a lot of info onto this
321     sheet!) Here's a picture of my cheat sheet after I've transcribed all
322     the commands that we've covered so far. Try to follow my layout if
323     possible so that we can fit everything on one sheet.
324     </p>
326     <figure
327     link="http://www.ibiblio.org/web-gentoo/images/vicheat-first.png"
328     short="First part of the cheat sheet" caption="Cheat Sheet"/>
329 drobbins 1.1
330     </body>
331     </section>
332     <section>
333     <title>Miscellaneous vi</title>
334 aaby 1.10 <body>
336     <p>
337     Let's continue our rapid command-covering pace. In command-mode, you
338     can jump to a particularline by typing <c>G</c>. To jump to the first
339     line of a file, type <c>1G</c>. Note that <c>G</c> is capitalized.
340     </p>
341 drobbins 1.1
342 aaby 1.10 <p>
343     If you want to jump to the next occurence of a particular text
344     pattern, type <c>/&lt;regexp&gt;</c> and hit <c>enter</c>. Replace
345     &lt;regexp&gt; with the regular expression you're looking for. If you
346     don't know how to use regular expressions, don't fret -- typing
347     <c>/foo</c> will move to the next occurence of <e>foo</e>. The only
348     thing you'll need to watch out for is when you want to refer to the
349     literal <c>^</c>, <c>.</c>, <c>$</c> or <c>\</c> characters. Prefix
350     these characters with a backslash (<c>\</c>), and you'll be set. For
351     example, <c>/foo\.gif</c> will search for the next occurence of
352     "foo.gif".
353     </p>
355     <p>
356     To repeat the search forwards, hit <c>n</c>. To repeat the search
357     backwards, type <c>N</c>. As always, test these commands out in your
358     very own vi editor. You can also type <c>//</c> to repeat the last
359     search.
360     </p>
361 drobbins 1.1
362     </body>
363     </section>
364     </chapter>
365 aaby 1.10
366 drobbins 1.1 <chapter>
367     <title>Saving and Editing</title>
368     <section>
369     <title>Save and save as...</title>
370     <body>
371 aaby 1.10
372     <p>
373     We've covered how you can use the <e>ex</e> command <c>:q</c> to quit
374     from vi. If you want to save your changes, type <c>:w</c>. If you
375     want to save your changes to another file, type <c>:w filename.txt</c>
376     to save as <e>filename.txt</e>. If you want to save and quit, type
377     <c>:x</c> or <c>:wq</c>.
378     </p>
380     <p>
381     In vim (and other advanced vi editors, like elvis)<c>:w</c>, you can
382     have multiple buffers open at once. To open a file into a new window,
383 cam 1.11 type <c>:sp filename.txt</c>. <path>filename.txt</path> will appear open
384 aaby 1.10 for editing in a new split window. To switch between windows, type
385     <c>&lt;CTR&gt;w&lt;CTR&gt;w</c> (control-w twice). Any <c>:q</c>,
386     <c>:q!</c>, <c>:w</c> and <c>:x</c> commands that you enter will only
387     be applied to the currently-active window.
388     </p>
390 drobbins 1.1 </body>
391     </section>
392     <section>
393     <title>Simple edits</title>
394     <body>
395 aaby 1.10
396     <p>
397     Now, it's time to start learning some of the simple editing commands.
398 cam 1.11 The commands that we'll cover here are considered <e>simple</e> because the
399 aaby 1.10 commands keep you in command mode. The more complex editing commands
400     automatically put you into insert mode -- a mode that allows you to
401     enter literal data from the keyboard. We'll cover those in a bit.
402     </p>
404     <p>
405     For now, try moving over some characters and hitting <c>x</c>
406     repeatedly. You'll see that <c>x</c> will delete the current
407     character under the cursor. Now, move to the middle of the paragraph
408     somewhere in your text file, and hit <c>J</c> (capitalized). You'll
409     see that the <c>J</c> command tells vi to join the next line to the
410     end of the current line. Now, move over a character and hit
411     <c>r</c>, and then type in a new character; you'll see that the
412     original character has been replaced. Finally, move to any line in
413     the file and type <c>dd</c>. You'll see that <c>dd</c> deletes the
414     current line of text.
415     </p>
416 drobbins 1.1
417     </body>
418     </section>
419     <section>
420     <title>Repeating and deleting</title>
421     <body>
422 aaby 1.10
423     <p>
424     You can repeat any editing command by hitting the <c>.</c> key. If
425     you experiment, you'll see that typing <c>dd...</c> will delete 4
426     lines, and <c>J......</c> will join four lines. As usual, vi provides
427     with another handy shortcut.
428     </p>
430     <p>
431     To delete text, you can also use the <c>d</c> command combined with
432     any movement command. For example, <c>dw</c> will delete from the
433     current position to the beginning of the next word; <c>d)</c> will
434     delete up until the end of the next sentence, and <c>d}</c> will
435     delete the remainder of the paragraph. Experiment with the
436     <c>d</c> command and the other editing commands until you're
437     comfortable with them.
438     </p>
440 drobbins 1.1 </body>
441     </section>
442     <section>
443     <title>Undo!</title>
444     <body>
445 aaby 1.10
446     <p>
447     Now that we're experimenting with deletion, it would be a good time to
448     learn how to undo any changes. By pressing <c>u</c>, the original
449     version of vi allowed you to undo the last edit only. However, modern
450     versions of vi like vim will allow you to repeatedly press <c>u</c> to
451     continue to undo changes to your file. Try combining some <c>d</c>
452     and <c>u</c> commands together.
453 drobbins 1.1 </p>
454 aaby 1.10
455 drobbins 1.1 </body>
456     </section>
457     <section>
458     <title>Updating the cheat sheet</title>
459     <body>
461 aaby 1.10 <p>
462     Time to update the cheat sheet! After adding all the commands we've
463     covered so far, your cheat sheet should look like this:
464     </p>
466     <figure
467     link="http://www.ibiblio.org/web-gentoo/images/vicheat-edit.png"
468     short="Cheat sheet" caption="Cheat sheet with editing commands"/>
469 drobbins 1.1
470     </body>
471     </section>
472     </chapter>
473 aaby 1.10
474 drobbins 1.1 <chapter>
475     <title>Insert mode</title>
476     <section>
477 aaby 1.10 <body>
479     <p>
480     So far, we've covered how to move around in vi, perform file i/o, and
481     perform basic editing operations. However, I still haven't shown you
482     how to actually type in free-form text! This was intentional, because
483     vi's insert mode is a bit complicated at first. However, after you
484     become comfortable with insert mode, its complexity (and flexibility)
485     will become an asset.
486     </p>
488     <p>
489 cam 1.11 In vi <e>insert mode</e>, you'll be able to enter text directly to the screen
490 aaby 1.10 just like you can in many other visual editors. Once you've entered
491 cam 1.11 your modifications, you can hit escape to return to <e>command mode</e>. You
492 aaby 1.10 can enter insert mode by pressing <c>i</c> or <c>a</c>. If you press
493     <c>i</c>, your text will be <e>inserted</e> before the current
494     character, and if you hit <c>a</c>, your text will be <e>appended</e>
495     after the current character. Remember, after you enter your text, hit
496     <c>&lt;ESC&gt;</c> to return to command mode.
497     </p>
498 drobbins 1.1
499     </body>
500     </section>
501     <section>
502     <title>Benefits of insert mode</title>
503     <body>
504 aaby 1.10
505     <p>
506     Go ahead and try using the <c>a</c> and <c>i</c> commands. Hit either
507     <c>a</c> or <c>i</c>, type some text, and then hit escape to get back
508     to command mode. After hitting <c>a</c> or <c>i</c>, try hitting
509     <c>&lt;ENTER&gt;</c>, and see what happens. Try using the arrow keys
510     and the <c>&lt;DEL&gt;</c> key to get a feel for how insert mode
511     works. By using the arrow keys and <c>&lt;DEL&gt;</c> key, you can
512     perform significant editing steps without repeatedly entering and
513 cam 1.11 leaving insert mode.
514 aaby 1.10 </p>
516 drobbins 1.1 </body>
517     </section>
518     <section>
519     <title>Insert options</title>
520     <body>
521 aaby 1.10
522     <p>
523     Here are some other handy ways to enter insert mode. Press <c>A</c>
524     (captial) to begin appending to the <e>end</e> of the current line,
525     regardless of your current position on the line. Likewise, press
526     <c>I</c> (capital) to begin inserting text at the <e>beginning</e> of
527     the current line. Press <c>o</c> to create a new blank line below the
528     current line into which you can insert text, and press <c>O</c>
529     (capital) to create a new line above the current line. To replace the
530     entire current line with a new line, press <c>cc</c>. To replace
531     everything from the current position to the end of the line, type
532     <c>c$</c>. To replace everything from the current position to the
533     beginning of the line, type <c>c0</c>.
534     </p>
536     <p>
537     In addition to performing a special operation, every one of these
538     commands will put you into insert mode. After typing in your text,
539     hit <c>&lt;ESC&gt;</c> to return to command mode.
540 drobbins 1.1 </p>
542     </body>
543     </section>
544     <section>
545     <title>Changing text</title>
546     <body>
547 aaby 1.10
548     <p>
549     We've used the <c>c</c> (change) command a little bit so far when we
550     typed <c>cc</c>, <c>c0</c> and <c>c$</c>. <c>cc</c> is a special form
551     of the change command, similar to <c>dd</c>. the <c>c0</c> and
552     <c>c$</c> commands are examples of using the change command in
553     combination with a movement command. In this form, <c>c</c> works
554     similarly to <c>d</c>, except that it leaves you in insert mode so
555     that you can enter replacement text for the deleted region. Try
556     combining some movement commands with <c>c</c> and test them out on
557     your file (hint: <c>cW</c>, <c>ce</c>, <c>c(</c> .)
558 drobbins 1.1 </p>
560     </body>
561     </section>
562     </chapter>
563 aaby 1.10
564 drobbins 1.1 <chapter>
565     <title>Compound Commands</title>
566     <section>
567     <body>
568 aaby 1.10
569     <p>
570     vi <e>really</e> becomes powerful when you start using compound
571     ("combo") commands, like <c>d{</c> and <c>cw</c>. In addition to
572     these commands, you can also combine a number with any movement
573     command, such as <c>3w</c>, which will tell vi to jump three words to
574     the right. Here are some more movement "combo" command examples:
575     <c>12b</c>, <c>4j</c>.
576     </p>
578     <p>
579     vi, in addition to allowing (number)(movement command) combinations,
580     also allows <c>d</c> or <c>c</c> to be combined with a number or
581     movement command. So, <c>d3w</c> will delete the next three words,
582     <c>d2j</c> will delete the current and next two lines, etc. Test out
583     some <c>c</c> and <c>d</c> combo moves to get a feel for how powerful
584     and concise vi editing can be. Once these commands are second-nature,
585     you'll be able to edit files at blazing speed.
586     </p>
587 drobbins 1.1
588     </body>
589     </section>
590     <section>
591     <title>Updating the cheat sheet</title>
592     <body>
593 aaby 1.10
594 drobbins 1.1 <p>
595 aaby 1.10 Time to update the cheat sheet again. Here's what it looks like so
596     far:
597     </p>
599     <figure
600     link="http://www.ibiblio.org/web-gentoo/images/vicheat-compound.png"
601     short="Cheat Sheet" caption="Cheat sheet with compound commands"/>
602 drobbins 1.1
603     </body>
604     </section>
605     <section>
606     <title>Productivity features</title>
607 aaby 1.10 <body>
609     <p>
610     So far, we've covered how to move, save and quit, perform simple edits
611     and deletions, and use insert mode. With everything listed on the
612     cheat sheet so far, you should be able to use vi to perform almost any
613     task.
614     </p>
615 drobbins 1.1
616 aaby 1.10 <p>
617     However, vi also has many more powerful commands. In this section,
618     you'll learn how to <e>cut</e>, <e>copy</e> and <e>paste</e>,
619     <e>search</e> and <e>replace</e>, and use <e>autoindent</e> features.
620     These commands will help make vi more fun and productive.
621     </p>
622 drobbins 1.1
623     </body>
624     </section>
625     <section>
626     <title>Visual mode</title>
627     <body>
628 aaby 1.10
629     <p>
630 cam 1.11 The best way to cut and paste is to use <e>visual mode</e>, a special mode that
631 drobbins 1.1 has been added to modern versions of vi, like vim and elvis. You can think
632     of visual mode as a "highlight text" mode. Once the text is highlighted,
633     it can be copied or deleted, and then pasted. If you are using gvim, you
634     can highlight by simply dragging the left mouse button over a particular
635 aaby 1.10 region:
636     </p>
637 drobbins 1.1
638 aaby 1.10 <figure
639     link="http://www.ibiblio.org/web-gentoo/images/vihighlight.png"
640     short="Highlighted text" caption="VIM with highlighted text"/>
641 drobbins 1.1
642 aaby 1.10 <p>
643     In addition, you can also enter visual mode by hitting <c>v</c> (this
644     may be your only option if you are using vi from the console.) Then,
645     by moving the cursor using movement commands (typically the arrow
646     keys), you'll be able to highlight a region of text. Once
647     highlighted, we are ready to cut or copy the text.
648 drobbins 1.1 </p>
650 aaby 1.10 <p>
651     If you're copying the text, hit <c>y</c> (which stands for "yank").
652     If you're cutting the text, hit <c>d</c>. You'll be placed back in
653     command mode. Now, move to the position where you'd like to insert
654     the cut or copied text, and hit <c>P</c> to insert after the cursor,
655     or <c>p</c> to insert before the cursor. Voila, the cut/copy and
656     paste is complete! Test out several copy/cut and paste operations
657     before advancing to the next section.
658 drobbins 1.1 </p>
660     </body>
661     </section>
662     <section>
663     <title>Replacing text</title>
664     <body>
665 aaby 1.10
666     <p>
667     To replace patterns of text, we use <e>ex</e> mode. If you'd like to
668     replace the first pattern that appears on the current line, type
669     <c>:s/&lt;regexp&gt;/&lt;replacement&gt;/</c> and hit
670     <c>&lt;ENTER&gt;</c>, where &lt;regexp&gt; is the pattern you'd like
671     to match and &lt;replacement&gt; is the replacement string. To
672     replace all matches on the current line, type
673     <c>:s/&lt;regexp&gt;/&lt;replacement&gt;/g</c> and hit enter. To
674     replace every occurence of this pattern in your file (normally what
675     you want), type <c>:%s/&lt;regexp&gt;/&lt;replacement&gt;/g</c>. If
676     you'd like to do a global replace, but have vi prompt you for each
677     change, type <c>:%s/&lt;regexp&gt;/&lt;replacement&gt;/gc</c> (stands
678     for "confirm") and hit <c>&lt;ENTER&gt;</c>.
679     </p>
680 drobbins 1.1
681     </body>
682     </section>
683     <section>
684     <title>Indentation</title>
685     <body>
686 aaby 1.10
687     <p>
688     vi supports autoindentation, for when you are editing source code.
689     Most modern versions of vi (like vim) will auto-enable autoindent mode
690     when you are editing a source file (like a .c file, for example).
691     When autoindent is enabled, you can use <c>&lt;CTR&gt;d</c>
692     (control-d) to move one indent level to the left, and
693     <c>&lt;CTR&gt;t</c> (control-t) to move one indent level to the right.
694     If autoindent wasn't enabled automatically, you can manually enable it
695     by typing in the <e>ex</e> command <c>:set</c> autoindent. You can
696     also tell vi to set the tab size to your favorite setting by using the
697     <c>:set tabstop</c> command; <c>:set tabstop=4</c> is quite popular.
698     </p>
699 drobbins 1.1
700     </body>
701     </section>
702     <section>
703     <title>Our final cheat sheet</title>
704     <body>
705 aaby 1.10
706 drobbins 1.1 <p>
707 aaby 1.10 Well, we've reached the end of the vi tutorial! After adding all the
708     advanced editing commands to your cheat sheet, it should look like
709     this:
710     </p>
712     <figure
713     link="http://www.ibiblio.org/web-gentoo/images/vicheat-final.png"
714     short="Cheat Sheet" caption="Final cheet sheet"/>
715 drobbins 1.1
716 aaby 1.10 <p>
717     Keep your cheat sheet handy, and begin using vi to edit files and
718     compose emails. Refer to the cheat sheet when needed; you'll find
719     that within the week, you'll have nearly all the commands memorized
720     and your vi productivity will shoot through the roof!
721 drobbins 1.1 </p>
723 cam 1.11 <p>
724     If you want to use vi as your default editor make the following change
725     to <path>/etc/rc.conf</path>:
726     </p>
728     <pre caption="Setting vi as the default editor">
729     <comment>(Set EDITOR to your preferred editor.)</comment>
730     #EDITOR="/bin/nano"
731     EDITOR="/usr/bin/vim"
732     #EDITOR="/usr/bin/emacs"
733     </pre>
735 drobbins 1.1 </body>
736     </section>
737     <section>
738     <title>Resources</title>
739     <body>
740 aaby 1.10
741     <p>
742     Here are some resources you may find helpful as you continue to learn
743     more about vi:
744     </p>
746 drobbins 1.1 <ul>
747 aaby 1.10 <li>
748 cam 1.11 <uri link="http://www.thomer.com/thomer/vi/vi.html">The vi Lovers
749 aaby 1.10 Home Page</uri>, an excellent resource for all things vi
750     </li>
751     <li>
752     <uri link="http://www.vim.org/">The vim homepage</uri> is the place
753     to go for all your vim needs
754     </li>
755     <li>
756     If you're looking for a good, old-fashioned book, <uri
757     link="http://www.oreilly.com/catalog/vi6/">Learning the vi Editor,
758     6th Edition</uri> would be an excellent choice. Contains good
759     coverage of vi and vi clones.
760     </li>
761 drobbins 1.1 </ul>
762 aaby 1.10
763 swift 1.8 </body>
764     </section>
765     <section>
766     <title>About this document</title>
767     <body>
769     <p>
770     The original version of this article was first published on IBM
771     developerWorks, and is property of Westtech Information Services. This
772     document is an updated version of the original article, and contains
773     various improvements made by the Gentoo Linux documentation team.
774     </p>
776 drobbins 1.1 </body>
777     </section>
778     </chapter>
779     </guide>

  ViewVC Help
Powered by ViewVC 1.1.20