/[gentoo]/xml/htdocs/doc/en/vi-guide.xml
Gentoo

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.15

  ViewVC Help
Powered by ViewVC 1.1.20