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