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