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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1.10 - (hide annotations) (download) (as text)
Tue Dec 30 15:23:55 2003 UTC (14 years, 5 months ago) by aaby
Branch: MAIN
Changes since 1.9: +503 -254 lines
File MIME type: application/xml
updated (closes bug #36751)

1 swift 1.8 <?xml version="1.0" encoding="UTF-8"?>
2 aaby 1.10 <!-- $Header: /home/cvsroot/gentoo/xml/htdocs/doc/en/vi-guide.xml,v 1.9 2003/11/15 00:35:19 neysx Exp $ -->
3 swift 1.8 <!DOCTYPE guide SYSTEM "/dtd/guide.dtd">
5 zhen 1.2 <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     <version>1.1.3</version>
24     <date>December 30, 2003</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     Using a special accelerated "cheat sheet" method, this tutorial is
35     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 drobbins 1.1
47     </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     going to gradually put together a vi "cheat sheet". This sheet will
65     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     use a version of vi called "vim". vim is very popular and has a
99     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     enhanced command-line vi, vim also comes with gvim, a nice graphical
104     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 drobbins 1.1 $ vi myfile.txt
135 aaby 1.10 </pre>
137     <p>
138     If you have vim installed, type "vim myfile.txt". If you'd prefer to
139     use gvim, type "gvim myfile.txt". myfile.txt should be the name of a
140     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     editors, when vi starts up, it is in a special mode called "command
153     mode". This means that if you press "l" on the keyboard, instead of
154     inserting an "l" into the file at the current cursor position, the
155     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     vim) will also allow you to use the PGUP and PGDOWN keys for this
208     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     noticed that vi considers words like "foo-bar-oni" as five separate
234     words! This is because by default, vi delimits words by spaces
235     <e>or</e> punctuation. foo-bar-oni is therefore considered five
236     words: "foo","-","bar","-" and "oni".
237     </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     while foo-bar-oni is considered five vi words, it's considered only
244     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     <title>Quitting</title>
286     <body>
287 aaby 1.10
288     <p>
289     We've covered the basic movement commands, but there are another
290     couple of commands that you need to know. Typing <c>:q</c> will quit
291     vi. If this doesn't work, then you probably accidentally modified the
292     file in some way. To tell vi to quit, throwing away any changes, type
293     <c>:q!</c>. You should now be at the command prompt.
294     </p>
296     <p>
297     In vi, any command that begins with a ":" is said to be an
298     <e>ex-mode</e> command. This is because vi has a built-in non-visual
299     editor called <e>ex</e>. It can be used similarly to sed to perform
300     line-based editing operations. In addition, it can also be used to
301     quit, as we've just seen. If you ever hit the <c>Q</c> key while in
302     command mode, you'll be transported to ex mode. If this ever happens
303     to you , you'll be confronted with a : prompt, and hitting enter will
304     scroll the entire screen upwards. To get back to good 'ol vi mode,
305     simply type vi and hit enter.
306     </p>
307 drobbins 1.1
308     </body>
309     </section>
310     </chapter>
311 aaby 1.10
312 drobbins 1.1 <chapter>
313     <title>The Cheat Sheet</title>
314     <section>
315     <title>The beginnings of the cheat sheet</title>
316     <body>
318 aaby 1.10 <p>
319     We've covered a lot of commands, and it's time to transcribe them to
320     our cheat sheet. For the cheat sheet, you'll need a US letter or A4
321     sized piece of paper (we're going to pack a lot of info onto this
322     sheet!) Here's a picture of my cheat sheet after I've transcribed all
323     the commands that we've covered so far. Try to follow my layout if
324     possible so that we can fit everything on one sheet.
325     </p>
327     <figure
328     link="http://www.ibiblio.org/web-gentoo/images/vicheat-first.png"
329     short="First part of the cheat sheet" caption="Cheat Sheet"/>
330 drobbins 1.1
331     </body>
332     </section>
333     <section>
334     <title>Miscellaneous vi</title>
335 aaby 1.10 <body>
337     <p>
338     Let's continue our rapid command-covering pace. In command-mode, you
339     can jump to a particularline by typing <c>G</c>. To jump to the first
340     line of a file, type <c>1G</c>. Note that <c>G</c> is capitalized.
341     </p>
342 drobbins 1.1
343 aaby 1.10 <p>
344     If you want to jump to the next occurence of a particular text
345     pattern, type <c>/&lt;regexp&gt;</c> and hit <c>enter</c>. Replace
346     &lt;regexp&gt; with the regular expression you're looking for. If you
347     don't know how to use regular expressions, don't fret -- typing
348     <c>/foo</c> will move to the next occurence of <e>foo</e>. The only
349     thing you'll need to watch out for is when you want to refer to the
350     literal <c>^</c>, <c>.</c>, <c>$</c> or <c>\</c> characters. Prefix
351     these characters with a backslash (<c>\</c>), and you'll be set. For
352     example, <c>/foo\.gif</c> will search for the next occurence of
353     "foo.gif".
354     </p>
356     <p>
357     To repeat the search forwards, hit <c>n</c>. To repeat the search
358     backwards, type <c>N</c>. As always, test these commands out in your
359     very own vi editor. You can also type <c>//</c> to repeat the last
360     search.
361     </p>
362 drobbins 1.1
363     </body>
364     </section>
365     </chapter>
366 aaby 1.10
367 drobbins 1.1 <chapter>
368     <title>Saving and Editing</title>
369     <section>
370     <title>Save and save as...</title>
371     <body>
372 aaby 1.10
373     <p>
374     We've covered how you can use the <e>ex</e> command <c>:q</c> to quit
375     from vi. If you want to save your changes, type <c>:w</c>. If you
376     want to save your changes to another file, type <c>:w filename.txt</c>
377     to save as <e>filename.txt</e>. If you want to save and quit, type
378     <c>:x</c> or <c>:wq</c>.
379     </p>
381     <p>
382     In vim (and other advanced vi editors, like elvis)<c>:w</c>, you can
383     have multiple buffers open at once. To open a file into a new window,
384     type <c>:sp filename.txt</c>. <e>filename.txt</e> will appear open
385     for editing in a new split window. To switch between windows, type
386     <c>&lt;CTR&gt;w&lt;CTR&gt;w</c> (control-w twice). Any <c>:q</c>,
387     <c>:q!</c>, <c>:w</c> and <c>:x</c> commands that you enter will only
388     be applied to the currently-active window.
389     </p>
391 drobbins 1.1 </body>
392     </section>
393     <section>
394     <title>Simple edits</title>
395     <body>
396 aaby 1.10
397     <p>
398     Now, it's time to start learning some of the simple editing commands.
399     The commands that we'll cover here are considered "simple" because the
400     commands keep you in command mode. The more complex editing commands
401     automatically put you into insert mode -- a mode that allows you to
402     enter literal data from the keyboard. We'll cover those in a bit.
403     </p>
405     <p>
406     For now, try moving over some characters and hitting <c>x</c>
407     repeatedly. You'll see that <c>x</c> will delete the current
408     character under the cursor. Now, move to the middle of the paragraph
409     somewhere in your text file, and hit <c>J</c> (capitalized). You'll
410     see that the <c>J</c> command tells vi to join the next line to the
411     end of the current line. Now, move over a character and hit
412     <c>r</c>, and then type in a new character; you'll see that the
413     original character has been replaced. Finally, move to any line in
414     the file and type <c>dd</c>. You'll see that <c>dd</c> deletes the
415     current line of text.
416     </p>
417 drobbins 1.1
418     </body>
419     </section>
420     <section>
421     <title>Repeating and deleting</title>
422     <body>
423 aaby 1.10
424     <p>
425     You can repeat any editing command by hitting the <c>.</c> key. If
426     you experiment, you'll see that typing <c>dd...</c> will delete 4
427     lines, and <c>J......</c> will join four lines. As usual, vi provides
428     with another handy shortcut.
429     </p>
431     <p>
432     To delete text, you can also use the <c>d</c> command combined with
433     any movement command. For example, <c>dw</c> will delete from the
434     current position to the beginning of the next word; <c>d)</c> will
435     delete up until the end of the next sentence, and <c>d}</c> will
436     delete the remainder of the paragraph. Experiment with the
437     <c>d</c> command and the other editing commands until you're
438     comfortable with them.
439     </p>
441 drobbins 1.1 </body>
442     </section>
443     <section>
444     <title>Undo!</title>
445     <body>
446 aaby 1.10
447     <p>
448     Now that we're experimenting with deletion, it would be a good time to
449     learn how to undo any changes. By pressing <c>u</c>, the original
450     version of vi allowed you to undo the last edit only. However, modern
451     versions of vi like vim will allow you to repeatedly press <c>u</c> to
452     continue to undo changes to your file. Try combining some <c>d</c>
453     and <c>u</c> commands together.
454 drobbins 1.1 </p>
455 aaby 1.10
456 drobbins 1.1 </body>
457     </section>
458     <section>
459     <title>Updating the cheat sheet</title>
460     <body>
462 aaby 1.10 <p>
463     Time to update the cheat sheet! After adding all the commands we've
464     covered so far, your cheat sheet should look like this:
465     </p>
467     <figure
468     link="http://www.ibiblio.org/web-gentoo/images/vicheat-edit.png"
469     short="Cheat sheet" caption="Cheat sheet with editing commands"/>
470 drobbins 1.1
471     </body>
472     </section>
473     </chapter>
474 aaby 1.10
475 drobbins 1.1 <chapter>
476     <title>Insert mode</title>
477     <section>
478 aaby 1.10 <body>
480     <p>
481     So far, we've covered how to move around in vi, perform file i/o, and
482     perform basic editing operations. However, I still haven't shown you
483     how to actually type in free-form text! This was intentional, because
484     vi's insert mode is a bit complicated at first. However, after you
485     become comfortable with insert mode, its complexity (and flexibility)
486     will become an asset.
487     </p>
489     <p>
490     In vi insert mode, you'll be able to enter text directly to the screen
491     just like you can in many other visual editors. Once you've entered
492     your modifications, you can hit escape to return to command mode. You
493     can enter insert mode by pressing <c>i</c> or <c>a</c>. If you press
494     <c>i</c>, your text will be <e>inserted</e> before the current
495     character, and if you hit <c>a</c>, your text will be <e>appended</e>
496     after the current character. Remember, after you enter your text, hit
497     <c>&lt;ESC&gt;</c> to return to command mode.
498     </p>
499 drobbins 1.1
500     </body>
501     </section>
502     <section>
503     <title>Benefits of insert mode</title>
504     <body>
505 aaby 1.10
506     <p>
507     Go ahead and try using the <c>a</c> and <c>i</c> commands. Hit either
508     <c>a</c> or <c>i</c>, type some text, and then hit escape to get back
509     to command mode. After hitting <c>a</c> or <c>i</c>, try hitting
510     <c>&lt;ENTER&gt;</c>, and see what happens. Try using the arrow keys
511     and the <c>&lt;DEL&gt;</c> key to get a feel for how insert mode
512     works. By using the arrow keys and <c>&lt;DEL&gt;</c> key, you can
513     perform significant editing steps without repeatedly entering and
514     leaving insert mode
515     </p>
517 drobbins 1.1 </body>
518     </section>
519     <section>
520     <title>Insert options</title>
521     <body>
522 aaby 1.10
523     <p>
524     Here are some other handy ways to enter insert mode. Press <c>A</c>
525     (captial) to begin appending to the <e>end</e> of the current line,
526     regardless of your current position on the line. Likewise, press
527     <c>I</c> (capital) to begin inserting text at the <e>beginning</e> of
528     the current line. Press <c>o</c> to create a new blank line below the
529     current line into which you can insert text, and press <c>O</c>
530     (capital) to create a new line above the current line. To replace the
531     entire current line with a new line, press <c>cc</c>. To replace
532     everything from the current position to the end of the line, type
533     <c>c$</c>. To replace everything from the current position to the
534     beginning of the line, type <c>c0</c>.
535     </p>
537     <p>
538     In addition to performing a special operation, every one of these
539     commands will put you into insert mode. After typing in your text,
540     hit <c>&lt;ESC&gt;</c> to return to command mode.
541 drobbins 1.1 </p>
543     </body>
544     </section>
545     <section>
546     <title>Changing text</title>
547     <body>
548 aaby 1.10
549     <p>
550     We've used the <c>c</c> (change) command a little bit so far when we
551     typed <c>cc</c>, <c>c0</c> and <c>c$</c>. <c>cc</c> is a special form
552     of the change command, similar to <c>dd</c>. the <c>c0</c> and
553     <c>c$</c> commands are examples of using the change command in
554     combination with a movement command. In this form, <c>c</c> works
555     similarly to <c>d</c>, except that it leaves you in insert mode so
556     that you can enter replacement text for the deleted region. Try
557     combining some movement commands with <c>c</c> and test them out on
558     your file (hint: <c>cW</c>, <c>ce</c>, <c>c(</c> .)
559 drobbins 1.1 </p>
561     </body>
562     </section>
563     </chapter>
564 aaby 1.10
565 drobbins 1.1 <chapter>
566     <title>Compound Commands</title>
567     <section>
568     <body>
569 aaby 1.10
570     <p>
571     vi <e>really</e> becomes powerful when you start using compound
572     ("combo") commands, like <c>d{</c> and <c>cw</c>. In addition to
573     these commands, you can also combine a number with any movement
574     command, such as <c>3w</c>, which will tell vi to jump three words to
575     the right. Here are some more movement "combo" command examples:
576     <c>12b</c>, <c>4j</c>.
577     </p>
579     <p>
580     vi, in addition to allowing (number)(movement command) combinations,
581     also allows <c>d</c> or <c>c</c> to be combined with a number or
582     movement command. So, <c>d3w</c> will delete the next three words,
583     <c>d2j</c> will delete the current and next two lines, etc. Test out
584     some <c>c</c> and <c>d</c> combo moves to get a feel for how powerful
585     and concise vi editing can be. Once these commands are second-nature,
586     you'll be able to edit files at blazing speed.
587     </p>
588 drobbins 1.1
589     </body>
590     </section>
591     <section>
592     <title>Updating the cheat sheet</title>
593     <body>
594 aaby 1.10
595 drobbins 1.1 <p>
596 aaby 1.10 Time to update the cheat sheet again. Here's what it looks like so
597     far:
598     </p>
600     <figure
601     link="http://www.ibiblio.org/web-gentoo/images/vicheat-compound.png"
602     short="Cheat Sheet" caption="Cheat sheet with compound commands"/>
603 drobbins 1.1
604     </body>
605     </section>
606     <section>
607     <title>Productivity features</title>
608 aaby 1.10 <body>
610     <p>
611     So far, we've covered how to move, save and quit, perform simple edits
612     and deletions, and use insert mode. With everything listed on the
613     cheat sheet so far, you should be able to use vi to perform almost any
614     task.
615     </p>
616 drobbins 1.1
617 aaby 1.10 <p>
618     However, vi also has many more powerful commands. In this section,
619     you'll learn how to <e>cut</e>, <e>copy</e> and <e>paste</e>,
620     <e>search</e> and <e>replace</e>, and use <e>autoindent</e> features.
621     These commands will help make vi more fun and productive.
622     </p>
623 drobbins 1.1
624     </body>
625     </section>
626     <section>
627     <title>Visual mode</title>
628     <body>
629 aaby 1.10
630     <p>
631     The best way to cut and paste is to use visual mode, a special mode that
632 drobbins 1.1 has been added to modern versions of vi, like vim and elvis. You can think
633     of visual mode as a "highlight text" mode. Once the text is highlighted,
634     it can be copied or deleted, and then pasted. If you are using gvim, you
635     can highlight by simply dragging the left mouse button over a particular
636 aaby 1.10 region:
637     </p>
638 drobbins 1.1
639 aaby 1.10 <figure
640     link="http://www.ibiblio.org/web-gentoo/images/vihighlight.png"
641     short="Highlighted text" caption="VIM with highlighted text"/>
642 drobbins 1.1
643 aaby 1.10 <p>
644     In addition, you can also enter visual mode by hitting <c>v</c> (this
645     may be your only option if you are using vi from the console.) Then,
646     by moving the cursor using movement commands (typically the arrow
647     keys), you'll be able to highlight a region of text. Once
648     highlighted, we are ready to cut or copy the text.
649 drobbins 1.1 </p>
651 aaby 1.10 <p>
652     If you're copying the text, hit <c>y</c> (which stands for "yank").
653     If you're cutting the text, hit <c>d</c>. You'll be placed back in
654     command mode. Now, move to the position where you'd like to insert
655     the cut or copied text, and hit <c>P</c> to insert after the cursor,
656     or <c>p</c> to insert before the cursor. Voila, the cut/copy and
657     paste is complete! Test out several copy/cut and paste operations
658     before advancing to the next section.
659 drobbins 1.1 </p>
661     </body>
662     </section>
663     <section>
664     <title>Replacing text</title>
665     <body>
666 aaby 1.10
667     <p>
668     To replace patterns of text, we use <e>ex</e> mode. If you'd like to
669     replace the first pattern that appears on the current line, type
670     <c>:s/&lt;regexp&gt;/&lt;replacement&gt;/</c> and hit
671     <c>&lt;ENTER&gt;</c>, where &lt;regexp&gt; is the pattern you'd like
672     to match and &lt;replacement&gt; is the replacement string. To
673     replace all matches on the current line, type
674     <c>:s/&lt;regexp&gt;/&lt;replacement&gt;/g</c> and hit enter. To
675     replace every occurence of this pattern in your file (normally what
676     you want), type <c>:%s/&lt;regexp&gt;/&lt;replacement&gt;/g</c>. If
677     you'd like to do a global replace, but have vi prompt you for each
678     change, type <c>:%s/&lt;regexp&gt;/&lt;replacement&gt;/gc</c> (stands
679     for "confirm") and hit <c>&lt;ENTER&gt;</c>.
680     </p>
681 drobbins 1.1
682     </body>
683     </section>
684     <section>
685     <title>Indentation</title>
686     <body>
687 aaby 1.10
688     <p>
689     vi supports autoindentation, for when you are editing source code.
690     Most modern versions of vi (like vim) will auto-enable autoindent mode
691     when you are editing a source file (like a .c file, for example).
692     When autoindent is enabled, you can use <c>&lt;CTR&gt;d</c>
693     (control-d) to move one indent level to the left, and
694     <c>&lt;CTR&gt;t</c> (control-t) to move one indent level to the right.
695     If autoindent wasn't enabled automatically, you can manually enable it
696     by typing in the <e>ex</e> command <c>:set</c> autoindent. You can
697     also tell vi to set the tab size to your favorite setting by using the
698     <c>:set tabstop</c> command; <c>:set tabstop=4</c> is quite popular.
699     </p>
700 drobbins 1.1
701     </body>
702     </section>
703     <section>
704     <title>Our final cheat sheet</title>
705     <body>
706 aaby 1.10
707 drobbins 1.1 <p>
708 aaby 1.10 Well, we've reached the end of the vi tutorial! After adding all the
709     advanced editing commands to your cheat sheet, it should look like
710     this:
711     </p>
713     <figure
714     link="http://www.ibiblio.org/web-gentoo/images/vicheat-final.png"
715     short="Cheat Sheet" caption="Final cheet sheet"/>
716 drobbins 1.1
717 aaby 1.10 <p>
718     Keep your cheat sheet handy, and begin using vi to edit files and
719     compose emails. Refer to the cheat sheet when needed; you'll find
720     that within the week, you'll have nearly all the commands memorized
721     and your vi productivity will shoot through the roof!
722 drobbins 1.1 </p>
724     </body>
725     </section>
726     <section>
727     <title>Resources</title>
728     <body>
729 aaby 1.10
730     <p>
731     Here are some resources you may find helpful as you continue to learn
732     more about vi:
733     </p>
735 drobbins 1.1 <ul>
736 aaby 1.10 <li>
737     <uri link="http://www.thomer.com/thomer/vi/vi.html">The vi Lovers
738     Home Page</uri>, an excellent resource for all things vi
739     </li>
740     <li>
741     <uri link="http://www.vim.org/">The vim homepage</uri> is the place
742     to go for all your vim needs
743     </li>
744     <li>
745     If you're looking for a good, old-fashioned book, <uri
746     link="http://www.oreilly.com/catalog/vi6/">Learning the vi Editor,
747     6th Edition</uri> would be an excellent choice. Contains good
748     coverage of vi and vi clones.
749     </li>
750 drobbins 1.1 </ul>
751 aaby 1.10
752 swift 1.8 </body>
753     </section>
754     <section>
755     <title>About this document</title>
756     <body>
758     <p>
759     The original version of this article was first published on IBM
760     developerWorks, and is property of Westtech Information Services. This
761     document is an updated version of the original article, and contains
762     various improvements made by the Gentoo Linux documentation team.
763     </p>
765 drobbins 1.1 </body>
766     </section>
767     </chapter>
768     </guide>

  ViewVC Help
Powered by ViewVC 1.1.20