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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1.14 - (hide annotations) (download) (as text)
Thu Jul 21 15:58:38 2005 UTC (8 years, 11 months ago) by neysx
Branch: MAIN
Changes since 1.13: +14 -20 lines
File MIME type: application/xml
Use local links instead of ibiblio for pics. Thanks to [exa] on #gentoo.cs

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

  ViewVC Help
Powered by ViewVC 1.1.20