usr_03.txt (23934B)
1 *usr_03.txt* For Vim version 9.2. Last change: 2026 Feb 14 2 3 4 VIM USER MANUAL by Bram Moolenaar 5 6 7 Moving around 8 9 10 Before you can insert or delete text the cursor has to be moved to the right 11 place. Vim has a large number of commands to position the cursor. This 12 chapter shows you how to use the most important ones. You can find a list of 13 these commands below |Q_lr|. 14 15 |03.1| Word movement 16 |03.2| Moving to the start or end of a line 17 |03.3| Moving to a character 18 |03.4| Matching a parenthesis 19 |03.5| Moving to a specific line 20 |03.6| Telling where you are 21 |03.7| Scrolling around 22 |03.8| Simple searches 23 |03.9| Simple search patterns 24 |03.10| Using marks 25 26 Next chapter: |usr_04.txt| Making small changes 27 Previous chapter: |usr_02.txt| The first steps in Vim 28 Table of contents: |usr_toc.txt| 29 30 ============================================================================== 31 *03.1* Word movement 32 33 To move the cursor forward one word, use the "w" command. Like most Vim 34 commands, you can use a numeric prefix to move past multiple words. For 35 example, "3w" moves three words. This figure shows how it works (starting at 36 the position marked with "x"): 37 38 This is a line with example text ~ 39 x-->-->->-----------------> 40 w w w 3w 41 42 Notice that "w" moves to the start of the next word if it already is at the 43 start of a word. 44 The "b" command moves backward to the start of the previous word: 45 46 This is a line with example text ~ 47 <----<--<-<---------<--x 48 b b b 2b b 49 50 There is also the "e" command that moves to the next end of a word and "ge", 51 which moves to the previous end of a word: 52 53 This is a line with example text ~ 54 <----<----x---->------------> 55 2ge ge e 2e 56 57 If you are at the last word of a line, the "w" command will take you to the 58 first word in the next line. Thus you can use this to move through a 59 paragraph, much faster than using "l". "b" does the same in the other 60 direction. 61 62 A word ends at a non-word character, such as a ".", "-" or ")". To change 63 what Vim considers to be a word, see the 'iskeyword' option. If you try this 64 out in the help directly, 'iskeyword' needs to be reset for the examples to 65 work: > 66 :set iskeyword& 67 It is also possible to move by white-space separated WORDs. This is not a 68 word in the normal sense, that's why the uppercase is used. The commands for 69 moving by WORDs are also uppercase, as this figure shows: 70 71 ge b w e 72 <- <- ---> ---> 73 This is-a line, with special/separated/words (and some more). ~ 74 <----- <----- --------------------> -----> 75 gE B W E 76 77 With this mix of lowercase and uppercase commands, you can quickly move 78 forward and backward through a paragraph. 79 80 ============================================================================== 81 *03.2* Moving to the start or end of a line 82 83 The "$" command moves the cursor to the end of a line. If your keyboard has 84 an <End> key it will do the same thing. 85 86 The "^" command moves to the first non-blank character of the line. The "0" 87 command (zero) moves to the very first character of the line, and the <Home> 88 key does the same thing. In a picture ("." indicates a space): 89 90 ^ 91 <-----------x 92 .....This is a line with example text ~ 93 <----------------x x--------------> 94 0 $ 95 96 (the "....." indicates blanks here) 97 98 The "$" command takes a count, like most movement commands. But moving to 99 the end of the line several times doesn't make sense. Therefore it causes the 100 editor to move to the end of another line. For example, "1$" moves you to 101 the end of the first line (the one you're on), "2$" to the end of the next 102 line, and so on. 103 The "0" command doesn't take a count argument, because the "0" would be 104 part of the count. Unexpectedly, using a count with "^" doesn't have any 105 effect. 106 107 ============================================================================== 108 *03.3* Moving to a character 109 110 One of the most useful movement commands is the single-character search 111 command. The command "fx" searches forward in the line for the single 112 character x. Hint: "f" stands for "Find". 113 For example, you are at the beginning of the following line. Suppose you 114 want to go to the h of human. Just execute the command "fh" and the cursor 115 will be positioned over the h: 116 117 To err is human. To really foul up you need a computer. ~ 118 ---------->---------------> 119 fh fy 120 121 This also shows that the command "fy" moves to the end of the word really. 122 You can specify a count; therefore, you can go to the "l" of "foul" with 123 "3fl": 124 125 To err is human. To really foul up you need a computer. ~ 126 ---------------------> 127 3fl 128 129 The "F" command searches to the left: 130 131 To err is human. To really foul up you need a computer. ~ 132 <--------------------- 133 Fh 134 135 The "tx" command works like the "fx" command, except it stops one character 136 before the searched character. Hint: "t" stands for "To". The backward 137 version of this command is "Tx". 138 139 To err is human. To really foul up you need a computer. ~ 140 <------------ -------------> 141 Th tn 142 143 These four commands can be repeated with ";". "," repeats in the other 144 direction. The cursor is never moved to another line. Not even when the 145 sentence continues. 146 147 Sometimes you will start a search, only to realize that you have typed the 148 wrong command. You type "f" to search backward, for example, only to realize 149 that you really meant "F". To abort a search, press <Esc>. So "f<Esc>" is an 150 aborted forward search and doesn't do anything. Note: <Esc> cancels most 151 operations, not just searches. 152 153 ============================================================================== 154 *03.4* Matching a parenthesis 155 156 When writing a program you often end up with nested () constructs. Then the 157 "%" command is very handy: It moves to the matching paren. If the cursor is 158 on a "(" it will move to the matching ")". If it's on a ")" it will move to 159 the matching "(". 160 161 % 162 <-----> 163 if (a == (b * c) / d) ~ 164 <----------------> 165 % 166 167 This also works for [] and {} pairs. (This can be defined with the 168 'matchpairs' option.) 169 170 When the cursor is not on a useful character, "%" will search forward to find 171 one. Thus if the cursor is at the start of the line of the previous example, 172 "%" will search forward and find the first "(". Then it moves to its match: 173 174 if (a == (b * c) / d) ~ 175 ---+----------------> 176 % 177 178 Other ways to move around code can be found in |usr_29.txt|. 179 180 ============================================================================== 181 *03.5* Moving to a specific line 182 183 If you are a C or C++ programmer, you are familiar with error messages such as 184 the following: 185 186 prog.c:33: j undeclared (first use in this function) ~ 187 188 This tells you that you might want to fix something on line 33. So how do you 189 find line 33? One way is to do "9999k" to go to the top of the file and "32j" 190 to go down thirty-two lines. It is not a good way, but it works. A much 191 better way of doing things is to use the "G" command. With a count, this 192 command positions you at the given line number. For example, "33G" puts you 193 on line 33. (For a better way of going through a compiler's error list, see 194 |usr_30.txt|, for information on the :make command.) 195 With no argument, "G" positions you at the end of the file. A quick way to 196 go to the start of a file use "gg". "1G" will do the same, but is a tiny bit 197 more typing. 198 199 | first line of a file ^ 200 | text text text text | 201 | text text text text | gg 202 7G | text text text text | 203 | text text text text 204 | text text text text 205 V text text text text | 206 text text text text | G 207 text text text text | 208 last line of a file V 209 210 Another way to move to a line is using the "%" command with a count. For 211 example "50%" moves you to halfway the file. "90%" goes to near the end. 212 213 The previous assumes that you want to move to a line in the file, no matter if 214 it's currently visible or not. What if you want to move to one of the lines 215 you can see? This figure shows the three commands you can use: 216 217 +---------------------------+ 218 H --> | text sample text | 219 | sample text | 220 | text sample text | 221 | sample text | 222 M --> | text sample text | 223 | sample text | 224 | text sample text | 225 | sample text | 226 L --> | text sample text | 227 +---------------------------+ 228 229 Hints: "H" stands for Home, "M" for Middle and "L" for Last. Alternatively, 230 "H" for High, "M" for Middle and "L" for Low. 231 232 ============================================================================== 233 *03.6* Telling where you are 234 235 To see where you are in a file, there are three ways: 236 237 1. Use the CTRL-G command. You get a message like this (assuming the 'ruler' 238 option is off): 239 240 "usr_03.txt" line 233 of 650 --35%-- col 45-52 ~ 241 242 This shows the name of the file you are editing, the line number where the 243 cursor is, the total number of lines, the percentage of the way through 244 the file and the column of the cursor. 245 Sometimes you will see a split column number. For example, "col 2-9". 246 This indicates that the cursor is positioned on the second character, but 247 because character one is a tab, occupying eight spaces worth of columns, 248 the screen column is 9. 249 250 2. Set the 'number' option. This will display a line number in front of 251 every line: > 252 253 :set number 254 < 255 To switch this off again: > 256 257 :set nonumber 258 < 259 Since 'number' is a boolean option, prepending "no" to its name has the 260 effect of switching it off. A boolean option has only these two values, 261 it is either on or off. 262 Vim has many options. Besides the boolean ones there are options with 263 a numerical value and string options. You will see examples of this where 264 they are used. 265 266 3. Set the 'ruler' option. This will display the cursor position in the 267 lower right corner of the Vim window: > 268 269 :set ruler 270 271 Using the 'ruler' option has the advantage that it doesn't take much room, 272 thus there is more space for your text. 273 274 ============================================================================== 275 *03.7* Scrolling around 276 277 The CTRL-U command scrolls down half a screen of text. Think of looking 278 through a viewing window at the text and moving this window up by half the 279 height of the window. Thus the window moves up over the text, which is 280 backward in the file. Don't worry if you have a little trouble remembering 281 which end is up. Most users have the same problem. 282 The CTRL-D command moves the viewing window down half a screen in the file, 283 thus scrolls the text up half a screen. 284 285 +----------------+ 286 | some text | 287 | some text | 288 | some text | 289 +---------------+ | some text | 290 | some text | CTRL-U --> | | 291 | | | 123456 | 292 | 123456 | +----------------+ 293 | 7890 | 294 | | +----------------+ 295 | example | CTRL-D --> | 7890 | 296 +---------------+ | | 297 | example | 298 | example | 299 | example | 300 | example | 301 +----------------+ 302 303 To scroll one line at a time use CTRL-E (scroll up) and CTRL-Y (scroll down). 304 Think of CTRL-E to give you one line Extra. (If you use MS-Windows compatible 305 key mappings CTRL-Y will redo a change instead of scroll.) 306 307 To scroll forward by a whole screen (except for two lines) use CTRL-F. To 308 scroll backwards, use CTRL-B. These should be easy to remember: F for 309 Forwards and B for Backwards. 310 311 A common issue is that after moving down many lines with "j" your cursor is at 312 the bottom of the screen. You would like to see the context of the line with 313 the cursor. That's done with the "zz" command. 314 315 +------------------+ +------------------+ 316 | earlier text | | earlier text | 317 | earlier text | | earlier text | 318 | earlier text | | earlier text | 319 | earlier text | zz --> | line with cursor | 320 | earlier text | | later text | 321 | earlier text | | later text | 322 | line with cursor | | later text | 323 +------------------+ +------------------+ 324 325 The "zt" command puts the cursor line at the top, "zb" at the bottom. There 326 are a few more scrolling commands, see |Q_sc|. To always keep a few lines of 327 context around the cursor, use the 'scrolloff' option. 328 329 ============================================================================== 330 *03.8* Simple searches 331 332 To search for a string, use the "/string" command. To find the word include, 333 for example, use the command: > 334 335 /include 336 337 You will notice that when you type the "/" the cursor jumps to the last line 338 of the Vim window, like with colon commands. That is where you type the word. 339 You can press the backspace key (backarrow or <BS>) to make corrections. Use 340 the <Left> and <Right> cursor keys when necessary. 341 Pressing <Enter> executes the command. 342 343 Note: 344 The characters .*[]^%/\?~$ have special meanings. If you want to use 345 them in a search you must put a \ in front of them. See below. 346 347 To find the next occurrence of the same string use the "n" command. Use this 348 to find the first #include after the cursor: > 349 350 /#include 351 352 And then type "n" several times. You will move to each #include in the text. 353 You can also use a count if you know which match you want. Thus "3n" finds 354 the third match. You can also use a count with "/": "4/the" goes to the 355 fourth match of "the". 356 357 The "?" command works like "/" but searches backwards: > 358 359 ?word 360 361 The "N" command repeats the last search the opposite direction. Thus using 362 "N" after a "/" command searches backwards, using "N" after "?" searches 363 forwards. 364 365 366 IGNORING CASE 367 368 Normally you have to type exactly what you want to find. If you don't care 369 about upper or lowercase in a word, set the 'ignorecase' option: > 370 371 :set ignorecase 372 373 If you now search for "word", it will also match "Word" and "WORD". To match 374 case again: > 375 376 :set noignorecase 377 378 379 HISTORY 380 381 Suppose you do three searches: > 382 383 /one 384 /two 385 /three 386 387 Now let's start searching by typing a simple "/" without pressing <Enter>. If 388 you press <Up> (the cursor key), Vim puts "/three" on the command line. 389 Pressing <Enter> at this point searches for three. If you do not press 390 <Enter>, but press <Up> instead, Vim changes the prompt to "/two". Another 391 press of <Up> moves you to "/one". 392 You can also use the <Down> cursor key to move through the history of 393 search commands in the other direction. 394 395 If you know what a previously used pattern starts with, and you want to use it 396 again, type that character before pressing <Up>. With the previous example, 397 you can type "/o<Up>" and Vim will put "/one" on the command line. 398 399 The commands starting with ":" also have a history. That allows you to recall 400 a previous command and execute it again. These two histories are separate. 401 402 403 SEARCHING FOR A WORD IN THE TEXT 404 405 Suppose you see the word "TheLongFunctionName" in the text and you want to 406 find the next occurrence of it. You could type "/TheLongFunctionName", but 407 that's a lot of typing. And when you make a mistake Vim won't find it. 408 There is an easier way: Position the cursor on the word and use the "*" 409 command. Vim will grab the word under the cursor and use it as the search 410 string. 411 The "#" command does the same in the other direction. You can prepend a 412 count: "3*" searches for the third occurrence of the word under the cursor. 413 414 415 SEARCHING FOR WHOLE WORDS 416 417 If you type "/the" it will also match "there". To only find words that end 418 in "the" use: > 419 420 /the\> 421 422 The "\>" item is a special marker that only matches at the end of a word. 423 Similarly "\<" only matches at the beginning of a word. Thus to search for 424 the word "the" only: > 425 426 /\<the\> 427 428 This does not match "there" or "soothe". Notice that the "*" and "#" commands 429 use these start-of-word and end-of-word markers to only find whole words (you 430 can use "g*" and "g#" to match partial words). 431 432 433 HIGHLIGHTING MATCHES 434 435 While editing a program you see a variable called "nr". You want to check 436 where it's used. You could move the cursor to "nr" and use the "*" command 437 and press "n" to go along all the matches. 438 There is another way. Type this command: > 439 440 :set hlsearch 441 442 If you now search for "nr", Vim will highlight all matches. That is a very 443 good way to see where the variable is used, without the need to type commands. 444 To switch this off: > 445 446 :set nohlsearch 447 448 Then you need to switch it on again if you want to use it for the next search 449 command. If you only want to remove the highlighting, use this command: > 450 451 :nohlsearch 452 453 This doesn't reset the option. Instead, it disables the highlighting. As 454 soon as you execute a search command, the highlighting will be used again. 455 Also for the "n" and "N" commands. 456 457 458 TUNING SEARCHES 459 460 There are a few options that change how searching works. These are the 461 essential ones: 462 > 463 :set incsearch 464 465 This makes Vim display the match for the string while you are still typing it. 466 Use this to check if the right match will be found. Then press <Enter> to 467 really jump to that location. Or type more to change the search string. 468 > 469 :set nowrapscan 470 471 This stops the search at the end of the file. Or, when you are searching 472 backwards, it stops the search at the start of the file. The 'wrapscan' 473 option is on by default, thus searching wraps around the end of the file. 474 475 476 INTERMEZZO 477 478 If you like one of the options mentioned before, and set it each time you use 479 Vim, you can put the command in your Vim startup file. 480 Edit the file, as mentioned at |not-compatible|. Or use this command to 481 find out where it is: > 482 483 :scriptnames 484 485 Edit the file, for example with: > 486 487 :edit ~/.vimrc 488 489 Then add a line with the command to set the option, just like you typed it in 490 Vim. Example: > 491 492 Go:set hlsearch<Esc> 493 494 "G" moves to the end of the file. "o" starts a new line, where you type the 495 ":set" command. You end insert mode with <Esc>. Then write and close the 496 file: > 497 498 ZZ 499 500 If you now start Vim again, the 'hlsearch' option will already be set. 501 502 ============================================================================== 503 *03.9* Simple search patterns 504 505 The Vim editor uses regular expressions to specify what to search for. 506 Regular expressions are an extremely powerful and compact way to specify a 507 search pattern. Unfortunately, this power comes at a price, because regular 508 expressions are a bit tricky to specify. 509 In this section we mention only a few essential ones. More about search 510 patterns and commands can be found in chapter 27 |usr_27.txt|. You can find 511 the full explanation here: |pattern|. 512 513 514 BEGINNING AND END OF A LINE 515 516 The ^ character matches the beginning of a line. On an English-US keyboard 517 you find it above the 6. The pattern "include" matches the word include 518 anywhere on the line. But the pattern "^include" matches the word include 519 only if it is at the beginning of a line. 520 The $ character matches the end of a line. Therefore, "was$" matches the 521 word was only if it is at the end of a line. 522 523 Let's mark the places where "/the" matches in this example line with "x"s: 524 525 the solder holding one of the chips melted and the ~ 526 xxx xxx xxx 527 528 Using "/the$" we find this match: 529 530 the solder holding one of the chips melted and the ~ 531 xxx 532 533 And with "/^the" we find this one: 534 the solder holding one of the chips melted and the ~ 535 xxx 536 537 You can try searching with "/^the$"; it will only match a single line 538 consisting entirely of "the". White space does matter here, thus if a line 539 contains a space after the word, like "the ", the pattern will not match. 540 541 542 MATCHING ANY SINGLE CHARACTER 543 544 The . (dot) character matches any existing character. For example, the 545 pattern "c.m" matches a string whose first character is a c, whose second 546 character is anything, and whose third character is m. Example: 547 548 We use a computer that became the cummin winter. ~ 549 xxx xxx xxx 550 551 552 MATCHING SPECIAL CHARACTERS 553 554 If you really want to match a dot, you must avoid its special meaning by 555 putting a backslash before it. 556 If you search for "ter.", you will find these matches: 557 558 We use a computer that became the cummin winter. ~ 559 xxxx xxxx 560 561 Searching for "ter\." only finds the second match. 562 563 ============================================================================== 564 *03.10* Using marks 565 566 When you make a jump to a position with the "G" command, Vim remembers the 567 position from before this jump. This position is called a mark. To go back 568 where you came from, use this command: > 569 570 `` 571 572 This ` is a backtick or open single-quote character. 573 If you use the same command a second time you will jump back again. That's 574 because the "`" command is a jump itself, and the position from before this 575 jump is remembered. 576 577 Generally, every time you do a command that can move the cursor further than 578 within the same line, this is called a jump. This includes the search 579 commands "/" and "n" (it doesn't matter how far away the match is). But not 580 the character searches with "fx" and "tx" or the word movements "w" and "e". 581 Also, "j" and "k" are not considered to be a jump, even when you use a 582 count to make them move the cursor quite a long way away. 583 584 The "``" command jumps back and forth, between two points. The CTRL-O command 585 jumps to older positions (Hint: O for older). CTRL-I then jumps back to newer 586 positions (Hint: for many common keyboard layouts, I is just next to O). 587 Consider this sequence of commands: > 588 589 33G 590 /^The 591 CTRL-O 592 593 You first jump to line 33, then search for a line that starts with "The". 594 Then with CTRL-O you jump back to line 33. Another CTRL-O takes you back to 595 where you started. If you now use CTRL-I you jump to line 33 again. And 596 to the match for "The" with another CTRL-I. 597 598 599 | example text ^ | 600 33G | example text | CTRL-O | CTRL-I 601 | example text | | 602 V line 33 text ^ V 603 | example text | | 604 /^The | example text | CTRL-O | CTRL-I 605 V There you are | V 606 example text 607 608 Note: 609 CTRL-I is the same as <Tab>. 610 611 The ":jumps" command gives a list of positions you jumped to. The entry which 612 you used last is marked with a ">". 613 614 615 NAMED MARKS *bookmark* 616 617 Vim enables you to place your own marks in the text. The command "ma" marks 618 the place under the cursor as mark a. You can place 26 marks (a through z) in 619 your text. You can't see them, it's just a position that Vim remembers. 620 To go to a mark, use the command `{mark}, where {mark} is the mark letter. 621 Thus to move to the a mark: 622 > 623 `a 624 625 The command "'mark" (single quotation mark, or apostrophe) moves you to the 626 beginning of the line containing the mark. This differs from the "`mark" 627 command, which also moves you to the marked column. 628 629 The marks can be very useful when working on two related parts in a file. 630 Suppose you have some text near the start of the file you need to look at, 631 while working on some text near the end of the file. 632 Move to the text at the start and place the s (start) mark there: > 633 634 ms 635 636 Then move to the text you want to work on and put the e (end) mark there: > 637 638 me 639 640 Now you can move around, and when you want to look at the start of the file, 641 you use this to jump there: > 642 643 's 644 645 Then you can use '' to jump back to where you were, or 'e to jump to the text 646 you were working on at the end. 647 There is nothing special about using s for start and e for end, they are 648 just easy to remember. 649 650 You can use this command to get a list of marks: > 651 652 :marks 653 654 You will notice a few special marks. These include: 655 656 ' The cursor position before doing a jump 657 " The cursor position when last editing the file 658 [ Start of the last change 659 ] End of the last change 660 661 ============================================================================== 662 663 Next chapter: |usr_04.txt| Making small changes 664 665 Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: