/[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.12 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.12 2004/08/26 17:39:17 swift 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.5</version> 23<version>2</version>
24<date>August 26, 2004</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>
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="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$ <i>vi myfile.txt</i> 133$ <i>vi myfile.txt</i>
135</pre> 134</pre>
136 135
137<p> 136<p>
138If 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
139use 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
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 <e>command 151editors, when vi starts up, it is in a special mode called <e>command
153mode</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
154inserting 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
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 <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
208purpose. 207purpose.
209</p> 208</p>
210 209
211</body> 210</body>
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>
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 <c>foo-bar-oni</c> 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. <c>foo-bar-oni</c> is therefore considered five 234<e>or</e> punctuation. <c>foo-bar-oni</c> is therefore considered five
236words: <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>.
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 <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
244one vi bigword. 243one vi bigword.
245</p> 244</p>
246 245
247</body> 246</body>
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>
284<section> 283<section>
285<body> 284<body>
286 285
287<p> 286<p>
288We've covered the basic movement commands, but there are another 287We've covered the basic movement commands, but there are another
289couple 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
290vi. If this doesn't work, then you probably accidentally modified the 289vi. If this doesn't work, then you probably accidentally modified the
291file 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
292<c>:q!</c>. You should now be at the command prompt. 291<c>:q!</c>. You should now be at the command prompt.
293</p> 292</p>
294 293
295<p> 294<p>
296In 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
297<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
298editor 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
299line-based editing operations. In addition, it can also be used to 298line-based editing operations. In addition, it can also be used to
300quit, 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
301command 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
302to 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
303scroll 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,
304simply type vi and hit enter. 303simply type vi and hit enter.
305</p> 304</p>
306 305
307</body> 306</body>
308</section> 307</section>
314<title>The beginnings of the cheat sheet</title> 313<title>The beginnings of the cheat sheet</title>
315<body> 314<body>
316 315
317<p> 316<p>
318We'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
319our 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
320sized 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
321sheet!) 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
322the 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
323possible so that we can fit everything on one sheet. 322possible so that we can fit everything on one sheet.
324</p> 323</p>
325 324
326<figure 325<figure link="/images/vicheat-first.png" short="First part of the cheat sheet"
327link="http://www.ibiblio.org/web-gentoo/images/vicheat-first.png" 326caption="Cheat Sheet"/>
328short="First part of the cheat sheet" caption="Cheat Sheet"/>
329 327
330</body> 328</body>
331</section> 329</section>
332<section> 330<section>
333<title>Miscellaneous vi</title> 331<title>Miscellaneous vi</title>
334<body> 332<body>
335 333
336<p> 334<p>
337Let's continue our rapid command-covering pace. In command-mode, you 335Let's continue our rapid command-covering pace. In command-mode, you
338can 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
339line 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.
340</p> 338</p>
341 339
342<p> 340<p>
343If 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
344pattern, 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
345&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
346don't know how to use regular expressions, don't fret -- typing 344don't know how to use regular expressions, don't fret -- typing
347<c>/foo</c> will move to the next occurence of <e>foo</e>. The only 345<c>/foo</c> will move to the next occurence of <e>foo</e>. The only
348thing 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
349literal <c>^</c>, <c>.</c>, <c>$</c> or <c>\</c> characters. Prefix 347literal <c>^</c>, <c>.</c>, <c>$</c> or <c>\</c> characters. Prefix
350these 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
351example, <c>/foo\.gif</c> will search for the next occurence of 349example, <c>/foo\.gif</c> will search for the next occurence of
352"foo.gif". 350"foo.gif".
353</p> 351</p>
354 352
355<p> 353<p>
356To 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
357backwards, 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
358very 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
359search. 357search.
360</p> 358</p>
361 359
362</body> 360</body>
369<title>Save and save as...</title> 367<title>Save and save as...</title>
370<body> 368<body>
371 369
372<p> 370<p>
373We'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
374from 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
375want 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>
376to 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
377<c>:x</c> or <c>:wq</c>. 375<c>:x</c> or <c>:wq</c>.
378</p> 376</p>
379 377
380<p> 378<p>
381In 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
382have 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,
383type <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
384for editing in a new split window. To switch between windows, type 382for editing in a new split window. To switch between windows, type
385<c>&lt;CTR&gt;w&lt;CTR&gt;w</c> (control-w twice). Any <c>:q</c>, 383<c>&lt;CTR&gt;w&lt;CTR&gt;w</c> (control-w twice). Any <c>:q</c>,
386<c>:q!</c>, <c>:w</c> and <c>:x</c> commands that you enter will only 384<c>:q!</c>, <c>:w</c> and <c>:x</c> commands that you enter will only
387be applied to the currently-active window. 385be applied to the currently-active window.
388</p> 386</p>
389 387
390</body> 388</body>
394<body> 392<body>
395 393
396<p> 394<p>
397Now, 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.
398The 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
399commands keep you in command mode. The more complex editing commands 397commands keep you in command mode. The more complex editing commands
400automatically put you into insert mode -- a mode that allows you to 398automatically put you into insert mode -- a mode that allows you to
401enter 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.
402</p> 400</p>
403 401
404<p> 402<p>
405For now, try moving over some characters and hitting <c>x</c> 403For now, try moving over some characters and hitting <c>x</c>
406repeatedly. You'll see that <c>x</c> will delete the current 404repeatedly. You'll see that <c>x</c> will delete the current
407character under the cursor. Now, move to the middle of the paragraph 405character under the cursor. Now, move to the middle of the paragraph
408somewhere 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
409see 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
410end of the current line. Now, move over a character and hit 408end of the current line. Now, move over a character and hit
411<c>r</c>, and then type in a new character; you'll see that the 409<c>r</c>, and then type in a new character; you'll see that the
412original character has been replaced. Finally, move to any line in 410original character has been replaced. Finally, move to any line in
413the 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
414current line of text. 412current line of text.
415</p> 413</p>
416 414
417</body> 415</body>
418</section> 416</section>
419<section> 417<section>
420<title>Repeating and deleting</title> 418<title>Repeating and deleting</title>
421<body> 419<body>
422 420
423<p> 421<p>
424You 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
425you 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
426lines, 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
427with another handy shortcut. 425with another handy shortcut.
428</p> 426</p>
429 427
430<p> 428<p>
431To 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
432any movement command. For example, <c>dw</c> will delete from the 430any movement command. For example, <c>dw</c> will delete from the
433current 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
434delete 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
435delete the remainder of the paragraph. Experiment with the 433delete the remainder of the paragraph. Experiment with the
436<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
437comfortable with them. 435comfortable with them.
438</p> 436</p>
439 437
440</body> 438</body>
443<title>Undo!</title> 441<title>Undo!</title>
444<body> 442<body>
445 443
446<p> 444<p>
447Now 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
448learn 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
449version 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
450versions 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
451continue 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>
452and <c>u</c> commands together. 450and <c>u</c> commands together.
453</p> 451</p>
454 452
455</body> 453</body>
456</section> 454</section>
457<section> 455<section>
458<title>Updating the cheat sheet</title> 456<title>Updating the cheat sheet</title>
459<body> 457<body>
460 458
461<p> 459<p>
462Time 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
463covered so far, your cheat sheet should look like this: 461covered so far, your cheat sheet should look like this:
464</p> 462</p>
465 463
466<figure 464<figure link="/images/vicheat-edit.png" short="Cheat sheet" caption="Cheat
467link="http://www.ibiblio.org/web-gentoo/images/vicheat-edit.png" 465sheet with editing commands"/>
468short="Cheat sheet" caption="Cheat sheet with editing commands"/>
469 466
470</body> 467</body>
471</section> 468</section>
472</chapter> 469</chapter>
473 470
476<section> 473<section>
477<body> 474<body>
478 475
479<p> 476<p>
480So 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
481perform basic editing operations. However, I still haven't shown you 478perform basic editing operations. However, I still haven't shown you
482how to actually type in free-form text! This was intentional, because 479how to actually type in free-form text! This was intentional, because
483vi'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
484become comfortable with insert mode, its complexity (and flexibility) 481become comfortable with insert mode, its complexity (and flexibility)
485will become an asset. 482will become an asset.
486</p> 483</p>
487 484
488<p> 485<p>
489In 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
490just 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
491your 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
492can 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
493<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
494character, 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>
495after the current character. Remember, after you enter your text, hit 492after the current character. Remember, after you enter your text, hit
496<c>&lt;ESC&gt;</c> to return to command mode. 493<c>&lt;ESC&gt;</c> to return to command mode.
497</p> 494</p>
498 495
499</body> 496</body>
500</section> 497</section>
501<section> 498<section>
502<title>Benefits of insert mode</title> 499<title>Benefits of insert mode</title>
503<body> 500<body>
504 501
505<p> 502<p>
506Go 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
507<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
508to 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
509<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
510and 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
511works. 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
512perform significant editing steps without repeatedly entering and 509perform significant editing steps without repeatedly entering and
513leaving insert mode. 510leaving insert mode.
514</p> 511</p>
515 512
516</body> 513</body>
518<section> 515<section>
519<title>Insert options</title> 516<title>Insert options</title>
520<body> 517<body>
521 518
522<p> 519<p>
523Here 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>
524(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,
525regardless of your current position on the line. Likewise, press 522regardless of your current position on the line. Likewise, press
526<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
527the 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
528current 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>
529(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
530entire 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
531everything from the current position to the end of the line, type 528everything from the current position to the end of the line, type
532<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
533beginning of the line, type <c>c0</c>. 530beginning of the line, type <c>c0</c>.
534</p> 531</p>
535 532
536<p> 533<p>
537In addition to performing a special operation, every one of these 534In addition to performing a special operation, every one of these
538commands will put you into insert mode. After typing in your text, 535commands will put you into insert mode. After typing in your text,
539hit <c>&lt;ESC&gt;</c> to return to command mode. 536hit <c>&lt;ESC&gt;</c> to return to command mode.
540</p> 537</p>
541 538
542</body> 539</body>
543</section> 540</section>
545<title>Changing text</title> 542<title>Changing text</title>
546<body> 543<body>
547 544
548<p> 545<p>
549We'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
550typed <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
551of 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
552<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
553combination with a movement command. In this form, <c>c</c> works 550combination with a movement command. In this form, <c>c</c> works
554similarly 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
555that you can enter replacement text for the deleted region. Try 552that you can enter replacement text for the deleted region. Try
556combining 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
557your file (hint: <c>cW</c>, <c>ce</c>, <c>c(</c> .) 554your file (hint: <c>cW</c>, <c>ce</c>, <c>c(</c> .)
558</p> 555</p>
559 556
560</body> 557</body>
566<section> 563<section>
567<body> 564<body>
568 565
569<p> 566<p>
570vi <e>really</e> becomes powerful when you start using compound 567vi <e>really</e> becomes powerful when you start using compound
571("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
572these commands, you can also combine a number with any movement 569these commands, you can also combine a number with any movement
573command, 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
574the right. Here are some more movement "combo" command examples: 571the right. Here are some more movement "combo" command examples:
575<c>12b</c>, <c>4j</c>. 572<c>12b</c>, <c>4j</c>.
576</p> 573</p>
577 574
578<p> 575<p>
579vi, in addition to allowing (number)(movement command) combinations, 576vi, in addition to allowing (number)(movement command) combinations,
580also 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
581movement command. So, <c>d3w</c> will delete the next three words, 578movement command. So, <c>d3w</c> will delete the next three words,
582<c>d2j</c> will delete the current and next two lines, etc. Test out 579<c>d2j</c> will delete the current and next two lines, etc. Test out
583some <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
584and concise vi editing can be. Once these commands are second-nature, 581and concise vi editing can be. Once these commands are second-nature,
585you'll be able to edit files at blazing speed. 582you'll be able to edit files at blazing speed.
586</p> 583</p>
587 584
588</body> 585</body>
589</section> 586</section>
590<section> 587<section>
591<title>Updating the cheat sheet</title> 588<title>Updating the cheat sheet</title>
592<body> 589<body>
593 590
594<p> 591<p>
595Time 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
596far: 593far:
597</p> 594</p>
598 595
599<figure 596<figure link="/images/vicheat-compound.png" short="Cheat Sheet" caption="Cheat
600link="http://www.ibiblio.org/web-gentoo/images/vicheat-compound.png" 597sheet with compound commands"/>
601short="Cheat Sheet" caption="Cheat sheet with compound commands"/>
602 598
603</body> 599</body>
604</section> 600</section>
605<section> 601<section>
606<title>Productivity features</title> 602<title>Productivity features</title>
607<body> 603<body>
608 604
609<p> 605<p>
610So 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
611and deletions, and use insert mode. With everything listed on the 607and deletions, and use insert mode. With everything listed on the
612cheat 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
613task. 609task.
614</p> 610</p>
615 611
616<p> 612<p>
617However, vi also has many more powerful commands. In this section, 613However, vi also has many more powerful commands. In this section,
618you'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>,
619<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.
620These commands will help make vi more fun and productive. 616These commands will help make vi more fun and productive.
621</p> 617</p>
622 618
626<title>Visual mode</title> 622<title>Visual mode</title>
627<body> 623<body>
628 624
629<p> 625<p>
630The 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
631has 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
632of 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,
633it 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
634can highlight by simply dragging the left mouse button over a particular 630can highlight by simply dragging the left mouse button over a particular
635region: 631region:
636</p> 632</p>
637 633
638<figure 634<figure link="/images/vihighlight.png" short="Highlighted text" caption="VIM
639link="http://www.ibiblio.org/web-gentoo/images/vihighlight.png" 635with highlighted text"/>
640short="Highlighted text" caption="VIM with highlighted text"/>
641 636
642<p> 637<p>
643In 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
644may 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,
645by moving the cursor using movement commands (typically the arrow 640by moving the cursor using movement commands (typically the arrow
646keys), you'll be able to highlight a region of text. Once 641keys), you'll be able to highlight a region of text. Once
647highlighted, we are ready to cut or copy the text. 642highlighted, we are ready to cut or copy the text.
648</p> 643</p>
649 644
650<p> 645<p>
651If 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").
652If 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
653command 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
654the 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,
655or <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
656paste is complete! Test out several copy/cut and paste operations 651paste is complete! Test out several copy/cut and paste operations
657before advancing to the next section. 652before advancing to the next section.
658</p> 653</p>
659 654
660</body> 655</body>
661</section> 656</section>
662<section> 657<section>
663<title>Replacing text</title> 658<title>Replacing text</title>
664<body> 659<body>
665 660
666<p> 661<p>
667To 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
668replace the first pattern that appears on the current line, type 663replace the first pattern that appears on the current line, type
669<c>:s/&lt;regexp&gt;/&lt;replacement&gt;/</c> and hit 664<c>:s/&lt;regexp&gt;/&lt;replacement&gt;/</c> and hit
670<c>&lt;ENTER&gt;</c>, where &lt;regexp&gt; is the pattern you'd like 665<c>&lt;ENTER&gt;</c>, where &lt;regexp&gt; is the pattern you'd like
671to match and &lt;replacement&gt; is the replacement string. To 666to match and &lt;replacement&gt; is the replacement string. To
672replace all matches on the current line, type 667replace all matches on the current line, type
673<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
674replace every occurence of this pattern in your file (normally what 669replace every occurence of this pattern in your file (normally what
675you 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
676you'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
677change, 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
678for "confirm") and hit <c>&lt;ENTER&gt;</c>. 673for "confirm") and hit <c>&lt;ENTER&gt;</c>.
679</p> 674</p>
680 675
690when 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).
691When 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>
692(control-d) to move one indent level to the left, and 687(control-d) to move one indent level to the left, and
693<c>&lt;CTR&gt;t</c> (control-t) to move one indent level to the right. 688<c>&lt;CTR&gt;t</c> (control-t) to move one indent level to the right.
694If autoindent wasn't enabled automatically, you can manually enable it 689If autoindent wasn't enabled automatically, you can manually enable it
695by 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
696also 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
697<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.
698</p> 693</p>
699 694
700</body> 695</body>
702<section> 697<section>
703<title>Our final cheat sheet</title> 698<title>Our final cheat sheet</title>
704<body> 699<body>
705 700
706<p> 701<p>
707Well, 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
708advanced editing commands to your cheat sheet, it should look like 703advanced editing commands to your cheat sheet, it should look like
709this: 704this:
710</p> 705</p>
711 706
712<figure 707<figure link="/images/vicheat-final.png" short="Cheat Sheet" caption="Final
713link="http://www.ibiblio.org/web-gentoo/images/vicheat-final.png" 708cheet sheet"/>
714short="Cheat Sheet" caption="Final cheet sheet"/>
715 709
716<p> 710<p>
717Keep 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
718compose emails. Refer to the cheat sheet when needed; you'll find 712compose emails. Refer to the cheat sheet when needed; you'll find
719that within the week, you'll have nearly all the commands memorized 713that within the week, you'll have nearly all the commands memorized
720and your vi productivity will shoot through the roof! 714and your vi productivity will shoot through the roof!
721</p> 715</p>
722 716
723<p> 717<p>
724If you want to use vi as your default editor make the following change 718If you want to use vi as your default editor, use <c>eselect</c>:
725to <path>/etc/rc.conf</path>:
726</p> 719</p>
727 720
728<pre caption="Setting vi as the default editor"> 721<pre caption="Setting vi as the default editor">
729<comment>(Set EDITOR to your preferred editor.)</comment> 722~# <i>eselect editor list</i>
730#EDITOR="/bin/nano" 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>
731EDITOR="/usr/bin/vim" 744export EDITOR="/usr/bin/vi"
732#EDITOR="/usr/bin/emacs"
733</pre> 745</pre>
734 746
735</body> 747</body>
736</section> 748</section>
737<section> 749<section>
753 to go for all your vim needs 765 to go for all your vim needs
754 </li> 766 </li>
755 <li> 767 <li>
756 If you're looking for a good, old-fashioned book, <uri 768 If you're looking for a good, old-fashioned book, <uri
757 link="http://www.oreilly.com/catalog/vi6/">Learning the vi Editor, 769 link="http://www.oreilly.com/catalog/vi6/">Learning the vi Editor,
758 6th Edition</uri> would be an excellent choice. Contains good 770 6th Edition</uri> would be an excellent choice. Contains good
759 coverage of vi and vi clones. 771 coverage of vi and vi clones.
760 </li> 772 </li>
761</ul> 773</ul>
762 774
763</body> 775</body>

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

  ViewVC Help
Powered by ViewVC 1.1.20