1702
|
1 *usr_04.txt* For Vim version 7.2. Last change: 2006 Jun 21
|
7
|
2
|
|
3 VIM USER MANUAL - by Bram Moolenaar
|
|
4
|
|
5 Making small changes
|
|
6
|
|
7
|
|
8 This chapter shows you several ways of making corrections and moving text
|
|
9 around. It teaches you the three basic ways to change text: operator-motion,
|
|
10 Visual mode and text objects.
|
|
11
|
|
12 |04.1| Operators and motions
|
|
13 |04.2| Changing text
|
|
14 |04.3| Repeating a change
|
|
15 |04.4| Visual mode
|
|
16 |04.5| Moving text
|
|
17 |04.6| Copying text
|
|
18 |04.7| Using the clipboard
|
|
19 |04.8| Text objects
|
|
20 |04.9| Replace mode
|
|
21 |04.10| Conclusion
|
|
22
|
|
23 Next chapter: |usr_05.txt| Set your settings
|
|
24 Previous chapter: |usr_03.txt| Moving around
|
|
25 Table of contents: |usr_toc.txt|
|
|
26
|
|
27 ==============================================================================
|
|
28 *04.1* Operators and motions
|
|
29
|
|
30 In chapter 2 you learned the "x" command to delete a single character. And
|
|
31 using a count: "4x" deletes four characters.
|
|
32 The "dw" command deletes a word. You may recognize the "w" command as the
|
|
33 move word command. In fact, the "d" command may be followed by any motion
|
|
34 command, and it deletes from the current location to the place where the
|
|
35 cursor winds up.
|
|
36 The "4w" command, for example, moves the cursor over four words. The d4w
|
|
37 command deletes four words.
|
|
38
|
|
39 To err is human. To really foul up you need a computer. ~
|
|
40 ------------------>
|
|
41 d4w
|
|
42
|
|
43 To err is human. you need a computer. ~
|
|
44
|
|
45 Vim only deletes up to the position where the motion takes the cursor. That's
|
|
46 because Vim knows that you probably don't want to delete the first character
|
|
47 of a word. If you use the "e" command to move to the end of a word, Vim
|
|
48 guesses that you do want to include that last character:
|
|
49
|
|
50 To err is human. you need a computer. ~
|
|
51 -------->
|
|
52 d2e
|
|
53
|
|
54 To err is human. a computer. ~
|
|
55
|
|
56 Whether the character under the cursor is included depends on the command you
|
|
57 used to move to that character. The reference manual calls this "exclusive"
|
|
58 when the character isn't included and "inclusive" when it is.
|
|
59
|
|
60 The "$" command moves to the end of a line. The "d$" command deletes from the
|
|
61 cursor to the end of the line. This is an inclusive motion, thus the last
|
|
62 character of the line is included in the delete operation:
|
|
63
|
|
64 To err is human. a computer. ~
|
|
65 ------------>
|
|
66 d$
|
|
67
|
|
68 To err is human ~
|
|
69
|
|
70 There is a pattern here: operator-motion. You first type an operator command.
|
|
71 For example, "d" is the delete operator. Then you type a motion command like
|
|
72 "4l" or "w". This way you can operate on any text you can move over.
|
|
73
|
|
74 ==============================================================================
|
|
75 *04.2* Changing text
|
|
76
|
|
77 Another operator is "c", change. It acts just like the "d" operator, except
|
|
78 it leaves you in Insert mode. For example, "cw" changes a word. Or more
|
|
79 specifically, it deletes a word and then puts you in Insert mode.
|
|
80
|
|
81 To err is human ~
|
|
82 ------->
|
|
83 c2wbe<Esc>
|
|
84
|
|
85 To be human ~
|
|
86
|
|
87 This "c2wbe<Esc>" contains these bits:
|
|
88
|
|
89 c the change operator
|
|
90 2w move two words (they are deleted and Insert mode started)
|
|
91 be insert this text
|
|
92 <Esc> back to Normal mode
|
|
93
|
|
94 If you have paid attention, you will have noticed something strange: The space
|
|
95 before "human" isn't deleted. There is a saying that for every problem there
|
|
96 is an answer that is simple, clear, and wrong. That is the case with the
|
|
97 example used here for the "cw" command. The c operator works just like the
|
|
98 d operator, with one exception: "cw". It actually works like "ce", change to
|
|
99 end of word. Thus the space after the word isn't included. This is an
|
|
100 exception that dates back to the old Vi. Since many people are used to it
|
|
101 now, the inconsistency has remained in Vim.
|
|
102
|
|
103
|
|
104 MORE CHANGES
|
|
105
|
|
106 Like "dd" deletes a whole line, "cc" changes a whole line. It keeps the
|
|
107 existing indent (leading white space) though.
|
|
108
|
|
109 Just like "d$" deletes until the end of the line, "c$" changes until the end
|
|
110 of the line. It's like doing "d$" to delete the text and then "a" to start
|
|
111 Insert mode and append new text.
|
|
112
|
|
113
|
|
114 SHORTCUTS
|
|
115
|
|
116 Some operator-motion commands are used so often that they have been given a
|
|
117 single letter command:
|
|
118
|
|
119 x stands for dl (delete character under the cursor)
|
|
120 X stands for dh (delete character left of the cursor)
|
|
121 D stands for d$ (delete to end of the line)
|
|
122 C stands for c$ (change to end of the line)
|
|
123 s stands for cl (change one character)
|
|
124 S stands for cc (change a whole line)
|
|
125
|
|
126
|
|
127 WHERE TO PUT THE COUNT
|
|
128
|
|
129 The commands "3dw" and "d3w" delete three words. If you want to get really
|
|
130 picky about things, the first command, "3dw", deletes one word three times;
|
|
131 the command "d3w" deletes three words once. This is a difference without a
|
|
132 distinction. You can actually put in two counts, however. For example,
|
|
133 "3d2w" deletes two words, repeated three times, for a total of six words.
|
|
134
|
|
135
|
|
136 REPLACING WITH ONE CHARACTER
|
|
137
|
|
138 The "r" command is not an operator. It waits for you to type a character, and
|
|
139 will replace the character under the cursor with it. You could do the same
|
|
140 with "cl" or with the "s" command, but with "r" you don't have to press <Esc>
|
|
141
|
|
142 there is somerhing grong here ~
|
|
143 rT rt rw
|
|
144
|
|
145 There is something wrong here ~
|
|
146
|
|
147 Using a count with "r" causes that many characters to be replaced with the
|
|
148 same character. Example:
|
|
149
|
|
150 There is something wrong here ~
|
|
151 5rx
|
|
152
|
|
153 There is something xxxxx here ~
|
|
154
|
|
155 To replace a character with a line break use "r<Enter>". This deletes one
|
|
156 character and inserts a line break. Using a count here only applies to the
|
|
157 number of characters deleted: "4r<Enter>" replaces four characters with one
|
|
158 line break.
|
|
159
|
|
160 ==============================================================================
|
|
161 *04.3* Repeating a change
|
|
162
|
|
163 The "." command is one of the most simple yet powerful commands in Vim. It
|
|
164 repeats the last change. For instance, suppose you are editing an HTML file
|
|
165 and want to delete all the <B> tags. You position the cursor on the first <
|
|
166 and delete the <B> with the command "df>". You then go to the < of the next
|
|
167 </B> and kill it using the "." command. The "." command executes the last
|
|
168 change command (in this case, "df>"). To delete another tag, position the
|
|
169 cursor on the < and use the "." command.
|
|
170
|
|
171 To <B>generate</B> a table of <B>contents ~
|
|
172 f< find first < --->
|
|
173 df> delete to > -->
|
|
174 f< find next < --------->
|
|
175 . repeat df> --->
|
|
176 f< find next < ------------->
|
|
177 . repeat df> -->
|
|
178
|
|
179 The "." command works for all changes you make, except for the "u" (undo),
|
|
180 CTRL-R (redo) and commands that start with a colon (:).
|
|
181
|
|
182 Another example: You want to change the word "four" to "five". It appears
|
|
183 several times in your text. You can do this quickly with this sequence of
|
|
184 commands:
|
|
185
|
|
186 /four<Enter> find the first string "four"
|
|
187 cwfive<Esc> change the word to "five"
|
|
188 n find the next "four"
|
|
189 . repeat the change to "five'
|
|
190 n find the next "four"
|
|
191 . repeat the change
|
|
192 etc.
|
|
193
|
|
194 ==============================================================================
|
|
195 *04.4* Visual mode
|
|
196
|
|
197 To delete simple items the operator-motion changes work quite well. But often
|
|
198 it's not so easy to decide which command will move over the text you want to
|
|
199 change. Then you can use Visual mode.
|
|
200
|
|
201 You start Visual mode by pressing "v". You move the cursor over the text you
|
|
202 want to work on. While you do this, the text is highlighted. Finally type
|
|
203 the operator command.
|
|
204 For example, to delete from halfway one word to halfway another word:
|
|
205
|
|
206 This is an examination sample of visual mode ~
|
|
207 ---------->
|
|
208 velllld
|
|
209
|
|
210 This is an example of visual mode ~
|
|
211
|
|
212 When doing this you don't really have to count how many times you have to
|
|
213 press "l" to end up in the right position. You can immediately see what text
|
|
214 will be deleted when you press "d".
|
|
215
|
|
216 If at any time you decide you don't want to do anything with the highlighted
|
|
217 text, just press <Esc> and Visual mode will stop without doing anything.
|
|
218
|
|
219
|
|
220 SELECTING LINES
|
|
221
|
|
222 If you want to work on whole lines, use "V" to start Visual mode. You will
|
|
223 see right away that the whole line is highlighted, without moving around.
|
|
224 When you move left or right nothing changes. When you move up or down the
|
|
225 selection is extended whole lines at a time.
|
|
226 For example, select three lines with "Vjj":
|
|
227
|
|
228 +------------------------+
|
|
229 | text more text |
|
|
230 >> | more text more text | |
|
|
231 selected lines >> | text text text | | Vjj
|
|
232 >> | text more | V
|
|
233 | more text more |
|
|
234 +------------------------+
|
|
235
|
|
236
|
|
237 SELECTING BLOCKS
|
|
238
|
|
239 If you want to work on a rectangular block of characters, use CTRL-V to start
|
|
240 Visual mode. This is very useful when working on tables.
|
|
241
|
|
242 name Q1 Q2 Q3
|
|
243 pierre 123 455 234
|
|
244 john 0 90 39
|
|
245 steve 392 63 334
|
|
246
|
|
247 To delete the middle "Q2" column, move the cursor to the "Q" of "Q2". Press
|
|
248 CTRL-V to start blockwise Visual mode. Now move the cursor three lines down
|
|
249 with "3j" and to the next word with "w". You can see the first character of
|
|
250 the last column is included. To exclude it, use "h". Now press "d" and the
|
|
251 middle column is gone.
|
|
252
|
|
253
|
|
254 GOING TO THE OTHER SIDE
|
|
255
|
|
256 If you have selected some text in Visual mode, and discover that you need to
|
|
257 change the other end of the selection, use the "o" command (Hint: o for other
|
|
258 end). The cursor will go to the other end, and you can move the cursor to
|
|
259 change where the selection starts. Pressing "o" again brings you back to the
|
|
260 other end.
|
|
261
|
|
262 When using blockwise selection, you have four corners. "o" only takes you to
|
|
263 one of the other corners, diagonally. Use "O" to move to the other corner in
|
|
264 the same line.
|
|
265
|
|
266 Note that "o" and "O" in Visual mode work very different from Normal mode,
|
|
267 where they open a new line below or above the cursor.
|
|
268
|
|
269 ==============================================================================
|
|
270 *04.5* Moving text
|
|
271
|
|
272 When you delete something with the "d", "x", or another command, the text is
|
237
|
273 saved. You can paste it back by using the p command. (The Vim name for
|
7
|
274 this is put).
|
|
275 Take a look at how this works. First you will delete an entire line, by
|
|
276 putting the cursor on the line you want to delete and typing "dd". Now you
|
|
277 move the cursor to where you want to put the line and use the "p" (put)
|
|
278 command. The line is inserted on the line below the cursor.
|
|
279
|
|
280 a line a line a line
|
|
281 line 2 dd line 3 p line 3
|
|
282 line 3 line 2
|
|
283
|
|
284 Because you deleted an entire line, the "p" command placed the text line below
|
|
285 the cursor. If you delete part of a line (a word, for instance), the "p"
|
|
286 command puts it just after the cursor.
|
|
287
|
|
288 Some more boring try text to out commands. ~
|
|
289 ---->
|
|
290 dw
|
|
291
|
|
292 Some more boring text to out commands. ~
|
|
293 ------->
|
|
294 welp
|
|
295
|
|
296 Some more boring text to try out commands. ~
|
|
297
|
|
298
|
|
299 MORE ON PUTTING
|
|
300
|
|
301 The "P" command puts text like "p", but before the cursor. When you deleted a
|
|
302 whole line with "dd", "P" will put it back above the cursor. When you deleted
|
|
303 a word with "dw", "P" will put it back just before the cursor.
|
|
304
|
|
305 You can repeat putting as many times as you like. The same text will be used.
|
|
306
|
|
307 You can use a count with "p" and "P". The text will be repeated as many times
|
|
308 as specified with the count. Thus "dd" and then "3p" puts three copies of the
|
|
309 same deleted line.
|
|
310
|
|
311
|
|
312 SWAPPING TWO CHARACTERS
|
|
313
|
|
314 Frequently when you are typing, your fingers get ahead of your brain (or the
|
|
315 other way around?). The result is a typo such as "teh" for "the". Vim
|
|
316 makes it easy to correct such problems. Just put the cursor on the e of "teh"
|
|
317 and execute the command "xp". This works as follows: "x" deletes the
|
|
318 character e and places it in a register. "p" puts the text after the cursor,
|
|
319 which is after the h.
|
|
320
|
|
321 teh th the ~
|
|
322 x p
|
|
323
|
|
324 ==============================================================================
|
|
325 *04.6* Copying text
|
|
326
|
|
327 To copy text from one place to another, you could delete it, use "u" to undo
|
|
328 the deletion and then "p" to put it somewhere else. There is an easier way:
|
|
329 yanking. The "y" operator copies text into a register. Then a "p" command
|
|
330 can be used to put it.
|
|
331 Yanking is just a Vim name for copying. The "c" letter was already used
|
|
332 for the change operator, and "y" was still available. Calling this
|
|
333 operator "yank" made it easier to remember to use the "y" key.
|
|
334
|
|
335 Since "y" is an operator, you use "yw" to yank a word. A count is possible as
|
|
336 usual. To yank two words use "y2w". Example:
|
|
337
|
|
338 let sqr = LongVariable * ~
|
|
339 -------------->
|
|
340 y2w
|
|
341
|
|
342 let sqr = LongVariable * ~
|
|
343 p
|
|
344
|
|
345 let sqr = LongVariable * LongVariable ~
|
|
346
|
|
347 Notice that "yw" includes the white space after a word. If you don't want
|
|
348 this, use "ye".
|
|
349
|
|
350 The "yy" command yanks a whole line, just like "dd" deletes a whole line.
|
|
351 Unexpectedly, while "D" deletes from the cursor to the end of the line, "Y"
|
|
352 works like "yy", it yanks the whole line. Watch out for this inconsistency!
|
|
353 Use "y$" to yank to the end of the line.
|
|
354
|
|
355 a text line yy a text line a text line
|
|
356 line 2 line 2 p line 2
|
|
357 last line last line a text line
|
|
358 last line
|
|
359
|
|
360 ==============================================================================
|
|
361 *04.7* Using the clipboard
|
|
362
|
|
363 If you are using the GUI version of Vim (gvim), you can find the "Copy" item
|
|
364 in the "Edit" menu. First select some text with Visual mode, then use the
|
|
365 Edit/Copy menu. The selected text is now copied to the clipboard. You can
|
|
366 paste the text in other programs. In Vim itself too.
|
|
367
|
|
368 If you have copied text to the clipboard in another application, you can paste
|
|
369 it in Vim with the Edit/Paste menu. This works in Normal mode and Insert
|
|
370 mode. In Visual mode the selected text is replaced with the pasted text.
|
|
371
|
|
372 The "Cut" menu item deletes the text before it's put on the clipboard. The
|
|
373 "Copy", "Cut" and "Paste" items are also available in the popup menu (only
|
|
374 when there is a popup menu, of course). If your Vim has a toolbar, you can
|
|
375 also find these items there.
|
|
376
|
|
377 If you are not using the GUI, or if you don't like using a menu, you have to
|
|
378 use another way. You use the normal "y" (yank) and "p" (put) commands, but
|
|
379 prepend "* (double-quote star) before it. To copy a line to the clipboard: >
|
|
380
|
|
381 "*yy
|
|
382
|
|
383 To put text from the clipboard back into the text: >
|
|
384
|
|
385 "*p
|
|
386
|
|
387 This only works on versions of Vim that include clipboard support. More about
|
|
388 the clipboard in section |09.3| and here: |clipboard|.
|
|
389
|
|
390 ==============================================================================
|
|
391 *04.8* Text objects
|
|
392
|
1125
|
393 If the cursor is in the middle of a word and you want to delete that word, you
|
7
|
394 need to move back to its start before you can do "dw". There is a simpler way
|
|
395 to do this: "daw".
|
|
396
|
|
397 this is some example text. ~
|
|
398 daw
|
|
399
|
|
400 this is some text. ~
|
|
401
|
|
402 The "d" of "daw" is the delete operator. "aw" is a text object. Hint: "aw"
|
|
403 stands for "A Word". Thus "daw" is "Delete A Word". To be precise, the white
|
|
404 space after the word is also deleted (the white space before the word at the
|
|
405 end of the line).
|
|
406
|
|
407 Using text objects is the third way to make changes in Vim. We already had
|
|
408 operator-motion and Visual mode. Now we add operator-text object.
|
|
409 It is very similar to operator-motion, but instead of operating on the text
|
|
410 between the cursor position before and after a movement command, the text
|
|
411 object is used as a whole. It doesn't matter where in the object the cursor
|
|
412 was.
|
|
413
|
|
414 To change a whole sentence use "cis". Take this text:
|
|
415
|
|
416 Hello there. This ~
|
|
417 is an example. Just ~
|
|
418 some text. ~
|
|
419
|
|
420 Move to the start of the second line, on "is an". Now use "cis":
|
|
421
|
|
422 Hello there. Just ~
|
|
423 some text. ~
|
|
424
|
|
425 The cursor is in between the blanks in the first line. Now you type the new
|
|
426 sentence "Another line.":
|
|
427
|
|
428 Hello there. Another line. Just ~
|
|
429 some text. ~
|
|
430
|
|
431 "cis" consists of the "c" (change) operator and the "is" text object. This
|
|
432 stands for "Inner Sentence". There is also the "as" (a sentence) object. The
|
|
433 difference is that "as" includes the white space after the sentence and "is"
|
|
434 doesn't. If you would delete a sentence, you want to delete the white space
|
|
435 at the same time, thus use "das". If you want to type new text the white
|
|
436 space can remain, thus you use "cis".
|
|
437
|
|
438 You can also use text objects in Visual mode. It will include the text object
|
|
439 in the Visual selection. Visual mode continues, thus you can do this several
|
|
440 times. For example, start Visual mode with "v" and select a sentence with
|
|
441 "as". Now you can repeat "as" to include more sentences. Finally you use an
|
|
442 operator to do something with the selected sentences.
|
|
443
|
|
444 You can find a long list of text objects here: |text-objects|.
|
|
445
|
|
446 ==============================================================================
|
|
447 *04.9* Replace mode
|
|
448
|
|
449 The "R" command causes Vim to enter replace mode. In this mode, each
|
|
450 character you type replaces the one under the cursor. This continues until
|
|
451 you type <Esc>.
|
|
452 In this example you start Replace mode on the first "t" of "text":
|
|
453
|
|
454 This is text. ~
|
|
455 Rinteresting.<Esc>
|
|
456
|
|
457 This is interesting. ~
|
|
458
|
|
459 You may have noticed that this command replaced 5 characters in the line with
|
237
|
460 twelve others. The "R" command automatically extends the line if it runs out
|
7
|
461 of characters to replace. It will not continue on the next line.
|
|
462
|
|
463 You can switch between Insert mode and Replace mode with the <Insert> key.
|
|
464
|
|
465 When you use <BS> (backspace) to make correction, you will notice that the
|
|
466 old text is put back. Thus it works like an undo command for the last typed
|
|
467 character.
|
|
468
|
|
469 ==============================================================================
|
|
470 *04.10* Conclusion
|
|
471
|
|
472 The operators, movement commands and text objects give you the possibility to
|
|
473 make lots of combinations. Now that you know how it works, you can use N
|
|
474 operators with M movement commands to make N * M commands!
|
|
475
|
|
476 You can find a list of operators here: |operator|
|
|
477
|
|
478 For example, there are many other ways to delete pieces of text. Here are a
|
|
479 few often used ones:
|
|
480
|
|
481 x delete character under the cursor (short for "dl")
|
|
482 X delete character before the cursor (short for "dh")
|
|
483 D delete from cursor to end of line (short for "d$")
|
|
484 dw delete from cursor to next start of word
|
|
485 db delete from cursor to previous start of word
|
|
486 diw delete word under the cursor (excluding white space)
|
|
487 daw delete word under the cursor (including white space)
|
|
488 dG delete until the end of the file
|
|
489 dgg delete until the start of the file
|
|
490
|
|
491 If you use "c" instead of "d" they become change commands. And with "y" you
|
|
492 yank the text. And so forth.
|
|
493
|
|
494
|
|
495 There are a few often used commands to make changes that didn't fit somewhere
|
|
496 else:
|
|
497
|
|
498 ~ change case of the character under the cursor, and move the
|
|
499 cursor to the next character. This is not an operator (unless
|
|
500 'tildeop' is set), thus you can't use it with a motion
|
|
501 command. It does works in Visual mode and changes case for
|
|
502 all the selected text then.
|
|
503
|
|
504 I Start Insert mode after moving the cursor to the first
|
|
505 non-blank in the line.
|
|
506
|
|
507 A Start Insert mode after moving the cursor to the end of the
|
|
508 line.
|
|
509
|
|
510 ==============================================================================
|
|
511
|
|
512 Next chapter: |usr_05.txt| Set your settings
|
|
513
|
|
514 Copyright: see |manual-copyright| vim:tw=78:ts=8:ft=help:norl:
|