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