usr_02.txt (23681B)
1 *usr_02.txt* Nvim 2 3 4 VIM USER MANUAL by Bram Moolenaar 5 6 7 The first steps in Vim 8 9 10 This chapter provides just enough information to edit a file with Vim. Not 11 well or fast, but you can edit. Take some time to practice with these 12 commands, they form the base for what follows. 13 14 |02.1| Running Vim for the First Time 15 |02.2| Inserting text 16 |02.3| Moving around 17 |02.4| Deleting characters 18 |02.5| Undo and Redo 19 |02.6| Other editing commands 20 |02.7| Getting out 21 |02.8| Finding help 22 23 Next chapter: |usr_03.txt| Moving around 24 Previous chapter: |usr_01.txt| About the manuals 25 Table of contents: |usr_toc.txt| 26 27 ============================================================================== 28 *02.1* Running Vim for the First Time 29 30 To start Nvim, enter this command: > 31 32 nvim file.txt 33 34 On Unix you can type this at any command prompt. If you are running Microsoft 35 Windows, open a Command Prompt and enter the command. In either case, Vim 36 starts editing a file called file.txt. Because this is a new file, you get a 37 blank window. This is what your screen will look like: 38 > 39 +---------------------------------------+ 40 |# | 41 |~ | 42 |~ | 43 |~ | 44 |~ | 45 |"file.txt" [New] | 46 +---------------------------------------+ 47 ('#' is the cursor position.) 48 < 49 The tilde (~) lines indicate lines not in the file. In other words, when Vim 50 runs out of file to display, it displays tilde lines. At the bottom of the 51 screen, a message line indicates the file is named file.txt and shows that you 52 are creating a new file. The message information is temporary and other 53 information overwrites it. 54 55 ============================================================================== 56 *02.2* Inserting text 57 58 The Vim editor is a modal editor. That means that the editor behaves 59 differently, depending on which mode you are in. The two basic modes are 60 called Normal mode and Insert mode. In Normal mode the characters you type 61 are commands. In Insert mode the characters are inserted as text. 62 Since you have just started Vim it will be in Normal mode. To start Insert 63 mode you type the "i" command (i for Insert). Then you can enter 64 the text. It will be inserted into the file. Do not worry if you make 65 mistakes; you can correct them later. To enter the following programmer's 66 limerick, this is what you type: > 67 68 iA very intelligent turtle 69 Found programming Unix a hurdle 70 71 After typing "turtle" you press the <Enter> key to start a new line. Finally 72 you press the <Esc> key to stop Insert mode and go back to Normal mode. You 73 now have two lines of text in your Vim window: 74 > 75 +---------------------------------------+ 76 |A very intelligent turtle | 77 |Found programming Unix a hurdle | 78 |~ | 79 |~ | 80 | | 81 +---------------------------------------+ 82 < 83 84 WHAT IS THE MODE? 85 86 To be able to see what mode you are in, type this command: > 87 88 :set showmode 89 90 You will notice that when typing the colon Vim moves the cursor to the last 91 line of the window. That's where you type colon commands (commands that start 92 with a colon). Finish this command by pressing the <Enter> key (all commands 93 that start with a colon are finished this way). 94 Now, if you type the "i" command Vim will display --INSERT-- at the bottom 95 of the window. This indicates you are in Insert mode. 96 > 97 +---------------------------------------+ 98 |A very intelligent turtle | 99 |Found programming Unix a hurdle | 100 |~ | 101 |~ | 102 |-- INSERT -- | 103 +---------------------------------------+ 104 < 105 If you press <Esc> to go back to Normal mode the last line will be made blank. 106 107 108 GETTING OUT OF TROUBLE 109 110 One of the problems for Vim novices is mode confusion, which is caused by 111 forgetting which mode you are in or by accidentally typing a command that 112 switches modes. To get back to Normal mode, no matter what mode you are in, 113 press the <Esc> key. Sometimes you have to press it twice. If Vim beeps back 114 at you, you already are in Normal mode. 115 116 ============================================================================== 117 *02.3* Moving around 118 119 After you return to Normal mode, you can move around by using these keys: 120 121 h left *hjkl* 122 j down 123 k up 124 l right 125 126 At first, it may appear that these commands were chosen at random. After all, 127 who ever heard of using l for right? But actually, there is a very good 128 reason for these choices: Moving the cursor is the most common thing you do in 129 an editor, and these keys are on the home row of your right hand. In other 130 words, these commands are placed where you can type them the fastest 131 (especially when you type with ten fingers). 132 133 Note: 134 You can also move the cursor by using the arrow keys. If you do, 135 however, you greatly slow down your editing because to press the arrow 136 keys, you must move your hand from the text keys to the arrow keys. 137 Considering that you might be doing it hundreds of times an hour, this 138 can take a significant amount of time. 139 Also, there are keyboards which do not have arrow keys, or which 140 locate them in unusual places; therefore, knowing the use of the hjkl 141 keys helps in those situations. 142 143 One way to remember these commands is that h is on the left, l is on the 144 right and j points down. In a picture: > 145 146 k 147 h l 148 j 149 150 The best way to learn these commands is by using them. Use the "i" command to 151 insert some more lines of text. Then use the hjkl keys to move around and 152 insert a word somewhere. Don't forget to press <Esc> to go back to Normal 153 mode. |:Tutor| is also a nice way to learn by doing. 154 155 For Japanese users, Hiroshi Iwatani suggested using this: 156 157 Komsomolsk 158 ^ 159 | 160 Huan Ho <--- ---> Los Angeles 161 (Yellow river) | 162 v 163 Java (the island, not the programming language) 164 165 ============================================================================== 166 *02.4* Deleting characters 167 168 To delete a character, move the cursor over it and type "x". (This is a 169 throwback to the old days of the typewriter, when you deleted things by typing 170 xxxx over them.) Move the cursor to the beginning of the first line, for 171 example, and type xxxxxxx (seven x's) to delete "A very ". The result should 172 look like this: 173 > 174 +---------------------------------------+ 175 |intelligent turtle | 176 |Found programming Unix a hurdle | 177 |~ | 178 |~ | 179 | | 180 +---------------------------------------+ 181 < 182 Now you can insert new text, for example by typing: > 183 184 iA young <Esc> 185 186 This begins an insert (the i), inserts the words "A young", and then exits 187 insert mode (the final <Esc>). The result: 188 > 189 +---------------------------------------+ 190 |A young intelligent turtle | 191 |Found programming Unix a hurdle | 192 |~ | 193 |~ | 194 | | 195 +---------------------------------------+ 196 < 197 198 DELETING A LINE 199 200 To delete a whole line use the "dd" command. The following line will 201 then move up to fill the gap: 202 > 203 +---------------------------------------+ 204 |Found programming Unix a hurdle | 205 |~ | 206 |~ | 207 |~ | 208 | | 209 +---------------------------------------+ 210 < 211 212 DELETING A LINE BREAK 213 214 In Vim you can join two lines together, which means that the line break 215 between them is deleted. The "J" command does this. 216 Take these two lines: 217 218 A young intelligent ~ 219 turtle ~ 220 221 Move the cursor to the first line and press "J": 222 223 A young intelligent turtle ~ 224 225 ============================================================================== 226 *02.5* Undo and Redo 227 228 Suppose you delete too much. Well, you can type it in again, but an easier 229 way exists. The "u" command undoes the last edit. Take a look at this in 230 action: After using "dd" to delete the first line, "u" brings it back. 231 Another one: Move the cursor to the A in the first line: 232 233 A young intelligent turtle ~ 234 235 Now type xxxxxxx to delete "A young". The result is as follows: 236 237 intelligent turtle ~ 238 239 Type "u" to undo the last delete. That delete removed the g, so the undo 240 restores the character. 241 242 g intelligent turtle ~ 243 244 The next "u" command restores the next-to-last character deleted: 245 246 ng intelligent turtle ~ 247 248 The next "u" command gives you the u, and so on: 249 250 ung intelligent turtle ~ 251 oung intelligent turtle ~ 252 young intelligent turtle ~ 253 young intelligent turtle ~ 254 A young intelligent turtle ~ 255 256 REDO 257 258 If you undo too many times, you can press CTRL-R (redo) to reverse the 259 preceding command. In other words, it undoes the undo. To see this in 260 action, press CTRL-R twice. The character A and the space after it disappear: 261 262 young intelligent turtle ~ 263 264 There's a special version of the undo command, the "U" (undo line) command. 265 The undo line command undoes all the changes made on the last line that was 266 edited. Typing this command twice cancels the preceding "U". 267 268 A very intelligent turtle ~ 269 xxxx Delete very 270 271 A intelligent turtle ~ 272 xxxxxx Delete turtle 273 274 A intelligent ~ 275 Restore line with "U" 276 A very intelligent turtle ~ 277 Undo "U" with "u" 278 A intelligent ~ 279 280 The "U" command is a change by itself, which the "u" command undoes and CTRL-R 281 redoes. This might be a bit confusing. Don't worry, with "u" and CTRL-R you 282 can go to any of the situations you had. More about that in section |32.2|. 283 284 ============================================================================== 285 *02.6* Other editing commands 286 287 Vim has a large number of commands to change the text. See |Q_in| and below. 288 Here are a few often used ones. 289 290 291 APPENDING 292 293 The "i" command inserts a character before the character under the cursor. 294 That works fine; but what happens if you want to add stuff to the end of the 295 line? For that you need to insert text after the cursor. This is done with 296 the "a" (append) command. 297 For example, to change the line 298 299 and that's not saying much for the turtle. ~ 300 to 301 and that's not saying much for the turtle!!! ~ 302 303 move the cursor over to the dot at the end of the line. Then type "x" to 304 delete the period. The cursor is now positioned at the end of the line on the 305 e in turtle. Now type > 306 307 a!!!<Esc> 308 309 to append three exclamation points after the e in turtle: 310 311 and that's not saying much for the turtle!!! ~ 312 313 314 OPENING UP A NEW LINE 315 316 The "o" command creates a new, empty line below the cursor and puts Vim in 317 Insert mode. Then you can type the text for the new line. 318 Suppose the cursor is somewhere in the first of these two lines: 319 320 A very intelligent turtle ~ 321 Found programming Unix a hurdle ~ 322 323 If you now use the "o" command and type new text: > 324 325 oThat liked using Vim<Esc> 326 327 The result is: 328 329 A very intelligent turtle ~ 330 That liked using Vim ~ 331 Found programming Unix a hurdle ~ 332 333 The "O" command (uppercase) opens a line above the cursor. 334 335 336 USING A COUNT 337 338 Suppose you want to move up nine lines. You can type "kkkkkkkkk" or you can 339 enter the command "9k". In fact, you can precede many commands with a number. 340 Earlier in this chapter, for instance, you added three exclamation points to 341 the end of a line by typing "a!!!<Esc>". Another way to do this is to use the 342 command "3a!<Esc>". The count of 3 tells the command that follows to triple 343 its effect. Similarly, to delete three characters, use the command "3x". The 344 count always comes before the command it applies to. 345 346 ============================================================================== 347 *02.7* Getting out 348 349 To exit, use the "ZZ" command. This command writes the file and exits. 350 351 Note: 352 Unlike many other editors, Vim does not automatically make a backup 353 file. If you type "ZZ", your changes are committed and there's no 354 turning back. You can configure the Vim editor to produce backup 355 files; see |07.4|. 356 357 358 DISCARDING CHANGES 359 360 Sometimes you will make a sequence of changes and suddenly realize you were 361 better off before you started. Not to worry; Vim has a 362 quit-and-throw-things-away command. It is: > 363 364 :q! 365 366 Don't forget to press <Enter> to finish the command. 367 368 For those of you interested in the details, the three parts of this command 369 are the colon (:), which enters Command-line mode; the q command, which tells 370 the editor to quit; and the override command modifier (!). 371 The override command modifier is needed because Vim is reluctant to throw 372 away changes. If you were to just type ":q", Vim would display an error 373 message and refuse to exit: 374 375 E37: No write since last change (use ! to override) ~ 376 377 By specifying the override, you are in effect telling Vim, "I know that what 378 I'm doing looks stupid, but I really want to do this." 379 380 If you want to continue editing with Vim: The ":e!" command reloads the 381 original version of the file. 382 383 ============================================================================== 384 *02.8* Finding help 385 386 Everything you always wanted to know can be found in the Vim help files. 387 Don't be afraid to ask! 388 389 If you know what you are looking for, it is usually easier to search for it 390 using the help system, instead of using Google. Because the subjects follow 391 a certain style guide. 392 393 Also the help has the advantage of belonging to your particular Vim version. 394 You won't see help for commands added later. These would not work for you. 395 396 To get generic help use this command: > 397 398 :help 399 400 You could also use the first function key <F1>. If your keyboard has a <Help> 401 key it might work as well. 402 If you don't supply a subject, ":help" displays the general help window. 403 The creators of Vim did something very clever (or very lazy) with the help 404 system: They made the help window a normal editing window. You can use all 405 the normal Vim commands to move through the help information. Therefore h, j, 406 k, and l move left, down, up and right. 407 To get out of the help window, use the same command you use to get out of 408 the editor: "ZZ". This will only close the help window, not exit Vim. 409 410 As you read the help text, you will notice some text enclosed in vertical bars 411 (for example, |help|). This indicates a hyperlink. If you position the 412 cursor anywhere between the bars and press CTRL-] (jump to tag), the help 413 system takes you to the indicated subject. (For reasons not discussed here, 414 the Vim terminology for a hyperlink is tag. So CTRL-] jumps to the location 415 of the tag given by the word under the cursor.) 416 After a few jumps, you might want to go back. CTRL-T (pop tag) takes you 417 back to the preceding position. CTRL-O (jump to older position) also works 418 nicely here. 419 At the top of the help screen, there is the notation "*help.txt*". This 420 name between "*" characters is used by the help system to define a tag 421 (hyperlink destination). 422 See |29.1| for details about using tags. 423 424 To get help on a given subject, use the following command: > 425 426 :help {subject} 427 428 To get help on the "x" command, for example, enter the following: > 429 430 :help x 431 432 To find out how to delete text, use this command: > 433 434 :help deleting 435 436 To get a complete index of all Vim commands, use the following command: > 437 438 :help index 439 440 When you need to get help for a control character command (for example, 441 CTRL-A), you need to spell it with the prefix "CTRL-". > 442 443 :help CTRL-A 444 445 The Vim editor has many different modes. By default, the help system displays 446 the normal-mode commands. For example, the following command displays help 447 for the normal-mode CTRL-H command: > 448 449 :help CTRL-H 450 451 To identify other modes, use a mode prefix. If you want the help for the 452 insert-mode version of a command, use "i_". For CTRL-H this gives you the 453 following command: > 454 455 :help i_CTRL-H 456 457 When you start the Vim editor, you can use several command-line arguments. 458 These all begin with a dash (-). To find what the -t argument does, for 459 example, use the command: > 460 461 :help -t 462 463 The Vim editor has a number of options that enable you to configure and 464 customize the editor. If you want help for an option, you need to enclose it 465 in single quotation marks. To find out what the 'number' option does, for 466 example, use the following command: > 467 468 :help 'number' 469 470 The table with all mode prefixes can be found below: |help-summary|. 471 472 Special keys are enclosed in angle brackets. To find help on the up-arrow key 473 in Insert mode, for instance, use this command: > 474 475 :help i_<Up> 476 477 If you see an error message that you don't understand, for example: 478 479 E37: No write since last change (use ! to override) ~ 480 481 You can use the error ID at the start to find help about it: > 482 483 :help E37 484 485 486 Summary: *help-summary* > 487 488 1) Use Ctrl-D after typing a topic and let Vim show all available topics. 489 Or press Tab to complete: > 490 :help some<Tab> 491 < More information on how to use the help: > 492 :help helphelp 493 494 2) Follow the links in bars to related help. You can go from the detailed 495 help to the user documentation, which describes certain commands more from 496 a user perspective and less detailed. E.g. after: > 497 :help pattern.txt 498 < You can see the user guide topics |03.9| and |usr_27.txt| in the 499 introduction. 500 501 3) Options are enclosed in single apostrophes. To go to the help topic for 502 the list option: > 503 :help 'list' 504 < If you only know you are looking for a certain option, you can also do: > 505 :help options.txt 506 < to open the help page which describes all option handling and then search 507 using regular expressions, e.g. textwidth. 508 Certain options have their own namespace, e.g.: > 509 :help cpo-<letter> 510 < for the corresponding flag of the 'cpoptions' settings, substitute <letter> 511 by a specific flag, e.g.: > 512 :help cpo-; 513 < And for the 'guioptions' flags: > 514 :help go-<letter> 515 516 4) Normal mode commands do not have a prefix. To go to the help page for the 517 "gt" command: > 518 :help gt 519 520 5) Insert mode commands start with i_. Help for deleting a word: > 521 :help i_CTRL-W 522 523 6) Visual mode commands start with v_. Help for jumping to the other side of 524 the Visual area: > 525 :help v_o 526 527 7) Command line editing and arguments start with c_. Help for using the 528 command argument %: > 529 :help c_% 530 531 8) Ex-commands always start with ":", so to go to the ":s" command help: > 532 :help :s 533 534 9) Commands specifically for debugging start with ">". To go to the help 535 for the "cont" debug command: > 536 :help >cont 537 538 10) Key combinations. They usually start with a single letter indicating 539 the mode for which they can be used. E.g.: > 540 :help i_CTRL-X 541 < takes you to the family of CTRL-X commands for insert mode which can be 542 used to auto-complete different things. Note, that certain keys will 543 always be written the same, e.g. Control will always be CTRL. 544 For normal mode commands there is no prefix and the topic is available at 545 :h CTRL-<Letter>. E.g. > 546 :help CTRL-W 547 < In contrast > 548 :help c_CTRL-R 549 < will describe what the CTRL-R does when entering commands in the Command 550 line and > 551 :help v_CTRL-A 552 < talks about incrementing numbers in visual mode and > 553 :help g_CTRL-A 554 < talks about the "g<C-A>" command (e.g. you have to press "g" then 555 <CTRL-A>). Here the "g" stands for the normal command "g" which always 556 expects a second key before doing something similar to the commands 557 starting with "z". 558 559 11) Regexp items always start with /. So to get help for the "\+" quantifier 560 in Vim regexes: > 561 :help /\+ 562 < If you need to know everything about regular expressions, start reading 563 at: > 564 :help pattern.txt 565 566 12) Registers always start with "quote". To find out about the special ":" 567 register: > 568 :help quote: 569 570 13) Vim script is available at > 571 :help vimeval.txt 572 < Certain aspects of the language are available at :h expr-X where "X" is a 573 single letter. E.g. > 574 :help expr-! 575 < will take you to the topic describing the "!" (Not) operator for Vim 576 Script. 577 Also important is > 578 :help function-list 579 < to find a short description of all functions available. Help topics for 580 Vim script functions always include the "()", so: > 581 :help append() 582 < talks about the append Vim script function rather than how to append text 583 in the current buffer. 584 585 14) Mappings are talked about in the help page :h |map.txt|. Use > 586 :help mapmode-i 587 < to find out about the |:imap| command. Also use :map-topic 588 to find out about certain subtopics particular for mappings. e.g: > 589 :help :map-local 590 < for buffer-local mappings or > 591 :help map-bar 592 < for how the '|' is handled in mappings. 593 594 15) Command definitions are talked about :h command-topic, so use > 595 :help command-bang 596 < to find out about the '!' argument for custom commands. 597 598 16) Window management commands always start with CTRL-W, so you find the 599 corresponding help at :h CTRL-W_letter. E.g. > 600 :help CTRL-W_p 601 < for moving the previous accessed window. You can also access > 602 :help windows.txt 603 < and read your way through if you are looking for window handling 604 commands. 605 606 17) Use |:helpgrep| to search in all help pages (and also of any installed 607 plugins). See |:helpgrep| for how to use it. 608 To search for a topic: > 609 :helpgrep topic 610 < This takes you to the first match. To go to the next one: > 611 :cnext 612 < All matches are available in the quickfix window which can be opened 613 with: > 614 :copen 615 < Move around to the match you like and press Enter to jump to that help. 616 617 18) The user manual. This describes help topics for beginners in a rather 618 friendly way. Start at |usr_toc.txt| to find the table of content (as you 619 might have guessed): > 620 :help usr_toc.txt 621 < Skim over the contents to find interesting topics. The "Digraphs" and 622 "Entering special characters" items are in chapter 24, so to go to that 623 particular help page: > 624 :help usr_24.txt 625 < Also if you want to access a certain chapter in the help, the chapter 626 number can be accessed directly like this: > 627 :help 10.1 628 < which goes to chapter 10.1 in |usr_10.txt| and talks about recording 629 macros. 630 631 19) Highlighting groups. Always start with hl-groupname. E.g. > 632 :help hl-WarningMsg 633 < talks about the WarningMsg highlighting group. 634 635 20) Syntax highlighting is namespaced to :syn-topic. E.g. > 636 :help :syn-conceal 637 < talks about the conceal argument for the ":syn" command. 638 639 21) Quickfix commands usually start with :c while location list commands 640 usually start with :l 641 642 22) Autocommand events can be found by their name: > 643 :help BufWinLeave 644 < To see all possible events: > 645 :help events 646 647 23) Command-line switches always start with "-". So for the help of the -f 648 command switch of Vim use: > 649 :help -f 650 651 24) Lua language and Nvim's Lua standard library are available at >vim 652 :help lua.txt 653 < Guide to using Lua in Nvim is available at >vim 654 :help lua-guide.txt 655 < Lua 5.1 reference manual is available at >vim 656 :help luaref.txt 657 < 658 25) Documentation for included filetype specific functionality is usually 659 available in the form ft-<filetype>-<functionality>. So > 660 :help ft-c-syntax 661 < talks about the C syntax file and the option it provides. Sometimes, 662 additional sections for omni completion > 663 :help ft-php-omni 664 < or filetype plugins > 665 :help ft-tex-plugin 666 < are available. 667 668 26) Error and Warning codes can be looked up directly in the help. So > 669 :help E297 670 < takes you exactly to the description of the swap error message and > 671 :help W10 672 < talks about the warning "Changing a readonly file". 673 Sometimes, however, those error codes are not described, but rather are 674 listed at the Vim command that usually causes this. So: > 675 :help E128 676 < takes you to the |:function| command 677 678 27) Documentation for packages distributed with Vim have the form 679 package-<name>. So > 680 :help package-termdebug 681 < 682 will bring you to the help section for the included termdebug plugin and 683 how to enable it. 684 685 686 ============================================================================== 687 688 Next chapter: |usr_03.txt| Moving around 689 690 Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: