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