Christian Brabandt | f79695c | 2025-07-06 18:26:49 +0200 | [diff] [blame] | 1 | *usr_27.txt* For Vim version 9.1. Last change: 2025 Jul 06 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | VIM USER MANUAL - by Bram Moolenaar |
| 4 | |
| 5 | Search commands and patterns |
| 6 | |
| 7 | |
| 8 | In chapter 3 a few simple search patterns were mentioned |03.9|. Vim can do |
| 9 | much more complex searches. This chapter explains the most often used ones. |
Christian Brabandt | f79695c | 2025-07-06 18:26:49 +0200 | [diff] [blame] | 10 | A detailed specification can be found here: |pattern| |
| 11 | Options affecting how search is done can be found here: |search-options| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 12 | |
| 13 | |27.1| Ignoring case |
| 14 | |27.2| Wrapping around the file end |
| 15 | |27.3| Offsets |
| 16 | |27.4| Matching multiple times |
| 17 | |27.5| Alternatives |
| 18 | |27.6| Character ranges |
| 19 | |27.7| Character classes |
| 20 | |27.8| Matching a line break |
| 21 | |27.9| Examples |
| 22 | |
| 23 | Next chapter: |usr_28.txt| Folding |
| 24 | Previous chapter: |usr_26.txt| Repeating |
| 25 | Table of contents: |usr_toc.txt| |
| 26 | |
| 27 | ============================================================================== |
| 28 | *27.1* Ignoring case |
| 29 | |
| 30 | By default, Vim's searches are case sensitive. Therefore, "include", |
| 31 | "INCLUDE", and "Include" are three different words and a search will match |
| 32 | only one of them. |
| 33 | Now switch on the 'ignorecase' option: > |
| 34 | |
| 35 | :set ignorecase |
| 36 | |
| 37 | Search for "include" again, and now it will match "Include", "INCLUDE" and |
| 38 | "InClUDe". (Set the 'hlsearch' option to quickly see where a pattern |
| 39 | matches.) |
| 40 | You can switch this off again with: > |
| 41 | |
| 42 | :set noignorecase |
| 43 | |
Bram Moolenaar | 00a927d | 2010-05-14 23:24:24 +0200 | [diff] [blame] | 44 | But let's keep it set, and search for "INCLUDE". It will match exactly the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | same text as "include" did. Now set the 'smartcase' option: > |
| 46 | |
| 47 | :set ignorecase smartcase |
| 48 | |
| 49 | If you have a pattern with at least one uppercase character, the search |
| 50 | becomes case sensitive. The idea is that you didn't have to type that |
| 51 | uppercase character, so you must have done it because you wanted case to |
| 52 | match. That's smart! |
| 53 | With these two options set you find the following matches: |
| 54 | |
| 55 | pattern matches ~ |
| 56 | word word, Word, WORD, WoRd, etc. |
| 57 | Word Word |
| 58 | WORD WORD |
| 59 | WoRd WoRd |
| 60 | |
| 61 | |
| 62 | CASE IN ONE PATTERN |
| 63 | |
| 64 | If you want to ignore case for one specific pattern, you can do this by |
| 65 | prepending the "\c" string. Using "\C" will make the pattern to match case. |
| 66 | This overrules the 'ignorecase' and 'smartcase' options, when "\c" or "\C" is |
| 67 | used their value doesn't matter. |
| 68 | |
| 69 | pattern matches ~ |
| 70 | \Cword word |
| 71 | \CWord Word |
| 72 | \cword word, Word, WORD, WoRd, etc. |
| 73 | \cWord word, Word, WORD, WoRd, etc. |
| 74 | |
| 75 | A big advantage of using "\c" and "\C" is that it sticks with the pattern. |
| 76 | Thus if you repeat a pattern from the search history, the same will happen, no |
| 77 | matter if 'ignorecase' or 'smartcase' was changed. |
| 78 | |
| 79 | Note: |
| 80 | The use of "\" items in search patterns depends on the 'magic' option. |
Bram Moolenaar | 06b5d51 | 2010-05-22 15:37:44 +0200 | [diff] [blame] | 81 | In this chapter we will assume 'magic' is on, because that is the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 82 | standard and recommended setting. If you would change 'magic', many |
| 83 | search patterns would suddenly become invalid. |
| 84 | |
| 85 | Note: |
| 86 | If your search takes much longer than you expected, you can interrupt |
Bram Moolenaar | 5666fcd | 2019-12-26 14:35:26 +0100 | [diff] [blame] | 87 | it with CTRL-C on Unix and CTRL-Break on MS-Windows. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 88 | |
| 89 | ============================================================================== |
| 90 | *27.2* Wrapping around the file end |
| 91 | |
| 92 | By default, a forward search starts searching for the given string at the |
| 93 | current cursor location. It then proceeds to the end of the file. If it has |
| 94 | not found the string by that time, it starts from the beginning and searches |
| 95 | from the start of the file to the cursor location. |
| 96 | Keep in mind that when repeating the "n" command to search for the next |
| 97 | match, you eventually get back to the first match. If you don't notice this |
| 98 | you keep searching forever! To give you a hint, Vim displays this message: |
| 99 | |
| 100 | search hit BOTTOM, continuing at TOP ~ |
| 101 | |
| 102 | If you use the "?" command, to search in the other direction, you get this |
| 103 | message: |
| 104 | |
| 105 | search hit TOP, continuing at BOTTOM ~ |
| 106 | |
| 107 | Still, you don't know when you are back at the first match. One way to see |
| 108 | this is by switching on the 'ruler' option: > |
| 109 | |
| 110 | :set ruler |
| 111 | |
| 112 | Vim will display the cursor position in the lower righthand corner of the |
| 113 | window (in the status line if there is one). It looks like this: |
| 114 | |
| 115 | 101,29 84% ~ |
| 116 | |
| 117 | The first number is the line number of the cursor. Remember the line number |
| 118 | where you started, so that you can check if you passed this position again. |
| 119 | |
| 120 | |
| 121 | NOT WRAPPING |
| 122 | |
| 123 | To turn off search wrapping, use the following command: > |
| 124 | |
| 125 | :set nowrapscan |
| 126 | |
| 127 | Now when the search hits the end of the file, an error message displays: |
| 128 | |
| 129 | E385: search hit BOTTOM without match for: forever ~ |
| 130 | |
| 131 | Thus you can find all matches by going to the start of the file with "gg" and |
| 132 | keep searching until you see this message. |
| 133 | If you search in the other direction, using "?", you get: |
| 134 | |
| 135 | E384: search hit TOP without match for: forever ~ |
| 136 | |
| 137 | ============================================================================== |
| 138 | *27.3* Offsets |
| 139 | |
| 140 | By default, the search command leaves the cursor positioned on the beginning |
| 141 | of the pattern. You can tell Vim to leave it some other place by specifying |
| 142 | an offset. For the forward search command "/", the offset is specified by |
| 143 | appending a slash (/) and the offset: > |
| 144 | |
| 145 | /default/2 |
| 146 | |
| 147 | This command searches for the pattern "default" and then moves to the |
| 148 | beginning of the second line past the pattern. Using this command on the |
| 149 | paragraph above, Vim finds the word "default" in the first line. Then the |
| 150 | cursor is moved two lines down and lands on "an offset". |
| 151 | |
| 152 | If the offset is a simple number, the cursor will be placed at the beginning |
| 153 | of the line that many lines from the match. The offset number can be positive |
| 154 | or negative. If it is positive, the cursor moves down that many lines; if |
| 155 | negative, it moves up. |
| 156 | |
| 157 | |
| 158 | CHARACTER OFFSETS |
| 159 | |
| 160 | The "e" offset indicates an offset from the end of the match. It moves the |
| 161 | cursor onto the last character of the match. The command: > |
| 162 | |
| 163 | /const/e |
| 164 | |
| 165 | puts the cursor on the "t" of "const". |
| 166 | From that position, adding a number moves forward that many characters. |
| 167 | This command moves to the character just after the match: > |
| 168 | |
| 169 | /const/e+1 |
| 170 | |
| 171 | A positive number moves the cursor to the right, a negative number moves it to |
| 172 | the left. For example: > |
| 173 | |
| 174 | /const/e-1 |
| 175 | |
| 176 | moves the cursor to the "s" of "const". |
| 177 | |
| 178 | If the offset begins with "b", the cursor moves to the beginning of the |
| 179 | pattern. That's not very useful, since leaving out the "b" does the same |
| 180 | thing. It does get useful when a number is added or subtracted. The cursor |
| 181 | then goes forward or backward that many characters. For example: > |
| 182 | |
| 183 | /const/b+2 |
| 184 | |
| 185 | Moves the cursor to the beginning of the match and then two characters to the |
| 186 | right. Thus it lands on the "n". |
| 187 | |
| 188 | |
| 189 | REPEATING |
| 190 | |
| 191 | To repeat searching for the previously used search pattern, but with a |
| 192 | different offset, leave out the pattern: > |
| 193 | |
| 194 | /that |
| 195 | //e |
| 196 | |
| 197 | Is equal to: > |
| 198 | |
| 199 | /that/e |
| 200 | |
| 201 | To repeat with the same offset: > |
| 202 | |
| 203 | / |
| 204 | |
| 205 | "n" does the same thing. To repeat while removing a previously used offset: > |
| 206 | |
| 207 | // |
| 208 | |
| 209 | |
| 210 | SEARCHING BACKWARDS |
| 211 | |
| 212 | The "?" command uses offsets in the same way, but you must use "?" to separate |
| 213 | the offset from the pattern, instead of "/": > |
| 214 | |
| 215 | ?const?e-2 |
| 216 | |
| 217 | The "b" and "e" keep their meaning, they don't change direction with the use |
| 218 | of "?". |
| 219 | |
| 220 | |
| 221 | START POSITION |
| 222 | |
| 223 | When starting a search, it normally starts at the cursor position. When you |
| 224 | specify a line offset, this can cause trouble. For example: > |
| 225 | |
| 226 | /const/-2 |
| 227 | |
| 228 | This finds the next word "const" and then moves two lines up. If you |
Bram Moolenaar | 40962ec | 2018-01-28 22:47:25 +0100 | [diff] [blame] | 229 | use "n" to search again, Vim could start at the current position and find the |
| 230 | same "const" match. Then using the offset again, you would be back where you |
| 231 | started. You would be stuck! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 232 | It could be worse: Suppose there is another match with "const" in the next |
| 233 | line. Then repeating the forward search would find this match and move two |
| 234 | lines up. Thus you would actually move the cursor back! |
| 235 | |
| 236 | When you specify a character offset, Vim will compensate for this. Thus the |
| 237 | search starts a few characters forward or backward, so that the same match |
| 238 | isn't found again. |
| 239 | |
| 240 | ============================================================================== |
| 241 | *27.4* Matching multiple times |
| 242 | |
| 243 | The "*" item specifies that the item before it can match any number of times. |
| 244 | Thus: > |
| 245 | |
| 246 | /a* |
| 247 | |
| 248 | matches "a", "aa", "aaa", etc. But also "" (the empty string), because zero |
| 249 | times is included. |
| 250 | The "*" only applies to the item directly before it. Thus "ab*" matches |
| 251 | "a", "ab", "abb", "abbb", etc. To match a whole string multiple times, it |
| 252 | must be grouped into one item. This is done by putting "\(" before it and |
| 253 | "\)" after it. Thus this command: > |
| 254 | |
| 255 | /\(ab\)* |
| 256 | |
| 257 | Matches: "ab", "abab", "ababab", etc. And also "". |
| 258 | |
| 259 | To avoid matching the empty string, use "\+". This makes the previous item |
| 260 | match one or more times. > |
| 261 | |
| 262 | /ab\+ |
| 263 | |
| 264 | Matches "ab", "abb", "abbb", etc. It does not match "a" when no "b" follows. |
| 265 | |
| 266 | To match an optional item, use "\=". Example: > |
| 267 | |
| 268 | /folders\= |
| 269 | |
| 270 | Matches "folder" and "folders". |
| 271 | |
| 272 | |
| 273 | SPECIFIC COUNTS |
| 274 | |
| 275 | To match a specific number of items use the form "\{n,m}". "n" and "m" are |
| 276 | numbers. The item before it will be matched "n" to "m" times |inclusive|. |
| 277 | Example: > |
| 278 | |
| 279 | /ab\{3,5} |
| 280 | |
| 281 | matches "abbb", "abbbb" and "abbbbb". |
| 282 | When "n" is omitted, it defaults to zero. When "m" is omitted it defaults |
| 283 | to infinity. When ",m" is omitted, it matches exactly "n" times. |
| 284 | Examples: |
| 285 | |
| 286 | pattern match count ~ |
| 287 | \{,4} 0, 1, 2, 3 or 4 |
| 288 | \{3,} 3, 4, 5, etc. |
| 289 | \{0,1} 0 or 1, same as \= |
| 290 | \{0,} 0 or more, same as * |
| 291 | \{1,} 1 or more, same as \+ |
| 292 | \{3} 3 |
| 293 | |
| 294 | |
| 295 | MATCHING AS LITTLE AS POSSIBLE |
| 296 | |
| 297 | The items so far match as many characters as they can find. To match as few |
| 298 | as possible, use "\{-n,m}". It works the same as "\{n,m}", except that the |
| 299 | minimal amount possible is used. |
| 300 | For example, use: > |
| 301 | |
| 302 | /ab\{-1,3} |
| 303 | |
| 304 | Will match "ab" in "abbb". Actually, it will never match more than one b, |
| 305 | because there is no reason to match more. It requires something else to force |
| 306 | it to match more than the lower limit. |
| 307 | The same rules apply to removing "n" and "m". It's even possible to remove |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 308 | both of the numbers, resulting in "\{-}". This matches the item before it |
| 309 | zero or more times, as few as possible. The item by itself always matches |
| 310 | zero times. It is useful when combined with something else. Example: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 311 | |
| 312 | /a.\{-}b |
| 313 | |
| 314 | This matches "axb" in "axbxb". If this pattern would be used: > |
| 315 | |
| 316 | /a.*b |
| 317 | |
| 318 | It would try to match as many characters as possible with ".*", thus it |
| 319 | matches "axbxb" as a whole. |
| 320 | |
| 321 | ============================================================================== |
| 322 | *27.5* Alternatives |
| 323 | |
| 324 | The "or" operator in a pattern is "\|". Example: > |
| 325 | |
| 326 | /foo\|bar |
| 327 | |
| 328 | This matches "foo" or "bar". More alternatives can be concatenated: > |
| 329 | |
| 330 | /one\|two\|three |
| 331 | |
| 332 | Matches "one", "two" and "three". |
| 333 | To match multiple times, the whole thing must be placed in "\(" and "\)": > |
| 334 | |
| 335 | /\(foo\|bar\)\+ |
| 336 | |
| 337 | This matches "foo", "foobar", "foofoo", "barfoobar", etc. |
| 338 | Another example: > |
| 339 | |
| 340 | /end\(if\|while\|for\) |
| 341 | |
| 342 | This matches "endif", "endwhile" and "endfor". |
| 343 | |
| 344 | A related item is "\&". This requires that both alternatives match in the |
| 345 | same place. The resulting match uses the last alternative. Example: > |
| 346 | |
| 347 | /forever\&... |
| 348 | |
| 349 | This matches "for" in "forever". It will not match "fortuin", for example. |
| 350 | |
| 351 | ============================================================================== |
| 352 | *27.6* Character ranges |
| 353 | |
| 354 | To match "a", "b" or "c" you could use "/a\|b\|c". When you want to match all |
| 355 | letters from "a" to "z" this gets very long. There is a shorter method: > |
| 356 | |
| 357 | /[a-z] |
| 358 | |
| 359 | The [] construct matches a single character. Inside you specify which |
| 360 | characters to match. You can include a list of characters, like this: > |
| 361 | |
| 362 | /[0123456789abcdef] |
| 363 | |
| 364 | This will match any of the characters included. For consecutive characters |
| 365 | you can specify the range. "0-3" stands for "0123". "w-z" stands for "wxyz". |
| 366 | Thus the same command as above can be shortened to: > |
| 367 | |
| 368 | /[0-9a-f] |
| 369 | |
| 370 | To match the "-" character itself make it the first or last one in the range. |
| 371 | These special characters are accepted to make it easier to use them inside a |
| 372 | [] range (they can actually be used anywhere in the search pattern): |
| 373 | |
| 374 | \e <Esc> |
| 375 | \t <Tab> |
| 376 | \r <CR> |
| 377 | \b <BS> |
| 378 | |
| 379 | There are a few more special cases for [] ranges, see |/[]| for the whole |
| 380 | story. |
| 381 | |
| 382 | |
| 383 | COMPLEMENTED RANGE |
| 384 | |
| 385 | To avoid matching a specific character, use "^" at the start of the range. |
| 386 | The [] item then matches everything but the characters included. Example: > |
| 387 | |
| 388 | /"[^"]*" |
| 389 | < |
| 390 | " a double quote |
| 391 | [^"] any character that is not a double quote |
| 392 | * as many as possible |
| 393 | " a double quote again |
| 394 | |
| 395 | This matches "foo" and "3!x", including the double quotes. |
| 396 | |
| 397 | |
| 398 | PREDEFINED RANGES |
| 399 | |
| 400 | A number of ranges are used very often. Vim provides a shortcut for these. |
| 401 | For example: > |
| 402 | |
| 403 | /\a |
| 404 | |
| 405 | Finds alphabetic characters. This is equal to using "/[a-zA-Z]". Here are a |
| 406 | few more of these: |
| 407 | |
| 408 | item matches equivalent ~ |
| 409 | \d digit [0-9] |
| 410 | \D non-digit [^0-9] |
| 411 | \x hex digit [0-9a-fA-F] |
| 412 | \X non-hex digit [^0-9a-fA-F] |
| 413 | \s white space [ ] (<Tab> and <Space>) |
| 414 | \S non-white characters [^ ] (not <Tab> and <Space>) |
| 415 | \l lowercase alpha [a-z] |
| 416 | \L non-lowercase alpha [^a-z] |
| 417 | \u uppercase alpha [A-Z] |
| 418 | \U non-uppercase alpha [^A-Z] |
| 419 | |
| 420 | Note: |
| 421 | Using these predefined ranges works a lot faster than the character |
| 422 | range it stands for. |
| 423 | These items can not be used inside []. Thus "[\d\l]" does NOT work to |
| 424 | match a digit or lowercase alpha. Use "\(\d\|\l\)" instead. |
| 425 | |
| 426 | See |/\s| for the whole list of these ranges. |
| 427 | |
| 428 | ============================================================================== |
| 429 | *27.7* Character classes |
| 430 | |
| 431 | The character range matches a fixed set of characters. A character class is |
| 432 | similar, but with an essential difference: The set of characters can be |
| 433 | redefined without changing the search pattern. |
| 434 | For example, search for this pattern: > |
| 435 | |
| 436 | /\f\+ |
| 437 | |
Bram Moolenaar | 2547aa9 | 2020-07-26 17:00:44 +0200 | [diff] [blame] | 438 | The "\f" item stands for file name characters. Thus this matches a sequence |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 439 | of characters that can be a file name. |
| 440 | Which characters can be part of a file name depends on the system you are |
| 441 | using. On MS-Windows, the backslash is included, on Unix it is not. This is |
| 442 | specified with the 'isfname' option. The default value for Unix is: > |
| 443 | |
| 444 | :set isfname |
| 445 | isfname=@,48-57,/,.,-,_,+,,,#,$,%,~,= |
| 446 | |
| 447 | For other systems the default value is different. Thus you can make a search |
| 448 | pattern with "\f" to match a file name, and it will automatically adjust to |
| 449 | the system you are using it on. |
| 450 | |
| 451 | Note: |
| 452 | Actually, Unix allows using just about any character in a file name, |
| 453 | including white space. Including these characters in 'isfname' would |
| 454 | be theoretically correct. But it would make it impossible to find the |
| 455 | end of a file name in text. Thus the default value of 'isfname' is a |
| 456 | compromise. |
| 457 | |
| 458 | The character classes are: |
| 459 | |
| 460 | item matches option ~ |
| 461 | \i identifier characters 'isident' |
| 462 | \I like \i, excluding digits |
| 463 | \k keyword characters 'iskeyword' |
| 464 | \K like \k, excluding digits |
| 465 | \p printable characters 'isprint' |
| 466 | \P like \p, excluding digits |
| 467 | \f file name characters 'isfname' |
| 468 | \F like \f, excluding digits |
| 469 | |
| 470 | ============================================================================== |
| 471 | *27.8* Matching a line break |
| 472 | |
| 473 | Vim can find a pattern that includes a line break. You need to specify where |
| 474 | the line break happens, because all items mentioned so far don't match a line |
| 475 | break. |
| 476 | To check for a line break in a specific place, use the "\n" item: > |
| 477 | |
Bram Moolenaar | 85850f3 | 2019-07-19 22:05:51 +0200 | [diff] [blame] | 478 | /one\ntwo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 479 | |
Bram Moolenaar | 85850f3 | 2019-07-19 22:05:51 +0200 | [diff] [blame] | 480 | This will match at a line that ends in "one" and the next line starts with |
| 481 | "two". To match "one two" as well, you need to match a space or a line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 482 | break. The item to use for it is "\_s": > |
| 483 | |
Bram Moolenaar | 85850f3 | 2019-07-19 22:05:51 +0200 | [diff] [blame] | 484 | /one\_stwo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 485 | |
| 486 | To allow any amount of white space: > |
| 487 | |
Bram Moolenaar | 85850f3 | 2019-07-19 22:05:51 +0200 | [diff] [blame] | 488 | /one\_s\+two |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 489 | |
Bram Moolenaar | 85850f3 | 2019-07-19 22:05:51 +0200 | [diff] [blame] | 490 | This also matches when "one " is at the end of a line and " two" at the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 491 | start of the next one. |
| 492 | |
| 493 | "\s" matches white space, "\_s" matches white space or a line break. |
| 494 | Similarly, "\a" matches an alphabetic character, and "\_a" matches an |
| 495 | alphabetic character or a line break. The other character classes and ranges |
| 496 | can be modified in the same way by inserting a "_". |
| 497 | |
| 498 | Many other items can be made to match a line break by prepending "\_". For |
| 499 | example: "\_." matches any character or a line break. |
| 500 | |
| 501 | Note: |
| 502 | "\_.*" matches everything until the end of the file. Be careful with |
| 503 | this, it can make a search command very slow. |
| 504 | |
| 505 | Another example is "\_[]", a character range that includes a line break: > |
| 506 | |
| 507 | /"\_[^"]*" |
| 508 | |
| 509 | This finds a text in double quotes that may be split up in several lines. |
| 510 | |
| 511 | ============================================================================== |
| 512 | *27.9* Examples |
| 513 | |
| 514 | Here are a few search patterns you might find useful. This shows how the |
| 515 | items mentioned above can be combined. |
| 516 | |
| 517 | |
| 518 | FINDING A CALIFORNIA LICENSE PLATE |
| 519 | |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 520 | A sample license plate number is "1MGU103". It has one digit, three uppercase |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 521 | letters and three digits. Directly putting this into a search pattern: > |
| 522 | |
| 523 | /\d\u\u\u\d\d\d |
| 524 | |
| 525 | Another way is to specify that there are three digits and letters with a |
| 526 | count: > |
| 527 | |
| 528 | /\d\u\{3}\d\{3} |
| 529 | |
| 530 | Using [] ranges instead: > |
| 531 | |
| 532 | /[0-9][A-Z]\{3}[0-9]\{3} |
| 533 | |
| 534 | Which one of these you should use? Whichever one you can remember. The |
| 535 | simple way you can remember is much faster than the fancy way that you can't. |
| 536 | If you can remember them all, then avoid the last one, because it's both more |
| 537 | typing and slower to execute. |
| 538 | |
| 539 | |
| 540 | FINDING AN IDENTIFIER |
| 541 | |
| 542 | In C programs (and many other computer languages) an identifier starts with a |
| 543 | letter and further consists of letters and digits. Underscores can be used |
| 544 | too. This can be found with: > |
| 545 | |
| 546 | /\<\h\w*\> |
| 547 | |
| 548 | "\<" and "\>" are used to find only whole words. "\h" stands for "[A-Za-z_]" |
| 549 | and "\w" for "[0-9A-Za-z_]". |
| 550 | |
| 551 | Note: |
| 552 | "\<" and "\>" depend on the 'iskeyword' option. If it includes "-", |
| 553 | for example, then "ident-" is not matched. In this situation use: > |
| 554 | |
| 555 | /\w\@<!\h\w*\w\@! |
| 556 | < |
| 557 | This checks if "\w" does not match before or after the identifier. |
| 558 | See |/\@<!| and |/\@!|. |
| 559 | |
| 560 | ============================================================================== |
| 561 | |
| 562 | Next chapter: |usr_28.txt| Folding |
| 563 | |
Bram Moolenaar | d473c8c | 2018-08-11 18:00:22 +0200 | [diff] [blame] | 564 | Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: |