usr_24.txt (20872B)
1 *usr_24.txt* Nvim 2 3 4 VIM USER MANUAL by Bram Moolenaar 5 6 7 Inserting quickly 8 9 10 When entering text, Vim offers various ways to reduce the number of keystrokes 11 and avoid typing mistakes. Use Insert mode completion to repeat previously 12 typed words. Abbreviate long words to short ones. Type characters that 13 aren't on your keyboard. 14 15 |24.1| Making corrections 16 |24.2| Showing matches 17 |24.3| Completion 18 |24.4| Repeating an insert 19 |24.5| Copying from another line 20 |24.6| Inserting a register 21 |24.7| Abbreviations 22 |24.8| Entering special characters 23 |24.9| Digraphs 24 |24.10| Normal mode commands 25 26 Next chapter: |usr_25.txt| Editing formatted text 27 Previous chapter: |usr_23.txt| Editing other files 28 Table of contents: |usr_toc.txt| 29 30 ============================================================================== 31 *24.1* Making corrections 32 33 The <BS> key was already mentioned. It deletes the character just before the 34 cursor. The <Del> key does the same for the character under (after) the 35 cursor. 36 When you typed a whole word wrong, use CTRL-W: 37 38 The horse had fallen to the sky ~ 39 CTRL-W 40 The horse had fallen to the ~ 41 42 If you really messed up a line and want to start over, use CTRL-U to delete 43 it. This keeps the text after the cursor and the indent. Only the text from 44 the first non-blank to the cursor is deleted. With the cursor on the "f" of 45 "fallen" in the next line pressing CTRL-U does this: 46 47 The horse had fallen to the ~ 48 CTRL-U 49 fallen to the ~ 50 51 When you spot a mistake a few words back, you need to move the cursor there to 52 correct it. For example, you typed this: 53 54 The horse had follen to the ground ~ 55 56 You need to change "follen" to "fallen". With the cursor at the end, you 57 would type this to correct it: > 58 59 <Esc>4blraA 60 61 < get out of Insert mode <Esc> 62 four words back 4b 63 move on top of the "o" l 64 replace with "a" ra 65 restart Insert mode A 66 67 Another way to do this: > 68 69 <C-Left><C-Left><C-Left><C-Left><Right><Del>a<End> 70 71 < four words back <C-Left><C-Left><C-Left><C-Left> 72 move on top of the "o" <Right> 73 delete the "o" <Del> 74 insert an "a" a 75 go to end of the line <End> 76 77 This uses special keys to move around, while remaining in Insert mode. This 78 resembles what you would do in a modeless editor. It's easier to remember, 79 but takes more time (you have to move your hand from the letters to the cursor 80 keys, and the <End> key is hard to press without looking at the keyboard). 81 These special keys are most useful when writing a mapping that doesn't 82 leave Insert mode. The extra typing doesn't matter then. 83 An overview of the keys you can use in Insert mode: 84 85 <C-Home> to start of the file 86 <PageUp> a whole screenful up 87 <Home> to start of line 88 <S-Left> one word left 89 <C-Left> one word left 90 <S-Right> one word right 91 <C-Right> one word right 92 <End> to end of the line 93 <PageDown> a whole screenful down 94 <C-End> to end of the file 95 96 There are a few more, see |ins-special-special|. 97 98 ============================================================================== 99 *24.2* Showing matches 100 101 When you type a ) it would be nice to see with which ( it matches. To make 102 Vim do that use this command: > 103 104 :set showmatch 105 106 When you now type a text like "(example)", as soon as you type the ) Vim will 107 briefly move the cursor to the matching (, keep it there for half a second, 108 and move back to where you were typing. 109 In case there is no matching (, Vim will beep. Then you know that you 110 might have forgotten the ( somewhere, or typed a ) too many. 111 The match will also be shown for [] and {} pairs. You don't have to wait 112 with typing the next character, as soon as Vim sees it the cursor will move 113 back and inserting continues as before. 114 You can change the time Vim waits with the 'matchtime' option. For 115 example, to make Vim wait one and a half second: > 116 117 :set matchtime=15 118 119 The time is specified in tenths of a second. 120 121 ============================================================================== 122 *24.3* Completion 123 124 Vim can automatically complete words on insertion. You type the first part of 125 a word, press CTRL-P, and Vim guesses the rest. 126 Suppose, for example, that you are creating a C program and want to type in 127 the following: 128 129 total = ch_array[0] + ch_array[1] + ch_array[2]; ~ 130 131 You start by entering the following: 132 133 total = ch_array[0] + ch_ ~ 134 135 At this point, you tell Vim to complete the word using the command CTRL-P. 136 Vim searches for a word that starts with what's in front of the cursor. In 137 this case, it is "ch_", which matches with the word ch_array. So typing 138 CTRL-P gives you the following: 139 140 total = ch_array[0] + ch_array ~ 141 142 After a little more typing, you get this (ending in a space): 143 144 total = ch_array[0] + ch_array[1] + ~ 145 146 If you now type CTRL-P Vim will search again for a word that completes the 147 word before the cursor. Since there is nothing in front of the cursor, it 148 finds the first word backwards, which is "ch_array". Typing CTRL-P again 149 gives you the next word that matches, in this case "total". A third CTRL-P 150 searches further back. If there is nothing else, it causes the editor to run 151 out of words, so it returns to the original text, which is nothing. A fourth 152 CTRL-P causes the editor to start over again with "ch_array". 153 154 To search forward, use CTRL-N. Since the search wraps around the end of the 155 file, CTRL-N and CTRL-P will find the same matches, but in a different 156 sequence. Hint: CTRL-N is Next-match and CTRL-P is Previous-match. 157 158 The Vim editor goes through a lot of effort to find words to complete. By 159 default, it searches the following places: 160 161 1. Current file 162 2. Files in other windows 163 3. Other loaded files (hidden buffers) 164 4. Files which are not loaded (inactive buffers) 165 5. Tag files 166 6. All files #included by the current file 167 168 169 OPTIONS 170 171 You can customize the search order with the 'complete' option. 172 173 The 'ignorecase' option is used. When it is set, case differences are ignored 174 when searching for matches. 175 176 A special option for completion is 'infercase'. This is useful to find 177 matches while ignoring case ('ignorecase' must be set) but still using the 178 case of the word typed so far. Thus if you type "For" and Vim finds a match 179 "fortunately", it will result in "Fortunately". 180 181 182 COMPLETING SPECIFIC ITEMS 183 184 If you know what you are looking for, you can use these commands to complete 185 with a certain type of item: 186 187 CTRL-X CTRL-F file names 188 CTRL-X CTRL-L whole lines 189 CTRL-X CTRL-D macro definitions (also in included files) 190 CTRL-X CTRL-I current and included files 191 CTRL-X CTRL-K words from a dictionary 192 CTRL-X CTRL-R contents from registers 193 CTRL-X CTRL-T words from a thesaurus 194 CTRL-X CTRL-] tags 195 CTRL-X CTRL-V Vim command line 196 197 After each of them CTRL-N can be used to find the next match, CTRL-P to find 198 the previous match. 199 More information for each of these commands here: |ins-completion|. 200 201 202 COMPLETING FILE NAMES 203 204 Let's take CTRL-X CTRL-F as an example. This will find file names. It scans 205 the current directory for files and displays each one that matches the word in 206 front of the cursor. 207 Suppose, for example, that you have the following files in the current 208 directory: 209 210 main.c sub_count.c sub_done.c sub_exit.c 211 212 Now enter Insert mode and start typing: 213 214 The exit code is in the file sub ~ 215 216 At this point, you enter the command CTRL-X CTRL-F. Vim now completes the 217 current word "sub" by looking at the files in the current directory. The 218 first match is sub_count.c. This is not the one you want, so you match the 219 next file by typing CTRL-N. This match is sub_done.c. Typing CTRL-N again 220 takes you to sub_exit.c. The results: 221 222 The exit code is in the file sub_exit.c ~ 223 224 If the file name starts with / (Unix) or C:\ (MS-Windows) you can find all 225 files in the file system. For example, type "/u" and CTRL-X CTRL-F. This 226 will match "/usr" (this is on Unix): 227 228 the file is found in /usr/ ~ 229 230 If you now press CTRL-N you go back to "/u". Instead, to accept the "/usr/" 231 and go one directory level deeper, use CTRL-X CTRL-F again: 232 233 the file is found in /usr/X11R6/ ~ 234 235 The results depend on what is found in your file system, of course. The 236 matches are sorted alphabetically. 237 238 239 COMPLETING IN SOURCE CODE 240 241 Source code files are well structured. That makes it possible to do 242 completion in an intelligent way. In Vim this is called Omni completion. In 243 some other editors it's called intellisense, but that is a trademark. 244 245 The key to Omni completion is CTRL-X CTRL-O. Obviously the O stands for Omni 246 here, so that you can remember it easier. Let's use an example for editing C 247 source: > 248 249 { 250 struct foo *p; 251 p-> 252 253 The cursor is after "p->". Now type CTRL-X CTRL-O. Vim will offer you a list 254 of alternatives, which are the items that "struct foo" contains. That is 255 quite different from using CTRL-P, which would complete any word, while only 256 members of "struct foo" are valid here. 257 258 For Omni completion to work you may need to do some setup. At least make sure 259 filetype plugins are enabled. Your vimrc file should contain a line like 260 this: > 261 filetype plugin on 262 Or: > 263 filetype plugin indent on 264 265 For C code you need to create a tags file and set the 'tags' option. That is 266 explained |ft-c-omni|. For other filetypes you may need to do something 267 similar, look below |compl-omni-filetypes|. It only works for specific 268 filetypes. Check the value of the 'omnifunc' option to find out if it would 269 work. 270 271 ============================================================================== 272 *24.4* Repeating an insert 273 274 If you press CTRL-A, the editor inserts the text you typed the last time you 275 were in Insert mode. 276 Assume, for example, that you have a file that begins with the following: > 277 278 "file.h" ~ 279 /* Main program begins */ ~ 280 281 You edit this file by inserting "#include " at the beginning of the first 282 line: > 283 284 #include "file.h" ~ 285 /* Main program begins */ ~ 286 287 You go down to the beginning of the next line using the commands "j^". You 288 now start to insert a new "#include" line. So you type: > 289 290 i CTRL-A 291 292 The result is as follows: > 293 294 #include "file.h" ~ 295 #include /* Main program begins */ ~ 296 297 The "#include " was inserted because CTRL-A inserts the text of the previous 298 insert. Now you type "main.h"<Enter> to finish the line: > 299 300 301 #include "file.h" ~ 302 #include "main.h" ~ 303 /* Main program begins */ ~ 304 305 The CTRL-@ command does a CTRL-A and then exits Insert mode. That's a quick 306 way of doing exactly the same insertion again. 307 308 ============================================================================== 309 *24.5* Copying from another line 310 311 The CTRL-Y command inserts the character above the cursor. This is useful 312 when you are duplicating a previous line. For example, you have this line of 313 C code: 314 315 b_array[i]->s_next = a_array[i]->s_next; ~ 316 317 Now you need to type the same line, but with "s_prev" instead of "s_next". 318 Start the new line, and press CTRL-Y 14 times, until you are at the "n" of 319 "next": 320 321 b_array[i]->s_next = a_array[i]->s_next; ~ 322 b_array[i]->s_ ~ 323 324 Now you type "prev": 325 326 b_array[i]->s_next = a_array[i]->s_next; ~ 327 b_array[i]->s_prev ~ 328 329 Continue pressing CTRL-Y until the following "next": 330 331 b_array[i]->s_next = a_array[i]->s_next; ~ 332 b_array[i]->s_prev = a_array[i]->s_ ~ 333 334 Now type "prev;" to finish it off. 335 336 The CTRL-E command acts like CTRL-Y except it inserts the character below the 337 cursor. 338 339 ============================================================================== 340 *24.6* Inserting a register 341 342 The command CTRL-R {register} inserts the contents of the register. This is 343 useful to avoid having to type a long word. For example, you need to type 344 this: 345 346 r = VeryLongFunction(a) + VeryLongFunction(b) + VeryLongFunction(c) ~ 347 348 The function name is defined in a different file. Edit that file and move the 349 cursor on top of the function name there, and yank it into register v: > 350 351 "vyiw 352 353 "v is the register specification, "yiw" is yank-inner-word. Now edit the file 354 where the new line is to be inserted, and type the first letters: 355 356 r = ~ 357 358 Now use CTRL-R v to insert the function name: 359 360 r = VeryLongFunction ~ 361 362 You continue to type the characters in between the function name, and use 363 CTRL-R v two times more. 364 You could have done the same with completion. Using a register is useful 365 when there are many words that start with the same characters. 366 367 If the register contains characters such as <BS> or other special characters, 368 they are interpreted as if they had been typed from the keyboard. If you do 369 not want this to happen (you really want the <BS> to be inserted in the text), 370 use the command CTRL-R CTRL-R {register}. 371 372 ============================================================================== 373 *24.7* Abbreviations 374 375 An abbreviation is a short word that takes the place of a long one. For 376 example, "ad" stands for "advertisement". Vim enables you to type an 377 abbreviation and then will automatically expand it for you. 378 To tell Vim to expand "ad" into "advertisement" every time you insert it, 379 use the following command: > 380 381 :iabbrev ad advertisement 382 383 Now, when you type "ad", the whole word "advertisement" will be inserted into 384 the text. This is triggered by typing a character that can't be part of a 385 word, for example a space: 386 387 What Is Entered What You See 388 I saw the a I saw the a ~ 389 I saw the ad I saw the ad ~ 390 I saw the ad<Space> I saw the advertisement<Space> ~ 391 392 The expansion doesn't happen when typing just "ad". That allows you to type a 393 word like "add", which will not get expanded. Only whole words are checked 394 for abbreviations. 395 396 397 ABBREVIATING SEVERAL WORDS 398 399 It is possible to define an abbreviation that results in multiple words. For 400 example, to define "JB" as "Jack Benny", use the following command: > 401 402 :iabbrev JB Jack Benny 403 404 As a programmer, I use two rather unusual abbreviations: > 405 406 :iabbrev #b /**************************************** 407 :iabbrev #e <Space>****************************************/ 408 409 These are used for creating boxed comments. The comment starts with #b, which 410 draws the top line. I then type the comment text and use #e to draw the 411 bottom line. 412 Notice that the #e abbreviation begins with a space. In other words, the 413 first two characters are space-star. Usually Vim ignores spaces between the 414 abbreviation and the expansion. To avoid that problem, I spell space as seven 415 characters: <, S, p, a, c, e, >. 416 417 Note: 418 ":iabbrev" is a long word to type. ":iab" works just as well. 419 That's abbreviating the abbreviate command! 420 421 422 FIXING TYPING MISTAKES 423 424 It's very common to make the same typing mistake every time. For example, 425 typing "teh" instead of "the". You can fix this with an abbreviation: > 426 427 :abbreviate teh the 428 429 You can add a whole list of these. Add one each time you discover a common 430 mistake. 431 432 433 LISTING ABBREVIATIONS 434 435 The ":abbreviate" command lists the abbreviations: > 436 437 :abbreviate 438 i #e ****************************************/ 439 i #b /**************************************** 440 i JB Jack Benny 441 i ad advertisement 442 ! teh the 443 444 The "i" in the first column indicates Insert mode. These abbreviations are 445 only active in Insert mode. Other possible characters are: 446 447 c Command-line mode :cabbrev 448 ! both Insert and Command-line mode :abbreviate 449 450 Since abbreviations are not often useful in Command-line mode, you will mostly 451 use the ":iabbrev" command. That avoids, for example, that "ad" gets expanded 452 when typing a command like: > 453 454 :edit ad 455 456 457 DELETING ABBREVIATIONS 458 459 To get rid of an abbreviation, use the ":unabbreviate" command. Suppose you 460 have the following abbreviation: > 461 462 :abbreviate @f fresh 463 464 You can remove it with this command: > 465 466 :unabbreviate @f 467 468 While you type this, you will notice that @f is expanded to "fresh". Don't 469 worry about this, Vim understands it anyway (except when you have an 470 abbreviation for "fresh", but that's very unlikely). 471 To remove all the abbreviations: > 472 473 :abclear 474 475 ":unabbreviate" and ":abclear" also come in the variants for Insert mode 476 (":iunabbreviate and ":iabclear") and Command-line mode (":cunabbreviate" and 477 ":cabclear"). 478 479 480 REMAPPING ABBREVIATIONS 481 482 There is one thing to watch out for when defining an abbreviation: The 483 resulting string should not be mapped. For example: > 484 485 :abbreviate @a adder 486 :imap dd disk-door 487 488 When you now type @a, you will get "adisk-doorer". That's not what you want. 489 To avoid this, use the ":noreabbrev" command. It does the same as 490 ":abbreviate", but avoids that the resulting string is used for mappings: > 491 492 :noreabbrev @a adder 493 494 Fortunately, it's unlikely that the result of an abbreviation is mapped. 495 496 ============================================================================== 497 *24.8* Entering special characters 498 499 The CTRL-V command is used to insert the next character literally. In other 500 words, any special meaning the character has, it will be ignored. For 501 example: > 502 503 CTRL-V <Esc> 504 505 Inserts an escape character. Thus you don't leave Insert mode. (Don't type 506 the space after CTRL-V, it's only to make this easier to read). 507 508 Note: 509 On MS-Windows CTRL-V is used to paste text. Use CTRL-Q instead of 510 CTRL-V. On Unix, on the other hand, CTRL-Q does not work on some 511 terminals, because it has a special meaning. 512 513 You can also use the command CTRL-V {digits} to insert a character with the 514 decimal number {digits}. For example, the character number 127 is the <Del> 515 character (but not necessarily the <Del> key!). To insert <Del> type: > 516 517 CTRL-V 127 518 519 You can enter characters up to 255 this way. When you type fewer than two 520 digits, a non-digit will terminate the command. To avoid the need of typing a 521 non-digit, prepend one or two zeros to make three digits. 522 All the next commands insert a <Tab> and then a dot: 523 524 CTRL-V 9. 525 CTRL-V 09. 526 CTRL-V 009. 527 528 To enter a character in hexadecimal, use an "x" after the CTRL-V: > 529 530 CTRL-V x7f 531 532 This also goes up to character 255 (CTRL-V xff). You can use "o" to type a 533 character as an octal number and two more methods allow you to type up to 534 a 16 bit and a 32 bit number (e.g., for a Unicode character): > 535 536 CTRL-V o123 537 CTRL-V u1234 538 CTRL-V U12345678 539 540 ============================================================================== 541 *24.9* Digraphs 542 543 Some characters are not on the keyboard. For example, the copyright character 544 (©). To type these characters in Vim, you use digraphs, where two characters 545 represent one. To enter a ©, for example, you press three keys: > 546 547 CTRL-K Co 548 549 To find out what digraphs are available, use the following command: > 550 551 :digraphs 552 553 Vim will display the digraph table. Here are three lines of it: 554 555 AC ~_ 159 NS | 160 !I ¡ 161 Ct ¢ 162 Pd £ 163 Cu ¤ 164 Ye ¥ 165 ~ 556 BB ¦ 166 SE § 167 ': ¨ 168 Co © 169 -a ª 170 << « 171 NO ¬ 172 ~ 557 -- 173 Rg ® 174 'm ¯ 175 DG ° 176 +- ± 177 2S ² 178 3S ³ 179 ~ 558 559 This shows, for example, that the digraph you get by typing CTRL-K Pd is the 560 character (£). This is character number 163 (decimal). 561 Pd is short for Pound. Most digraphs are selected to give you a hint about 562 the character they will produce. If you look through the list you will 563 understand the logic. 564 You can exchange the first and second character, if there is no digraph for 565 that combination. Thus CTRL-K dP also works. Since there is no digraph for 566 "dP" Vim will also search for a "Pd" digraph. 567 568 Note: 569 The digraphs depend on the character set that Vim assumes you are 570 using. Always use ":digraphs" to find out which digraphs are 571 currently available. 572 573 You can define your own digraphs by specifying the target character with a 574 decimal number. Example: > 575 576 :digraph a\" 228 577 578 This defines that CTRL-K a" inserts an ä character. Note: we had to escape 579 the " character since otherwise it would act as a comment character. 580 581 More information about digraphs here: |digraphs| 582 Another way to insert special characters is with a keymap. More about that 583 here: |45.5| 584 585 ============================================================================== 586 *24.10* Normal mode commands 587 588 Insert mode offers a limited number of commands. In Normal mode you have many 589 more. When you want to use one, you usually leave Insert mode with <Esc>, 590 execute the Normal mode command, and re-enter Insert mode with "i" or "a". 591 There is a quicker way. With CTRL-O {command} you can execute any Normal 592 mode command from Insert mode. For example, to delete from the cursor to the 593 end of the line: > 594 595 CTRL-O D 596 597 You can execute only one Normal mode command this way. But you can specify a 598 register or a count. A more complicated example: > 599 600 CTRL-O "g3dw 601 602 This deletes up to the third word into register g. 603 604 ============================================================================== 605 606 Next chapter: |usr_25.txt| Editing formatted text 607 608 Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: