/[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.10 Revision 1.18
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.10 2003/12/30 15:23:55 aaby Exp $ --> 2<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/vi-guide.xml,v 1.18 2011/09/04 17:53:40 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>
6<title>Learning vi -- the "cheatsheet" technique</title> 6<title>Learning vi -- the "cheatsheet" technique</title>
7<author title="Author"> 7<author title="Author">
8 <mail link="drobbins@gentoo.org">Daniel Robbins</mail> 8 <mail link="drobbins@gentoo.org">Daniel Robbins</mail>
9</author> 9</author>
10<author title="Author"> 10<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.3</version> 23<version>2</version>
24<date>December 30, 2003</date> 24<date>2011-08-15</date>
25 25
26<chapter> 26<chapter>
27<title>Getting Started</title> 27<title>Getting Started</title>
28<section> 28<section>
29<title>Introduction</title> 29<title>Introduction</title>
30<body> 30<body>
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 "cheat sheet" 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>
42If you either don't know or aren't comfortable using vi, then you owe 42If you either don't know or aren't comfortable using vi, then you owe
43it to yourself to take this tutorial and get up to speed with one of 43it to yourself to take this tutorial and get up to speed with one of
44the most popular and powerful Linux/UNIX visual editing programs. 44the most popular and powerful Linux/UNIX visual editing programs.
45</p> 45</p>
46 46
47</body> 47</body>
48</section> 48</section>
49<section> 49<section>
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 "vim". 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 gvim, 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="http://www.ibiblio.org/web-gentoo/images/vishot.png" 108<figure link="/images/vishot.png" short="screenshot" caption="VIM screenshot"/>
109short="screenshot" caption="VIM screenshot"/>
110 109
111<p> 110<p>
112If 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
113vi from a gui can make things a bit easier for beginners. 112vi from a gui can make things a bit easier for beginners.
114</p> 113</p>
115 114
116</body> 115</body>
117</section> 116</section>
123<title>Pick a file</title> 122<title>Pick a file</title>
124<body> 123<body>
125 124
126<p> 125<p>
127Before 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
128around 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
129to 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
130an unimportant text file and load it into vi by typing: 129an unimportant text file and load it into vi by typing:
131</p> 130</p>
132 131
133<pre caption="Loading a file into vi"> 132<pre caption="Loading a file into vi">
134$ vi myfile.txt 133$ <i>vi myfile.txt</i>
135</pre> 134</pre>
136 135
137<p> 136<p>
138If you have vim installed, type "vim myfile.txt". If you'd prefer to 137If you have vim installed, type <c>vim myfile.txt</c>. If you'd prefer to
139use gvim, type "gvim myfile.txt". myfile.txt should be the name of a 138use gvim, type <c>gvim myfile.txt</c>. <path>myfile.txt</path> should be the
140text file on your system. 139name of a text file on your system.
141</p> 140</p>
142 141
143</body> 142</body>
144</section> 143</section>
145<section> 144<section>
146<title>Inside vi</title> 145<title>Inside vi</title>
147<body> 146<body>
148 147
149<p> 148<p>
150After 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
151on your screen. Congratulations -- you're in vi! Unlike many 150on your screen. Congratulations -- you're in vi! Unlike many
152editors, when vi starts up, it is in a special mode called "command 151editors, when vi starts up, it is in a special mode called <e>command
153mode". This means that if you press "l" on the keyboard, instead of 152mode</e>. This means that if you press <c>l</c>on the keyboard, instead of
154inserting an "l" into the file at the current cursor position, the 153inserting an <c>l</c> into the file at the current cursor position, the
155cursor will move one character to the right instead. In command mode, 154cursor will move one character to the right instead. In command mode,
156the 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
157than insert literal characters into the text. One of the most 156than insert literal characters into the text. One of the most
158essential types of commands are movement commands; let's take a look 157essential types of commands are movement commands; let's take a look
159at some. 158at some.
160</p> 159</p>
161 160
162</body> 161</body>
170<body> 169<body>
171 170
172<p> 171<p>
173When 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
174<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
175respectively. 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
176use 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>
177and <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,
178you'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
179from 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
180<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
181using <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
182vi 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
183<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
184"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
185line. 184line.
186</p> 185</p>
187 186
188</body> 187</body>
191<title>Moving in vi, part 2</title> 190<title>Moving in vi, part 2</title>
192<body> 191<body>
193 192
194<p> 193<p>
195vi 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
196current 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
197character 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
198line. 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,
199it 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
200as 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
201quickly. 200quickly.
202</p> 201</p>
203 202
204<p> 203<p>
205You 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
206forwards 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
207vim) will also allow you to use the PGUP and PGDOWN keys for this 206vim) will also allow you to use the <c>PGUP</c> and <c>PGDOWN</c> keys for this
208purpose. 207purpose.
209</p> 208</p>
210 209
211</body> 210</body>
212</section> 211</section>
215<body> 214<body>
216 215
217<p> 216<p>
218vi 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.
219To 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
220<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,
221press <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>
222word, press <c>b</c>. Test 'em out. 221word, press <c>b</c>. Test 'em out.
223</p> 222</p>
224 223
225</body> 224</body>
226</section> 225</section>
227<section> 226<section>
228<title>Word moves, part 2</title> 227<title>Word moves, part 2</title>
229<body> 228<body>
230 229
231<p> 230<p>
232After playing around with the word movement commands, you may have 231After playing around with the word movement commands, you may have
233noticed that vi considers words like "foo-bar-oni" as five separate 232noticed that vi considers words like <c>foo-bar-oni</c> as five separate
234words! This is because by default, vi delimits words by spaces 233words! This is because by default, vi delimits words by spaces
235<e>or</e> punctuation. foo-bar-oni is therefore considered five 234<e>or</e> punctuation. <c>foo-bar-oni</c> is therefore considered five
236words: "foo","-","bar","-" and "oni". 235words: <c>foo</c>, <c>-</c>, <c>bar</c>, <c>-</c> and <c>oni</c>.
237</p> 236</p>
238 237
239<p> 238<p>
240Sometimes, this is what you want, and sometimes it isn't. 239Sometimes, this is what you want, and sometimes it isn't.
241Fortunately, vi also understands the concept of a "bigword". vi 240Fortunately, vi also understands the concept of a "bigword". vi
242delimits bigwords by <e>spaces or newlines only</e>. This means that 241delimits bigwords by <e>spaces or newlines only</e>. This means that
243while foo-bar-oni is considered five vi words, it's considered only 242while <c>foo-bar-oni</c> is considered five vi words, it's considered only
244one vi bigword. 243one vi bigword.
245</p> 244</p>
246 245
247</body> 246</body>
248</section> 247</section>
250<title>Word moves, part 3</title> 249<title>Word moves, part 3</title>
251<body> 250<body>
252 251
253<p> 252<p>
254To 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
255<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
256first 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
257character 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
258character of the previous bigword. Test 'em out, and compare the 257character of the previous bigword. Test 'em out, and compare the
259matching word and bigword movement commands until you understand their 258matching word and bigword movement commands until you understand their
260differences. 259differences.
261</p> 260</p>
262 261
263</body> 262</body>
266<title>Bigger moves</title> 265<title>Bigger moves</title>
267<body> 266<body>
268 267
269<p> 268<p>
270We 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
271puting together our cheat sheet. You can use the <c>(</c> and 270puting 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 271<c>)</c> characters to move to the beginning of the previous and next
273sentence. 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
274the beginning of the current paragraph, and the beginning of the next. 273the beginning of the current paragraph, and the beginning of the next.
275Test 'em out. 274Test 'em out.
276</p> 275</p>
277 276
278</body> 277</body>
280</chapter> 279</chapter>
281 280
282<chapter> 281<chapter>
283<title>Quitting</title> 282<title>Quitting</title>
284<section> 283<section>
285<title>Quitting</title>
286<body> 284<body>
287 285
288<p> 286<p>
289We've covered the basic movement commands, but there are another 287We've covered the basic movement commands, but there are another
290couple 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
291vi. If this doesn't work, then you probably accidentally modified the 289vi. If this doesn't work, then you probably accidentally modified the
292file 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
293<c>:q!</c>. You should now be at the command prompt. 291<c>:q!</c>. You should now be at the command prompt.
294</p>
295
296<p> 292</p>
293
294<p>
297In vi, any command that begins with a ":" is said to be an 295In vi, any command that begins with a <c>:</c> is said to be an
298<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
299editor 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
300line-based editing operations. In addition, it can also be used to 298line-based editing operations. In addition, it can also be used to
301quit, 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
302command 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
303to 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
304scroll 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,
305simply type vi and hit enter. 303simply type vi and hit enter.
306</p> 304</p>
307 305
308</body> 306</body>
309</section> 307</section>
315<title>The beginnings of the cheat sheet</title> 313<title>The beginnings of the cheat sheet</title>
316<body> 314<body>
317 315
318<p> 316<p>
319We'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
320our 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
321sized 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
322sheet!) 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
323the 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
324possible so that we can fit everything on one sheet. 322possible so that we can fit everything on one sheet.
325</p> 323</p>
326 324
327<figure 325<figure link="/images/vicheat-first.png" short="First part of the cheat sheet"
328link="http://www.ibiblio.org/web-gentoo/images/vicheat-first.png" 326caption="Cheat Sheet"/>
329short="First part of the cheat sheet" caption="Cheat Sheet"/>
330 327
331</body> 328</body>
332</section> 329</section>
333<section> 330<section>
334<title>Miscellaneous vi</title> 331<title>Miscellaneous vi</title>
335<body> 332<body>
336 333
337<p> 334<p>
338Let's continue our rapid command-covering pace. In command-mode, you 335Let's continue our rapid command-covering pace. In command-mode, you
339can 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
340line 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.
341</p> 338</p>
342 339
343<p> 340<p>
344If 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
345pattern, 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
346&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
347don't know how to use regular expressions, don't fret -- typing 344don'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 345<c>/foo</c> will move to the next occurence of <e>foo</e>. The only
349thing 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
350literal <c>^</c>, <c>.</c>, <c>$</c> or <c>\</c> characters. Prefix 347literal <c>^</c>, <c>.</c>, <c>$</c> or <c>\</c> characters. Prefix
351these 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
352example, <c>/foo\.gif</c> will search for the next occurence of 349example, <c>/foo\.gif</c> will search for the next occurence of
353"foo.gif". 350"foo.gif".
354</p> 351</p>
355 352
356<p> 353<p>
357To 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
358backwards, 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
359very 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
360search. 357search.
361</p> 358</p>
362 359
363</body> 360</body>
370<title>Save and save as...</title> 367<title>Save and save as...</title>
371<body> 368<body>
372 369
373<p> 370<p>
374We'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
375from 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
376want 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>
377to 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
378<c>:x</c> or <c>:wq</c>. 375<c>:x</c> or <c>:wq</c>.
379</p> 376</p>
380 377
381<p> 378<p>
382In 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
383have 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,
384type <c>:sp filename.txt</c>. <e>filename.txt</e> will appear open 381type <c>:sp filename.txt</c>. <path>filename.txt</path> will appear open
385for editing in a new split window. To switch between windows, type 382for 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>, 383<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 384<c>:q!</c>, <c>:w</c> and <c>:x</c> commands that you enter will only
388be applied to the currently-active window. 385be applied to the currently-active window.
389</p> 386</p>
390 387
391</body> 388</body>
394<title>Simple edits</title> 391<title>Simple edits</title>
395<body> 392<body>
396 393
397<p> 394<p>
398Now, 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.
399The commands that we'll cover here are considered "simple" because the 396The commands that we'll cover here are considered <e>simple</e> because the
400commands keep you in command mode. The more complex editing commands 397commands keep you in command mode. The more complex editing commands
401automatically put you into insert mode -- a mode that allows you to 398automatically put you into insert mode -- a mode that allows you to
402enter 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.
403</p> 400</p>
404 401
405<p> 402<p>
406For now, try moving over some characters and hitting <c>x</c> 403For now, try moving over some characters and hitting <c>x</c>
407repeatedly. You'll see that <c>x</c> will delete the current 404repeatedly. You'll see that <c>x</c> will delete the current
408character under the cursor. Now, move to the middle of the paragraph 405character under the cursor. Now, move to the middle of the paragraph
409somewhere 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
410see 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
411end of the current line. Now, move over a character and hit 408end 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 409<c>r</c>, and then type in a new character; you'll see that the
413original character has been replaced. Finally, move to any line in 410original character has been replaced. Finally, move to any line in
414the 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
415current line of text. 412current line of text.
416</p> 413</p>
417 414
418</body> 415</body>
419</section> 416</section>
420<section> 417<section>
421<title>Repeating and deleting</title> 418<title>Repeating and deleting</title>
422<body> 419<body>
423 420
424<p> 421<p>
425You 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
426you 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
427lines, 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
428with another handy shortcut. 425with another handy shortcut.
429</p> 426</p>
430 427
431<p> 428<p>
432To 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
433any movement command. For example, <c>dw</c> will delete from the 430any movement command. For example, <c>dw</c> will delete from the
434current 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
435delete 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
436delete the remainder of the paragraph. Experiment with the 433delete the remainder of the paragraph. Experiment with the
437<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
438comfortable with them. 435comfortable with them.
439</p> 436</p>
440 437
441</body> 438</body>
444<title>Undo!</title> 441<title>Undo!</title>
445<body> 442<body>
446 443
447<p> 444<p>
448Now 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
449learn 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
450version 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
451versions 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
452continue 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>
453and <c>u</c> commands together. 450and <c>u</c> commands together.
454</p> 451</p>
455 452
456</body> 453</body>
457</section> 454</section>
458<section> 455<section>
459<title>Updating the cheat sheet</title> 456<title>Updating the cheat sheet</title>
460<body> 457<body>
461 458
462<p> 459<p>
463Time 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
464covered so far, your cheat sheet should look like this: 461covered so far, your cheat sheet should look like this:
465</p> 462</p>
466 463
467<figure 464<figure link="/images/vicheat-edit.png" short="Cheat sheet" caption="Cheat
468link="http://www.ibiblio.org/web-gentoo/images/vicheat-edit.png" 465sheet with editing commands"/>
469short="Cheat sheet" caption="Cheat sheet with editing commands"/>
470 466
471</body> 467</body>
472</section> 468</section>
473</chapter> 469</chapter>
474 470
477<section> 473<section>
478<body> 474<body>
479 475
480<p> 476<p>
481So 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
482perform basic editing operations. However, I still haven't shown you 478perform basic editing operations. However, I still haven't shown you
483how to actually type in free-form text! This was intentional, because 479how to actually type in free-form text! This was intentional, because
484vi'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
485become comfortable with insert mode, its complexity (and flexibility) 481become comfortable with insert mode, its complexity (and flexibility)
486will become an asset. 482will become an asset.
487</p> 483</p>
488 484
489<p> 485<p>
490In vi insert mode, 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
491just 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
492your modifications, you can hit escape to return to command mode. You 488your modifications, you can hit escape to return to <e>command mode</e>. You
493can 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
494<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
495character, 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>
496after the current character. Remember, after you enter your text, hit 492after the current character. Remember, after you enter your text, hit
497<c>&lt;ESC&gt;</c> to return to command mode. 493<c>&lt;ESC&gt;</c> to return to command mode.
498</p> 494</p>
499 495
500</body> 496</body>
501</section> 497</section>
502<section> 498<section>
503<title>Benefits of insert mode</title> 499<title>Benefits of insert mode</title>
504<body> 500<body>
505 501
506<p> 502<p>
507Go 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
508<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
509to 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
510<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
511and 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
512works. 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
513perform significant editing steps without repeatedly entering and 509perform significant editing steps without repeatedly entering and
514leaving insert mode 510leaving insert mode.
515</p> 511</p>
516 512
517</body> 513</body>
518</section> 514</section>
519<section> 515<section>
520<title>Insert options</title> 516<title>Insert options</title>
521<body> 517<body>
522 518
523<p> 519<p>
524Here 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>
525(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,
526regardless of your current position on the line. Likewise, press 522regardless of your current position on the line. Likewise, press
527<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
528the 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
529current 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>
530(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
531entire 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
532everything from the current position to the end of the line, type 528everything from the current position to the end of the line, type
533<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
534beginning of the line, type <c>c0</c>. 530beginning of the line, type <c>c0</c>.
535</p> 531</p>
536 532
537<p> 533<p>
538In addition to performing a special operation, every one of these 534In addition to performing a special operation, every one of these
539commands will put you into insert mode. After typing in your text, 535commands will put you into insert mode. After typing in your text,
540hit <c>&lt;ESC&gt;</c> to return to command mode. 536hit <c>&lt;ESC&gt;</c> to return to command mode.
541</p> 537</p>
542 538
543</body> 539</body>
544</section> 540</section>
546<title>Changing text</title> 542<title>Changing text</title>
547<body> 543<body>
548 544
549<p> 545<p>
550We'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
551typed <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
552of 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
553<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
554combination with a movement command. In this form, <c>c</c> works 550combination with a movement command. In this form, <c>c</c> works
555similarly 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
556that you can enter replacement text for the deleted region. Try 552that you can enter replacement text for the deleted region. Try
557combining 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
558your file (hint: <c>cW</c>, <c>ce</c>, <c>c(</c> .) 554your file (hint: <c>cW</c>, <c>ce</c>, <c>c(</c> .)
559</p> 555</p>
560 556
561</body> 557</body>
567<section> 563<section>
568<body> 564<body>
569 565
570<p> 566<p>
571vi <e>really</e> becomes powerful when you start using compound 567vi <e>really</e> becomes powerful when you start using compound
572("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
573these commands, you can also combine a number with any movement 569these commands, you can also combine a number with any movement
574command, 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
575the right. Here are some more movement "combo" command examples: 571the right. Here are some more movement "combo" command examples:
576<c>12b</c>, <c>4j</c>. 572<c>12b</c>, <c>4j</c>.
577</p> 573</p>
578 574
579<p> 575<p>
580vi, in addition to allowing (number)(movement command) combinations, 576vi, in addition to allowing (number)(movement command) combinations,
581also 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
582movement command. So, <c>d3w</c> will delete the next three words, 578movement 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 579<c>d2j</c> will delete the current and next two lines, etc. Test out
584some <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
585and concise vi editing can be. Once these commands are second-nature, 581and concise vi editing can be. Once these commands are second-nature,
586you'll be able to edit files at blazing speed. 582you'll be able to edit files at blazing speed.
587</p> 583</p>
588 584
589</body> 585</body>
590</section> 586</section>
591<section> 587<section>
592<title>Updating the cheat sheet</title> 588<title>Updating the cheat sheet</title>
593<body> 589<body>
594 590
595<p> 591<p>
596Time 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
597far: 593far:
598</p> 594</p>
599 595
600<figure 596<figure link="/images/vicheat-compound.png" short="Cheat Sheet" caption="Cheat
601link="http://www.ibiblio.org/web-gentoo/images/vicheat-compound.png" 597sheet with compound commands"/>
602short="Cheat Sheet" caption="Cheat sheet with compound commands"/>
603 598
604</body> 599</body>
605</section> 600</section>
606<section> 601<section>
607<title>Productivity features</title> 602<title>Productivity features</title>
608<body> 603<body>
609 604
610<p> 605<p>
611So 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
612and deletions, and use insert mode. With everything listed on the 607and deletions, and use insert mode. With everything listed on the
613cheat 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
614task. 609task.
615</p> 610</p>
616 611
617<p> 612<p>
618However, vi also has many more powerful commands. In this section, 613However, vi also has many more powerful commands. In this section,
619you'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>,
620<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.
621These commands will help make vi more fun and productive. 616These commands will help make vi more fun and productive.
622</p> 617</p>
623 618
626<section> 621<section>
627<title>Visual mode</title> 622<title>Visual mode</title>
628<body> 623<body>
629 624
630<p> 625<p>
631The best way to cut and paste is to use visual mode, a special mode that 626The best way to cut and paste is to use <e>visual mode</e>, a special mode that
632has 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
633of 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,
634it 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
635can highlight by simply dragging the left mouse button over a particular 630can highlight by simply dragging the left mouse button over a particular
636region: 631region:
637</p> 632</p>
638 633
639<figure 634<figure link="/images/vihighlight.png" short="Highlighted text" caption="VIM
640link="http://www.ibiblio.org/web-gentoo/images/vihighlight.png" 635with highlighted text"/>
641short="Highlighted text" caption="VIM with highlighted text"/>
642 636
643<p> 637<p>
644In 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
645may 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,
646by moving the cursor using movement commands (typically the arrow 640by moving the cursor using movement commands (typically the arrow
647keys), you'll be able to highlight a region of text. Once 641keys), you'll be able to highlight a region of text. Once
648highlighted, we are ready to cut or copy the text. 642highlighted, we are ready to cut or copy the text.
649</p> 643</p>
650 644
651<p> 645<p>
652If 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").
653If 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
654command 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
655the cut or copied text, and hit <c>P</c> to insert after the cursor, 649the cut or copied text, and hit <c>P</c> to insert before the cursor,
656or <c>p</c> to insert before the cursor. Voila, the cut/copy and 650or <c>p</c> to insert after the cursor. Voila, the cut/copy and
657paste is complete! Test out several copy/cut and paste operations 651paste is complete! Test out several copy/cut and paste operations
658before advancing to the next section. 652before advancing to the next section.
659</p> 653</p>
660 654
661</body> 655</body>
662</section> 656</section>
663<section> 657<section>
664<title>Replacing text</title> 658<title>Replacing text</title>
665<body> 659<body>
666 660
667<p> 661<p>
668To 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
669replace the first pattern that appears on the current line, type 663replace the first pattern that appears on the current line, type
670<c>:s/&lt;regexp&gt;/&lt;replacement&gt;/</c> and hit 664<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 665<c>&lt;ENTER&gt;</c>, where &lt;regexp&gt; is the pattern you'd like
672to match and &lt;replacement&gt; is the replacement string. To 666to match and &lt;replacement&gt; is the replacement string. To
673replace all matches on the current line, type 667replace all matches on the current line, type
674<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
675replace every occurence of this pattern in your file (normally what 669replace every occurence of this pattern in your file (normally what
676you 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
677you'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
678change, 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
679for "confirm") and hit <c>&lt;ENTER&gt;</c>. 673for "confirm") and hit <c>&lt;ENTER&gt;</c>.
680</p> 674</p>
681 675
691when 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).
692When 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>
693(control-d) to move one indent level to the left, and 687(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. 688<c>&lt;CTR&gt;t</c> (control-t) to move one indent level to the right.
695If autoindent wasn't enabled automatically, you can manually enable it 689If autoindent wasn't enabled automatically, you can manually enable it
696by typing in the <e>ex</e> command <c>:set</c> autoindent. You can 690by typing in the <e>ex</e> command <c>:set autoindent</c>. You can
697also 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
698<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.
699</p> 693</p>
700 694
701</body> 695</body>
703<section> 697<section>
704<title>Our final cheat sheet</title> 698<title>Our final cheat sheet</title>
705<body> 699<body>
706 700
707<p> 701<p>
708Well, 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
709advanced editing commands to your cheat sheet, it should look like 703advanced editing commands to your cheat sheet, it should look like
710this: 704this:
711</p> 705</p>
712 706
713<figure 707<figure link="/images/vicheat-final.png" short="Cheat Sheet" caption="Final
714link="http://www.ibiblio.org/web-gentoo/images/vicheat-final.png" 708cheet sheet"/>
715short="Cheat Sheet" caption="Final cheet sheet"/>
716 709
717<p> 710<p>
718Keep 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
719compose emails. Refer to the cheat sheet when needed; you'll find 712compose emails. Refer to the cheat sheet when needed; you'll find
720that within the week, you'll have nearly all the commands memorized 713that within the week, you'll have nearly all the commands memorized
721and your vi productivity will shoot through the roof! 714and your vi productivity will shoot through the roof!
722</p> 715</p>
723 716
717<p>
718If you want to use vi as your default editor, use <c>eselect</c>:
719</p>
720
721<pre caption="Setting vi as the default editor">
722~# <i>eselect editor list</i>
723Available targets for the EDITOR variable:
724 [1] /bin/nano
725 [2] /bin/ed
726 [3] /usr/bin/ex
727 [4] /usr/bin/vi
728 [ ] (free form)
729
730~# <i>eselect editor set 4</i>
731Setting EDITOR to /usr/bin/vi ...
732>>> Regenerating /etc/ld.so.cache...
733Run ". /etc/profile" to update the variable in your shell.
734</pre>
735
736<p>
737If you want to set this for your user only instead of system wide, set the
738<c>EDITOR</c> variable in your <path>~/.bashrc</path>:
739</p>
740
741<pre caption="Setting vi as the users' editor">
742~$ <i>vi ~/.bashrc</i>
743<comment>(...)</comment>
744export EDITOR="/usr/bin/vi"
745</pre>
746
724</body> 747</body>
725</section> 748</section>
726<section> 749<section>
727<title>Resources</title> 750<title>Resources</title>
728<body> 751<body>
732more about vi: 755more about vi:
733</p> 756</p>
734 757
735<ul> 758<ul>
736 <li> 759 <li>
737 <uri link="http://www.thomer.com/thomer/vi/vi.html">The vi Lovers 760 <uri link="http://www.thomer.com/thomer/vi/vi.html">The vi Lovers
738 Home Page</uri>, an excellent resource for all things vi 761 Home Page</uri>, an excellent resource for all things vi
739 </li> 762 </li>
740 <li> 763 <li>
741 <uri link="http://www.vim.org/">The vim homepage</uri> is the place 764 <uri link="http://www.vim.org/">The vim homepage</uri> is the place
742 to go for all your vim needs 765 to go for all your vim needs
743 </li> 766 </li>
744 <li> 767 <li>
745 If you're looking for a good, old-fashioned book, <uri 768 If you're looking for a good, old-fashioned book, <uri
746 link="http://www.oreilly.com/catalog/vi6/">Learning the vi Editor, 769 link="http://www.oreilly.com/catalog/vi6/">Learning the vi Editor,
747 6th Edition</uri> would be an excellent choice. Contains good 770 6th Edition</uri> would be an excellent choice. Contains good
748 coverage of vi and vi clones. 771 coverage of vi and vi clones.
749 </li> 772 </li>
750</ul> 773</ul>
751 774
752</body> 775</body>

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.18

  ViewVC Help
Powered by ViewVC 1.1.20