robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
7e778205ab
master
1*usr_24.txt* Nvim 2 3 4 VIM USER MANUAL by Bram Moolenaar 5 6 7 Inserting quickly 8 9 10When entering text, Vim offers various ways to reduce the number of keystrokes 11and avoid typing mistakes. Use Insert mode completion to repeat previously 12typed words. Abbreviate long words to short ones. Type characters that 13aren'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 28Table of contents: |usr_toc.txt| 29 30============================================================================== 31*24.1* Making corrections 32 33The <BS> key was already mentioned. It deletes the character just before the 34cursor. The <Del> key does the same for the character under (after) the 35cursor. 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 42If you really messed up a line and want to start over, use CTRL-U to delete 43it. This keeps the text after the cursor and the indent. Only the text from 44the 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 51When you spot a mistake a few words back, you need to move the cursor there to 52correct it. For example, you typed this: 53 54 The horse had follen to the ground ~ 55 56You need to change "follen" to "fallen". With the cursor at the end, you 57would 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 67Another 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 77This uses special keys to move around, while remaining in Insert mode. This 78resembles what you would do in a modeless editor. It's easier to remember, 79but takes more time (you have to move your hand from the letters to the cursor 80keys, 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 82leave 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 96There are a few more, see |ins-special-special|. 97 98============================================================================== 99*24.2* Showing matches 100 101When you type a ) it would be nice to see with which ( it matches. To make 102Vim do that use this command: > 103 104 :set showmatch 105 106When you now type a text like "(example)", as soon as you type the ) Vim will 107briefly move the cursor to the matching (, keep it there for half a second, 108and move back to where you were typing. 109 In case there is no matching (, Vim will beep. Then you know that you 110might 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 112with typing the next character, as soon as Vim sees it the cursor will move 113back and inserting continues as before. 114 You can change the time Vim waits with the 'matchtime' option. For 115example, to make Vim wait one and a half second: > 116 117 :set matchtime=15 118 119The time is specified in tenths of a second. 120 121============================================================================== 122*24.3* Completion 123 124Vim can automatically complete words on insertion. You type the first part of 125a 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 127the following: 128 129 total = ch_array[0] + ch_array[1] + ch_array[2]; ~ 130 131You start by entering the following: 132 133 total = ch_array[0] + ch_ ~ 134 135At this point, you tell Vim to complete the word using the command CTRL-P. 136Vim searches for a word that starts with what's in front of the cursor. In 137this case, it is "ch_", which matches with the word ch_array. So typing 138CTRL-P gives you the following: 139 140 total = ch_array[0] + ch_array ~ 141 142After a little more typing, you get this (ending in a space): 143 144 total = ch_array[0] + ch_array[1] + ~ 145 146If you now type CTRL-P Vim will search again for a word that completes the 147word before the cursor. Since there is nothing in front of the cursor, it 148finds the first word backwards, which is "ch_array". Typing CTRL-P again 149gives you the next word that matches, in this case "total". A third CTRL-P 150searches further back. If there is nothing else, it causes the editor to run 151out of words, so it returns to the original text, which is nothing. A fourth 152CTRL-P causes the editor to start over again with "ch_array". 153 154To search forward, use CTRL-N. Since the search wraps around the end of the 155file, CTRL-N and CTRL-P will find the same matches, but in a different 156sequence. Hint: CTRL-N is Next-match and CTRL-P is Previous-match. 157 158The Vim editor goes through a lot of effort to find words to complete. By 159default, 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 169OPTIONS 170 171You can customize the search order with the 'complete' option. 172 173The 'ignorecase' option is used. When it is set, case differences are ignored 174when searching for matches. 175 176A special option for completion is 'infercase'. This is useful to find 177matches while ignoring case ('ignorecase' must be set) but still using the 178case 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 182COMPLETING SPECIFIC ITEMS 183 184If you know what you are looking for, you can use these commands to complete 185with 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 197After each of them CTRL-N can be used to find the next match, CTRL-P to find 198the previous match. 199 More information for each of these commands here: |ins-completion|. 200 201 202COMPLETING FILE NAMES 203 204Let's take CTRL-X CTRL-F as an example. This will find file names. It scans 205the current directory for files and displays each one that matches the word in 206front of the cursor. 207 Suppose, for example, that you have the following files in the current 208directory: 209 210 main.c sub_count.c sub_done.c sub_exit.c 211 212Now enter Insert mode and start typing: 213 214 The exit code is in the file sub ~ 215 216At this point, you enter the command CTRL-X CTRL-F. Vim now completes the 217current word "sub" by looking at the files in the current directory. The 218first match is sub_count.c. This is not the one you want, so you match the 219next file by typing CTRL-N. This match is sub_done.c. Typing CTRL-N again 220takes you to sub_exit.c. The results: 221 222 The exit code is in the file sub_exit.c ~ 223 224If the file name starts with / (Unix) or C:\ (MS-Windows) you can find all 225files in the file system. For example, type "/u" and CTRL-X CTRL-F. This 226will match "/usr" (this is on Unix): 227 228 the file is found in /usr/ ~ 229 230If you now press CTRL-N you go back to "/u". Instead, to accept the "/usr/" 231and go one directory level deeper, use CTRL-X CTRL-F again: 232 233 the file is found in /usr/X11R6/ ~ 234 235The results depend on what is found in your file system, of course. The 236matches are sorted alphabetically. 237 238 239COMPLETING IN SOURCE CODE 240 241Source code files are well structured. That makes it possible to do 242completion in an intelligent way. In Vim this is called Omni completion. In 243some other editors it's called intellisense, but that is a trademark. 244 245The key to Omni completion is CTRL-X CTRL-O. Obviously the O stands for Omni 246here, so that you can remember it easier. Let's use an example for editing C 247source: > 248 249 { 250 struct foo *p; 251 p-> 252 253The cursor is after "p->". Now type CTRL-X CTRL-O. Vim will offer you a list 254of alternatives, which are the items that "struct foo" contains. That is 255quite different from using CTRL-P, which would complete any word, while only 256members of "struct foo" are valid here. 257 258For Omni completion to work you may need to do some setup. At least make sure 259filetype plugins are enabled. Your vimrc file should contain a line like 260this: > 261 filetype plugin on 262Or: > 263 filetype plugin indent on 264 265For C code you need to create a tags file and set the 'tags' option. That is 266explained |ft-c-omni|. For other filetypes you may need to do something 267similar, look below |compl-omni-filetypes|. It only works for specific 268filetypes. Check the value of the 'omnifunc' option to find out if it would 269work. 270 271============================================================================== 272*24.4* Repeating an insert 273 274If you press CTRL-A, the editor inserts the text you typed the last time you 275were 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 281You edit this file by inserting "#include " at the beginning of the first 282line: > 283 284 #include "file.h" ~ 285 /* Main program begins */ ~ 286 287You go down to the beginning of the next line using the commands "j^". You 288now start to insert a new "#include" line. So you type: > 289 290 i CTRL-A 291 292The result is as follows: > 293 294 #include "file.h" ~ 295 #include /* Main program begins */ ~ 296 297The "#include " was inserted because CTRL-A inserts the text of the previous 298insert. 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 305The CTRL-@ command does a CTRL-A and then exits Insert mode. That's a quick 306way of doing exactly the same insertion again. 307 308============================================================================== 309*24.5* Copying from another line 310 311The CTRL-Y command inserts the character above the cursor. This is useful 312when you are duplicating a previous line. For example, you have this line of 313C code: 314 315 b_array[i]->s_next = a_array[i]->s_next; ~ 316 317Now you need to type the same line, but with "s_prev" instead of "s_next". 318Start 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 324Now you type "prev": 325 326 b_array[i]->s_next = a_array[i]->s_next; ~ 327 b_array[i]->s_prev ~ 328 329Continue 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 334Now type "prev;" to finish it off. 335 336The CTRL-E command acts like CTRL-Y except it inserts the character below the 337cursor. 338 339============================================================================== 340*24.6* Inserting a register 341 342The command CTRL-R {register} inserts the contents of the register. This is 343useful to avoid having to type a long word. For example, you need to type 344this: 345 346 r = VeryLongFunction(a) + VeryLongFunction(b) + VeryLongFunction(c) ~ 347 348The function name is defined in a different file. Edit that file and move the 349cursor 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 354where the new line is to be inserted, and type the first letters: 355 356 r = ~ 357 358Now use CTRL-R v to insert the function name: 359 360 r = VeryLongFunction ~ 361 362You continue to type the characters in between the function name, and use 363CTRL-R v two times more. 364 You could have done the same with completion. Using a register is useful 365when there are many words that start with the same characters. 366 367If the register contains characters such as <BS> or other special characters, 368they are interpreted as if they had been typed from the keyboard. If you do 369not want this to happen (you really want the <BS> to be inserted in the text), 370use the command CTRL-R CTRL-R {register}. 371 372============================================================================== 373*24.7* Abbreviations 374 375An abbreviation is a short word that takes the place of a long one. For 376example, "ad" stands for "advertisement". Vim enables you to type an 377abbreviation and then will automatically expand it for you. 378 To tell Vim to expand "ad" into "advertisement" every time you insert it, 379use the following command: > 380 381 :iabbrev ad advertisement 382 383Now, when you type "ad", the whole word "advertisement" will be inserted into 384the text. This is triggered by typing a character that can't be part of a 385word, 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 392The expansion doesn't happen when typing just "ad". That allows you to type a 393word like "add", which will not get expanded. Only whole words are checked 394for abbreviations. 395 396 397ABBREVIATING SEVERAL WORDS 398 399It is possible to define an abbreviation that results in multiple words. For 400example, to define "JB" as "Jack Benny", use the following command: > 401 402 :iabbrev JB Jack Benny 403 404As a programmer, I use two rather unusual abbreviations: > 405 406 :iabbrev #b /**************************************** 407 :iabbrev #e <Space>****************************************/ 408 409These are used for creating boxed comments. The comment starts with #b, which 410draws the top line. I then type the comment text and use #e to draw the 411bottom line. 412 Notice that the #e abbreviation begins with a space. In other words, the 413first two characters are space-star. Usually Vim ignores spaces between the 414abbreviation and the expansion. To avoid that problem, I spell space as seven 415characters: <, 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 422FIXING TYPING MISTAKES 423 424It's very common to make the same typing mistake every time. For example, 425typing "teh" instead of "the". You can fix this with an abbreviation: > 426 427 :abbreviate teh the 428 429You can add a whole list of these. Add one each time you discover a common 430mistake. 431 432 433LISTING ABBREVIATIONS 434 435The ":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 444The "i" in the first column indicates Insert mode. These abbreviations are 445only active in Insert mode. Other possible characters are: 446 447 c Command-line mode :cabbrev 448 ! both Insert and Command-line mode :abbreviate 449 450Since abbreviations are not often useful in Command-line mode, you will mostly 451use the ":iabbrev" command. That avoids, for example, that "ad" gets expanded 452when typing a command like: > 453 454 :edit ad 455 456 457DELETING ABBREVIATIONS 458 459To get rid of an abbreviation, use the ":unabbreviate" command. Suppose you 460have the following abbreviation: > 461 462 :abbreviate @f fresh 463 464You can remove it with this command: > 465 466 :unabbreviate @f 467 468While you type this, you will notice that @f is expanded to "fresh". Don't 469worry about this, Vim understands it anyway (except when you have an 470abbreviation 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 480REMAPPING ABBREVIATIONS 481 482There is one thing to watch out for when defining an abbreviation: The 483resulting string should not be mapped. For example: > 484 485 :abbreviate @a adder 486 :imap dd disk-door 487 488When you now type @a, you will get "adisk-doorer". That's not what you want. 489To 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 494Fortunately, it's unlikely that the result of an abbreviation is mapped. 495 496============================================================================== 497*24.8* Entering special characters 498 499The CTRL-V command is used to insert the next character literally. In other 500words, any special meaning the character has, it will be ignored. For 501example: > 502 503 CTRL-V <Esc> 504 505Inserts an escape character. Thus you don't leave Insert mode. (Don't type 506the 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 513You can also use the command CTRL-V {digits} to insert a character with the 514decimal number {digits}. For example, the character number 127 is the <Del> 515character (but not necessarily the <Del> key!). To insert <Del> type: > 516 517 CTRL-V 127 518 519You can enter characters up to 255 this way. When you type fewer than two 520digits, a non-digit will terminate the command. To avoid the need of typing a 521non-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 528To enter a character in hexadecimal, use an "x" after the CTRL-V: > 529 530 CTRL-V x7f 531 532This also goes up to character 255 (CTRL-V xff). You can use "o" to type a 533character as an octal number and two more methods allow you to type up to 534a 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 543Some characters are not on the keyboard. For example, the copyright character 544(©). To type these characters in Vim, you use digraphs, where two characters 545represent one. To enter a ©, for example, you press three keys: > 546 547 CTRL-K Co 548 549To find out what digraphs are available, use the following command: > 550 551 :digraphs 552 553Vim 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 559This shows, for example, that the digraph you get by typing CTRL-K Pd is the 560character (£). This is character number 163 (decimal). 561 Pd is short for Pound. Most digraphs are selected to give you a hint about 562the character they will produce. If you look through the list you will 563understand the logic. 564 You can exchange the first and second character, if there is no digraph for 565that 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 573You can define your own digraphs by specifying the target character with a 574decimal number. Example: > 575 576 :digraph a\" 228 577 578This defines that CTRL-K a" inserts an ä character. Note: we had to escape 579the " character since otherwise it would act as a comment character. 580 581More information about digraphs here: |digraphs| 582 Another way to insert special characters is with a keymap. More about that 583here: |45.5| 584 585============================================================================== 586*24.10* Normal mode commands 587 588Insert mode offers a limited number of commands. In Normal mode you have many 589more. When you want to use one, you usually leave Insert mode with <Esc>, 590execute 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 592mode command from Insert mode. For example, to delete from the cursor to the 593end of the line: > 594 595 CTRL-O D 596 597You can execute only one Normal mode command this way. But you can specify a 598register or a count. A more complicated example: > 599 600 CTRL-O "g3dw 601 602This deletes up to the third word into register g. 603 604============================================================================== 605 606Next chapter: |usr_25.txt| Editing formatted text 607 608Copyright: see |manual-copyright| 609 610vim:tw=78:ts=8:noet:ft=help:norl: