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