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