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