pattern.txt (64535B)
1 *pattern.txt* Nvim 2 3 4 VIM REFERENCE MANUAL by Bram Moolenaar 5 6 7 Patterns and search commands *pattern-searches* 8 9 The very basics can be found in section |03.9| of the user manual. A few more 10 explanations are in chapter 27 |usr_27.txt|. 11 12 Type |gO| to see the table of contents. 13 14 ============================================================================== 15 1. Search commands *search-commands* 16 17 */* 18 /{pattern}[/]<CR> Search forward for the [count]'th occurrence of 19 {pattern} |exclusive|. 20 21 /{pattern}/{offset}<CR> Search forward for the [count]'th occurrence of 22 {pattern} and go |{offset}| lines up or down. 23 |linewise|. 24 25 */<CR>* 26 /<CR> Search forward for the [count]'th occurrence of the 27 latest used pattern |last-pattern| with latest used 28 |{offset}|. 29 30 //{offset}<CR> Search forward for the [count]'th occurrence of the 31 latest used pattern |last-pattern| with new 32 |{offset}|. If {offset} is empty no offset is used. 33 34 *?* 35 ?{pattern}[?]<CR> Search backward for the [count]'th previous 36 occurrence of {pattern} |exclusive|. 37 38 ?{pattern}?{offset}<CR> Search backward for the [count]'th previous 39 occurrence of {pattern} and go |{offset}| lines up or 40 down |linewise|. 41 42 *?<CR>* 43 ?<CR> Search backward for the [count]'th occurrence of the 44 latest used pattern |last-pattern| with latest used 45 |{offset}|. 46 47 ??{offset}<CR> Search backward for the [count]'th occurrence of the 48 latest used pattern |last-pattern| with new 49 |{offset}|. If {offset} is empty no offset is used. 50 51 *n* 52 n Repeat the latest "/" or "?" [count] times. 53 If the cursor doesn't move the search is repeated with 54 count + 1. 55 |last-pattern| 56 57 *N* 58 N Repeat the latest "/" or "?" [count] times in 59 opposite direction. |last-pattern| 60 61 *star* *E348* *E349* 62 * Search forward for the [count]'th occurrence of the 63 word nearest to the cursor. The word used for the 64 search is the first of: 65 1. the keyword under the cursor 'iskeyword' 66 2. the first keyword after the cursor, in the 67 current line 68 3. the non-blank word under the cursor 69 4. the first non-blank word after the cursor, 70 in the current line 71 Only whole keywords are searched for, like with the 72 command "/\<keyword\>". |exclusive| 73 'ignorecase' is used, 'smartcase' is not. 74 *v_star-default* 75 {Visual}* In Visual mode, search forward for the current selection. 76 |default-mappings| 77 78 *#* 79 # Same as "*", but search backward. The pound sign 80 (character 163) also works. If the "#" key works as 81 backspace, try using "stty erase <BS>" before starting 82 Vim (<BS> is CTRL-H or a real backspace). 83 *v_#-default* 84 {Visual}# In Visual mode, search backward for the current selection. 85 |default-mappings| 86 87 *gstar* 88 g* Like "*", but don't put "\<" and "\>" around the word. 89 This makes the search also find matches that are not a 90 whole word. 91 92 *g#* 93 g# Like "#", but don't put "\<" and "\>" around the word. 94 This makes the search also find matches that are not a 95 whole word. 96 97 *gd* 98 gd Goto local Declaration. When the cursor is on a local 99 variable, this command will jump to its declaration. 100 This was made to work for C code, in other languages 101 it may not work well. 102 First Vim searches for the start of the current 103 function, just like "[[". If it is not found the 104 search stops in line 1. If it is found, Vim goes back 105 until a blank line is found. From this position Vim 106 searches for the keyword under the cursor, like with 107 "*", but lines that look like a comment are ignored 108 (see 'comments' option). 109 Note that this is not guaranteed to work, Vim does not 110 really check the syntax, it only searches for a match 111 with the keyword. If included files also need to be 112 searched use the commands listed in |include-search|. 113 After this command |n| searches forward for the next 114 match (not backward). 115 116 *gD* 117 gD Goto global Declaration. When the cursor is on a 118 global variable that is defined in the file, this 119 command will jump to its declaration. This works just 120 like "gd", except that the search for the keyword 121 always starts in line 1. 122 123 *1gd* 124 1gd Like "gd", but ignore matches inside a {} block that 125 ends before the cursor position. 126 127 *1gD* 128 1gD Like "gD", but ignore matches inside a {} block that 129 ends before the cursor position. 130 131 *CTRL-C* 132 CTRL-C Interrupt current (search) command. 133 In Normal mode, any pending command is aborted. 134 135 *:noh* *:nohlsearch* 136 :noh[lsearch] Stop the highlighting for the 'hlsearch' option. It 137 is automatically turned back on when using a search 138 command, or setting the 'hlsearch' option. 139 This command doesn't work in an autocommand, because 140 the highlighting state is saved and restored when 141 executing autocommands |autocmd-searchpat|. 142 Same thing for when invoking a user function. 143 144 145 While typing the search pattern the current match will be shown if the 146 'incsearch' option is on. Remember that you still have to finish the search 147 command with <CR> to actually position the cursor at the displayed match. Or 148 use <Esc> to abandon the search. 149 150 *nohlsearch-auto* 151 All matches for the last used search pattern will be highlighted if you set 152 the 'hlsearch' option. This can be suspended with the |:nohlsearch| command 153 or auto suspended with nohlsearch plugin. See |nohlsearch-install|. 154 155 156 When 'shortmess' does not include the "S" flag, Vim will automatically show an 157 index, on which the cursor is. This can look like this: > 158 159 [1/5] Cursor is on first of 5 matches. 160 [1/>99] Cursor is on first of more than 99 matches. 161 [>99/>99] Cursor is after 99 match of more than 99 matches. 162 [?/??] Unknown how many matches exists, generating the 163 statistics was aborted because of search timeout. 164 165 Note: the count does not take offset into account. 166 167 When no match is found you get the error: *E486* Pattern not found 168 Note that for the `:global` command, you get a normal message "Pattern not 169 found", for Vi compatibility. 170 For the |:s| command the "e" flag can be used to avoid the error message 171 |:s_flags|. 172 173 *search-options* 174 The following options affect how a search is performed in Vim: 175 'hlsearch' highlight matches 176 'ignorecase' ignore case when searching 177 'imsearch' use IME when entering the search pattern 178 'incsearch' show matches incrementally as the pattern is typed 179 'maxsearchcount' maximum number for the search count |shm-S| 180 'shortmess' suppress messages |shm-s|; show search count |shm-S| 181 'smartcase' override 'ignorecase' if pattern contains uppercase 182 'wrapscan' continue searching from the start of the file 183 184 *search-offset* *{offset}* 185 These commands search for the specified pattern. With "/" and "?" an 186 additional offset may be given. There are two types of offsets: line offsets 187 and character offsets. 188 189 The offset gives the cursor position relative to the found match: 190 [num] [num] lines downwards, in column 1 191 +[num] [num] lines downwards, in column 1 192 -[num] [num] lines upwards, in column 1 193 e[+num] [num] characters to the right of the end of the match 194 e[-num] [num] characters to the left of the end of the match 195 s[+num] [num] characters to the right of the start of the match 196 s[-num] [num] characters to the left of the start of the match 197 b[+num] [num] identical to s[+num] above (mnemonic: begin) 198 b[-num] [num] identical to s[-num] above (mnemonic: begin) 199 ;{pattern} perform another search, see |//;| 200 201 If a '-' or '+' is given but [num] is omitted, a count of one will be used. 202 When including an offset with 'e', the search becomes inclusive (the 203 character the cursor lands on is included in operations). 204 205 Examples: 206 207 pattern cursor position ~ 208 /test/+1 one line below "test", in column 1 209 /test/e on the last t of "test" 210 /test/s+2 on the 's' of "test" 211 /test/b-3 three characters before "test" 212 213 If one of these commands is used after an operator, the characters between 214 the cursor position before and after the search is affected. However, if a 215 line offset is given, the whole lines between the two cursor positions are 216 affected. 217 218 An example of how to search for matches with a pattern and change the match 219 with another word: > 220 /foo<CR> find "foo" 221 c//e<CR> change until end of match 222 bar<Esc> type replacement 223 //<CR> go to start of next match 224 c//e<CR> change until end of match 225 beep<Esc> type another replacement 226 etc. 227 < 228 *//;* *E386* 229 A very special offset is ';' followed by another search command. For example: > 230 231 /test 1/;/test 232 /test.*/+1;?ing? 233 234 The first one first finds the next occurrence of "test 1", and then the first 235 occurrence of "test" after that. 236 237 This is like executing two search commands after each other, except that: 238 - It can be used as a single motion command after an operator. 239 - The direction for a following "n" or "N" command comes from the first 240 search command. 241 - When an error occurs the cursor is not moved at all. 242 243 *last-pattern* 244 The last used pattern and offset are remembered. They can be used to repeat 245 the search, possibly in another direction or with another count. Note that 246 two patterns are remembered: One for "normal" search commands and one for the 247 substitute command ":s". Each time an empty pattern is given, the previously 248 used pattern is used. However, if there is no previous search command, a 249 previous substitute pattern is used, if possible. 250 251 The 'magic' option sticks with the last used pattern. If you change 'magic', 252 this will not change how the last used pattern will be interpreted. 253 The 'ignorecase' option does not do this. When 'ignorecase' is changed, it 254 will result in the pattern to match other text. 255 256 All matches for the last used search pattern will be highlighted if you set 257 the 'hlsearch' option. 258 259 To clear the last used search pattern: > 260 :let @/ = "" 261 This will not set the pattern to an empty string, because that would match 262 everywhere. The pattern is really cleared, like when starting Vim. 263 264 The search usually skips matches that don't move the cursor. Whether the next 265 match is found at the next character or after the skipped match depends on the 266 'c' flag in 'cpoptions'. See |cpo-c|. 267 with 'c' flag: "/..." advances 1 to 3 characters 268 without 'c' flag: "/..." advances 1 character 269 The unpredictability with the 'c' flag is caused by starting the search in the 270 first column, skipping matches until one is found past the cursor position. 271 272 When searching backwards, searching starts at the start of the line, using the 273 'c' flag in 'cpoptions' as described above. Then the last match before the 274 cursor position is used. 275 276 In Vi the ":tag" command sets the last search pattern when the tag is searched 277 for. In Vim this is not done, the previous search pattern is still 278 remembered, unless the 't' flag is present in 'cpoptions'. The search pattern 279 is always put in the search history. 280 281 If the 'wrapscan' option is on (which is the default), searches wrap around 282 the end of the buffer. If 'wrapscan' is not set, the backward search stops 283 at the beginning and the forward search stops at the end of the buffer. If 284 'wrapscan' is set and the pattern was not found the error message "pattern 285 not found" is given, and the cursor will not be moved. If 'wrapscan' is not 286 set the message becomes "search hit BOTTOM without match" when searching 287 forward, or "search hit TOP without match" when searching backward. If 288 wrapscan is set and the search wraps around the end of the file the message 289 "search hit TOP, continuing at BOTTOM" or "search hit BOTTOM, continuing at 290 TOP" is given when searching backwards or forwards respectively. This can be 291 switched off by setting the 's' flag in the 'shortmess' option. The highlight 292 method 'w' is used for this message (default: standout). 293 294 *search-range* 295 You can limit the search command "/" to a certain range of lines by including 296 \%>l items. For example, to match the word "limit" below line 199 and above 297 line 300: > 298 /\%>199l\%<300llimit 299 Also see |/\%>l|. 300 301 Another way is to use the ":substitute" command with the 'c' flag. Example: > 302 :.,300s/Pattern//gc 303 This command will search from the cursor position until line 300 for 304 "Pattern". At the match, you will be asked to type a character. Type 'q' to 305 stop at this match, type 'n' to find the next match. 306 307 The "*", "#", "g*" and "g#" commands look for a word near the cursor in this 308 order, the first one that is found is used: 309 - The keyword currently under the cursor. 310 - The first keyword to the right of the cursor, in the same line. 311 - The WORD currently under the cursor. 312 - The first WORD to the right of the cursor, in the same line. 313 The keyword may only contain letters and characters in 'iskeyword'. 314 The WORD may contain any non-blanks (<Tab>s and/or <Space>s). 315 Note that if you type with ten fingers, the characters are easy to remember: 316 the "#" is under your left hand middle finger (search to the left and up) and 317 the "*" is under your right hand middle finger (search to the right and down). 318 (this depends on your keyboard layout though). 319 320 *E956* 321 In very rare cases a regular expression is used recursively. This can happen 322 when executing a pattern takes a long time and when checking for messages on 323 channels a callback is invoked that also uses a pattern or an autocommand is 324 triggered. In most cases this should be fine, but if a pattern is in use when 325 it's used again it fails. Usually this means there is something wrong with 326 the pattern. 327 328 ============================================================================== 329 2. The definition of a pattern *search-pattern* *pattern* *[pattern]* 330 *regular-expression* *regexp* *Pattern* 331 *E383* *E476* 332 333 For starters, read chapter 27 of the user manual |usr_27.txt|. 334 335 */bar* */\bar* */pattern* 336 1. A pattern is one or more branches, separated by "\|". It matches anything 337 that matches one of the branches. Example: "foo\|beep" matches "foo" and 338 matches "beep". If more than one branch matches, the first one is used. 339 340 pattern ::= branch 341 or branch \| branch 342 or branch \| branch \| branch 343 etc. 344 345 */branch* */\&* 346 2. A branch is one or more concats, separated by "\&". It matches the last 347 concat, but only if all the preceding concats also match at the same 348 position. Examples: > 349 "foobeep\&..." matches "foo" in "foobeep". 350 ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" 351 < 352 branch ::= concat 353 or concat \& concat 354 or concat \& concat \& concat 355 etc. 356 357 */concat* 358 3. A concat is one or more pieces, concatenated. It matches a match for the 359 first piece, followed by a match for the second piece, etc. Example: 360 "f[0-9]b", first matches "f", then a digit and then "b". 361 362 concat ::= piece 363 or piece piece 364 or piece piece piece 365 etc. 366 367 */piece* 368 4. A piece is an atom, possibly followed by a multi, an indication of how many 369 times the atom can be matched. Example: "a*" matches any sequence of "a" 370 characters: "", "a", "aa", etc. See |/multi|. 371 372 piece ::= atom 373 or atom multi 374 375 */atom* 376 5. An atom can be one of a long list of items. Many atoms match one character 377 in the text. It is often an ordinary character or a character class. 378 Parentheses can be used to make a pattern into an atom. The "\z(\)" 379 construct is only for syntax highlighting. 380 381 atom ::= ordinary-atom |/ordinary-atom| 382 or \( pattern \) |/\(| 383 or \%( pattern \) |/\%(| 384 or \z( pattern \) |/\z(| 385 386 387 */\%#=* *two-engines* *NFA* 388 Vim includes two regexp engines: 389 1. An old, backtracking engine that supports everything. 390 2. A new, NFA engine that works much faster on some patterns, possibly slower 391 on some patterns. 392 *E1281* 393 Vim will automatically select the right engine for you. However, if you run 394 into a problem or want to specifically select one engine or the other, you can 395 prepend one of the following to the pattern: 396 397 \%#=0 Force automatic selection. Only has an effect when 398 'regexpengine' has been set to a non-zero value. 399 \%#=1 Force using the old engine. 400 \%#=2 Force using the NFA engine. 401 402 You can also use the 'regexpengine' option to change the default. 403 404 *E864* *E868* *E874* *E875* *E876* *E877* *E878* 405 If selecting the NFA engine and it runs into something that is not implemented 406 the pattern will not match. This is only useful when debugging Vim. 407 408 ============================================================================== 409 3. Magic */magic* 410 411 Some characters in the pattern, such as letters, are taken literally. They 412 match exactly the same character in the text. When preceded with a backslash 413 however, these characters may get a special meaning. For example, "a" matches 414 the letter "a", while "\a" matches any alphabetic character. 415 416 Other characters have a special meaning without a backslash. They need to be 417 preceded with a backslash to match literally. For example "." matches any 418 character while "\." matches a dot. 419 420 If a character is taken literally or not depends on the 'magic' option and the 421 items in the pattern mentioned next. The 'magic' option should always be set. 422 We mention the effect of 'nomagic' here for completeness, but we recommend 423 against using that. 424 */\m* */\M* 425 Use of "\m" makes the pattern after it be interpreted as if 'magic' is set, 426 ignoring the actual value of the 'magic' option. 427 Use of "\M" makes the pattern after it be interpreted as if 'nomagic' is used. 428 */\v* */\V* 429 Use of "\v" means that after it, all ASCII characters except '0'-'9', 'a'-'z', 430 'A'-'Z' and '_' have special meaning: "very magic" 431 432 Use of "\V" means that after it, only a backslash and the terminating 433 character (usually / or ?) have special meaning: "very nomagic" 434 435 Examples: 436 after: \v \m \M \V matches ~ 437 'magic' 'nomagic' 438 a a a a literal 'a' 439 \a \a \a \a any alphabetic character 440 . . \. \. any character 441 \. \. . . literal dot 442 $ $ $ \$ end-of-line 443 * * \* \* any number of the previous atom 444 ~ ~ \~ \~ latest substitute string 445 () \(\) \(\) \(\) group as an atom 446 | \| \| \| nothing: separates alternatives 447 \\ \\ \\ \\ literal backslash 448 \{ { { { literal curly brace 449 450 If you want to you can make a pattern immune to the 'magic' option being set 451 or not by putting "\m" or "\M" at the start of the pattern. 452 453 ============================================================================== 454 4. Overview of pattern items *pattern-overview* 455 *E865* *E866* *E867* *E869* 456 457 Overview of multi items. */multi* *E61* *E62* 458 More explanation and examples below, follow the links. *E64* *E871* 459 460 multi ~ 461 'magic' 'nomagic' matches of the preceding atom ~ 462 |/star| * \* 0 or more as many as possible 463 |/\+| \+ \+ 1 or more as many as possible 464 |/\=| \= \= 0 or 1 as many as possible 465 |/\?| \? \? 0 or 1 as many as possible 466 467 |/\{| \{n,m} \{n,m} n to m as many as possible 468 \{n} \{n} n exactly 469 \{n,} \{n,} at least n as many as possible 470 \{,m} \{,m} 0 to m as many as possible 471 \{} \{} 0 or more as many as possible (same as "*") 472 473 |/\{-| \{-n,m} \{-n,m} n to m as few as possible 474 \{-n} \{-n} n exactly 475 \{-n,} \{-n,} at least n as few as possible 476 \{-,m} \{-,m} 0 to m as few as possible 477 \{-} \{-} 0 or more as few as possible 478 479 *E59* 480 |/\@>| \@> \@> 1, like matching a whole pattern 481 |/\@=| \@= \@= nothing, requires a match |/zero-width| 482 |/\@!| \@! \@! nothing, requires NO match |/zero-width| 483 |/\@<=| \@<= \@<= nothing, requires a match behind |/zero-width| 484 |/\@<!| \@<! \@<! nothing, requires NO match behind |/zero-width| 485 486 487 Overview of ordinary atoms. */ordinary-atom* 488 More explanation and examples below, follow the links. 489 490 ordinary atom ~ 491 magic nomagic matches ~ 492 |/^| ^ ^ start-of-line (at start of pattern) |/zero-width| 493 |/\^| \^ \^ literal '^' 494 |/\_^| \_^ \_^ start-of-line (used anywhere) |/zero-width| 495 |/$| $ $ end-of-line (at end of pattern) |/zero-width| 496 |/\$| \$ \$ literal '$' 497 |/\_$| \_$ \_$ end-of-line (used anywhere) |/zero-width| 498 |/.| . \. any single character (not an end-of-line) 499 |/\_.| \_. \_. any single character or end-of-line 500 |/\<| \< \< beginning of a word |/zero-width| 501 |/\>| \> \> end of a word |/zero-width| 502 |/\zs| \zs \zs anything, sets start of match 503 |/\ze| \ze \ze anything, sets end of match 504 |/\%^| \%^ \%^ beginning of file |/zero-width| *E71* 505 |/\%$| \%$ \%$ end of file |/zero-width| 506 |/\%V| \%V \%V inside Visual area |/zero-width| 507 |/\%#| \%# \%# cursor position |/zero-width| 508 |/\%'m| \%'m \%'m mark m position |/zero-width| 509 |/\%l| \%23l \%23l in line 23 |/zero-width| 510 |/\%c| \%23c \%23c in column 23 |/zero-width| 511 |/\%v| \%23v \%23v in virtual column 23 |/zero-width| 512 513 Character classes: */character-classes* 514 magic nomagic matches ~ 515 |/\i| \i \i identifier character (see 'isident' option) 516 |/\I| \I \I like "\i", but excluding digits 517 |/\k| \k \k keyword character (see 'iskeyword' option) 518 |/\K| \K \K like "\k", but excluding digits 519 |/\f| \f \f file name character (see 'isfname' option) 520 |/\F| \F \F like "\f", but excluding digits 521 |/\p| \p \p printable character (see 'isprint' option) 522 |/\P| \P \P like "\p", but excluding digits 523 |/\s| \s \s whitespace character: <Space> and <Tab> 524 |/\S| \S \S non-whitespace character; opposite of \s 525 |/\d| \d \d digit: [0-9] 526 |/\D| \D \D non-digit: [^0-9] 527 |/\x| \x \x hex digit: [0-9A-Fa-f] 528 |/\X| \X \X non-hex digit: [^0-9A-Fa-f] 529 |/\o| \o \o octal digit: [0-7] 530 |/\O| \O \O non-octal digit: [^0-7] 531 |/\w| \w \w word character: [0-9A-Za-z_] 532 |/\W| \W \W non-word character: [^0-9A-Za-z_] 533 |/\h| \h \h head of word character: [A-Za-z_] 534 |/\H| \H \H non-head of word character: [^A-Za-z_] 535 |/\a| \a \a alphabetic character: [A-Za-z] 536 |/\A| \A \A non-alphabetic character: [^A-Za-z] 537 |/\l| \l \l lowercase character: [a-z] 538 |/\L| \L \L non-lowercase character: [^a-z] 539 |/\u| \u \u uppercase character: [A-Z] 540 |/\U| \U \U non-uppercase character [^A-Z] 541 |/\_| \_x \_x where x is any of the characters above: character 542 class with end-of-line included 543 (end of character classes) 544 545 magic nomagic matches ~ 546 |/\e| \e \e <Esc> 547 |/\t| \t \t <Tab> 548 |/\r| \r \r <CR> 549 |/\b| \b \b <BS> 550 |/\n| \n \n end-of-line 551 |/~| ~ \~ last given substitute string 552 |/\1| \1 \1 same string as matched by first \(\) 553 |/\2| \2 \2 Like "\1", but uses second \(\) 554 ... 555 |/\9| \9 \9 Like "\1", but uses ninth \(\) 556 *E68* 557 |/\z1| \z1 \z1 only for syntax highlighting, see |:syn-ext-match| 558 ... 559 |/\z1| \z9 \z9 only for syntax highlighting, see |:syn-ext-match| 560 561 x x a character with no special meaning matches itself 562 563 |/[]| [] \[] any character specified inside the [] 564 |/\%[]| \%[] \%[] a sequence of optionally matched atoms 565 566 |/\c| \c \c ignore case, do not use the 'ignorecase' option 567 |/\C| \C \C match case, do not use the 'ignorecase' option 568 |/\Z| \Z \Z ignore differences in Unicode "combining characters". 569 Useful when searching voweled Hebrew or Arabic text. 570 571 magic nomagic matches ~ 572 |/\m| \m \m 'magic' on for the following chars in the pattern 573 |/\M| \M \M 'magic' off for the following chars in the pattern 574 |/\v| \v \v the following chars in the pattern are "very magic" 575 |/\V| \V \V the following chars in the pattern are "very nomagic" 576 |/\%#=| \%#=1 \%#=1 select regexp engine |/zero-width| 577 578 |/\%d| \%d \%d match specified decimal character (eg \%d123) 579 |/\%x| \%x \%x match specified hex character (eg \%x2a) 580 |/\%o| \%o \%o match specified octal character (eg \%o040) 581 |/\%u| \%u \%u match specified multibyte character (eg \%u20ac) 582 |/\%U| \%U \%U match specified large multibyte character (eg 583 \%U12345678) 584 |/\%C| \%C \%C match any composing characters 585 586 Example matches ~ 587 \<\I\i* or 588 \<\h\w* 589 \<[a-zA-Z_][a-zA-Z0-9_]* 590 An identifier (e.g., in a C program). 591 592 \(\.$\|\. \) A period followed by <EOL> or a space. 593 594 [.!?][])"']*\($\|[ ]\) A search pattern that finds the end of a sentence, 595 with almost the same definition as the ")" command. 596 597 cat\Z Both "cat" and "càt" ("a" followed by 0x0300) 598 Does not match "càt" (character 0x00e0), even 599 though it may look the same. 600 601 602 ============================================================================== 603 5. Multi items *pattern-multi-items* 604 605 An atom can be followed by an indication of how many times the atom can be 606 matched and in what way. This is called a multi. See |/multi| for an 607 overview. 608 609 */star* */\star* 610 * (use \* when 'magic' is not set) 611 Matches 0 or more of the preceding atom, as many as possible. 612 Example 'nomagic' matches > 613 a* a\* "", "a", "aa", "aaa", etc. 614 .* \.\* anything, also an empty string, no end-of-line 615 \_.* \_.\* everything up to the end of the buffer 616 \_.*END \_.\*END everything up to and including the last "END" 617 in the buffer 618 < 619 Exception: When "*" is used at the start of the pattern or just after 620 "^" it matches the star character. 621 622 Be aware that repeating "\_." can match a lot of text and take a long 623 time. For example, `\_.*END` matches all text from the current 624 position to the last occurrence of "END" in the file. Since the "*" 625 will match as many as possible, this first skips over all lines until 626 the end of the file and then tries matching "END", backing up one 627 character at a time. 628 629 */\+* 630 \+ Matches 1 or more of the preceding atom, as many as possible. 631 Example matches ~ 632 ^.\+$ any non-empty line 633 \s\+ white space of at least one character 634 635 */\=* 636 \= Matches 0 or 1 of the preceding atom, as many as possible. 637 Example matches ~ 638 foo\= "fo" and "foo" 639 640 */\?* 641 \? Just like \=. Cannot be used when searching backwards with the "?" 642 command. 643 644 */\{* *E60* *E554* *E870* 645 \{n,m} Matches n to m of the preceding atom, as many as possible 646 \{n} Matches n of the preceding atom 647 \{n,} Matches at least n of the preceding atom, as many as possible 648 \{,m} Matches 0 to m of the preceding atom, as many as possible 649 \{} Matches 0 or more of the preceding atom, as many as possible (like "*") 650 */\{-* 651 \{-n,m} matches n to m of the preceding atom, as few as possible 652 \{-n} matches n of the preceding atom 653 \{-n,} matches at least n of the preceding atom, as few as possible 654 \{-,m} matches 0 to m of the preceding atom, as few as possible 655 \{-} matches 0 or more of the preceding atom, as few as possible 656 657 n and m are positive decimal numbers or zero 658 *non-greedy* 659 If a "-" appears immediately after the "{", then a shortest match 660 first algorithm is used (see example below). In particular, "\{-}" is 661 the same as "*" but uses the shortest match first algorithm. BUT: A 662 match that starts earlier is preferred over a shorter match: "a\{-}b" 663 matches "aaab" in "xaaab". 664 665 Example matches ~ 666 ab\{2,3}c "abbc" or "abbbc" 667 a\{5} "aaaaa" 668 ab\{2,}c "abbc", "abbbc", "abbbbc", etc. 669 ab\{,3}c "ac", "abc", "abbc" or "abbbc" 670 a[bc]\{3}d "abbbd", "abbcd", "acbcd", "acccd", etc. 671 a\(bc\)\{1,2}d "abcd" or "abcbcd" 672 a[bc]\{-}[cd] "abc" in "abcd" 673 a[bc]*[cd] "abcd" in "abcd" 674 675 The } may optionally be preceded with a backslash: \{n,m\}. 676 677 */\@=* 678 \@= Matches the preceding atom with zero width. 679 Like "(?=pattern)" in Perl. 680 Example matches ~ 681 foo\(bar\)\@= "foo" in "foobar" 682 foo\(bar\)\@=foo nothing 683 */zero-width* 684 When using "\@=" (or "^", "$", "\<", "\>") no characters are included 685 in the match. These items are only used to check if a match can be 686 made. This can be tricky, because a match with following items will 687 be done in the same position. The last example above will not match 688 "foobarfoo", because it tries match "foo" in the same position where 689 "bar" matched. 690 691 Note that using "\&" works the same as using "\@=": "foo\&.." is the 692 same as "\(foo\)\@=..". But using "\&" is easier, you don't need the 693 parentheses. 694 695 696 */\@!* 697 \@! Matches with zero width if the preceding atom does NOT match at the 698 current position. |/zero-width| 699 Like "(?!pattern)" in Perl. 700 Example matches ~ 701 foo\(bar\)\@! any "foo" not followed by "bar" 702 a.\{-}p\@! "a", "ap", "app", "appp", etc. not immediately 703 followed by a "p" 704 if \(\(then\)\@!.\)*$ "if " not followed by "then" 705 706 Using "\@!" is tricky, because there are many places where a pattern 707 does not match. `a.*p\@!` will match from an "a" to the end of the 708 line, because `.*` can match all characters in the line and the "p" 709 doesn't match at the end of the line. "a.\{-}p\@!" will match any 710 "a", "ap", "app", etc. that isn't followed by a "p", because the "." 711 can match a "p" and "p\@!" doesn't match after that. 712 713 You can't use "\@!" to look for a non-match before the matching 714 position: "\(foo\)\@!bar" will match "bar" in "foobar", because at the 715 position where "bar" matches, "foo" does not match. To avoid matching 716 "foobar" you could use "\(foo\)\@!...bar", but that doesn't match a 717 bar at the start of a line. Use "\(foo\)\@<!bar". 718 719 Useful example: to find "foo" in a line that does not contain "bar": > 720 /^\%(.*bar\)\@!.*\zsfoo 721 < This pattern first checks that there is not a single position in the 722 line where "bar" matches. If `.*bar` matches somewhere the \@! will 723 reject the pattern. When there is no match any "foo" will be found. 724 The "\zs" is to have the match start just before "foo". 725 726 */\@<=* 727 \@<= Matches with zero width if the preceding atom matches just before what 728 follows. |/zero-width| 729 Like "(?<=pattern)" in Perl, but Vim allows non-fixed-width patterns. 730 Example matches ~ 731 \(an\_s\+\)\@<=file "file" after "an" and white space or an 732 end-of-line 733 For speed it's often much better to avoid this multi. Try using "\zs" 734 instead |/\zs|. To match the same as the above example: 735 an\_s\+\zsfile 736 At least set a limit for the look-behind, see below. 737 738 "\@<=" and "\@<!" check for matches just before what follows. 739 Theoretically these matches could start anywhere before this position. 740 But to limit the time needed, only the line where what follows matches 741 is searched, and one line before that (if there is one). This should 742 be sufficient to match most things and not be too slow. 743 744 In the old regexp engine the part of the pattern after "\@<=" and 745 "\@<!" are checked for a match first, thus things like "\1" don't work 746 to reference \(\) inside the preceding atom. It does work the other 747 way around: 748 Bad example matches ~ 749 \%#=1\1\@<=,\([a-z]\+\) ",abc" in "abc,abc" 750 751 However, the new regexp engine works differently, it is better to not 752 rely on this behavior, do not use \@<= if it can be avoided: 753 Example matches ~ 754 \([a-z]\+\)\zs,\1 ",abc" in "abc,abc" 755 756 \@123<= 757 Like "\@<=" but only look back 123 bytes. This avoids trying lots 758 of matches that are known to fail and make executing the pattern very 759 slow. Example, check if there is a "<" just before "span": 760 /<\@1<=span 761 This will try matching "<" only one byte before "span", which is the 762 only place that works anyway. 763 After crossing a line boundary, the limit is relative to the end of 764 the line. Thus the characters at the start of the line with the match 765 are not counted (this is just to keep it simple). 766 The number zero is the same as no limit. 767 768 */\@<!* 769 \@<! Matches with zero width if the preceding atom does NOT match just 770 before what follows. Thus this matches if there is no position in the 771 current or previous line where the atom matches such that it ends just 772 before what follows. |/zero-width| 773 Like "(?<!pattern)" in Perl, but Vim allows non-fixed-width patterns. 774 The match with the preceding atom is made to end just before the match 775 with what follows, thus an atom that ends in `.*` will work. 776 Warning: This can be slow (because many positions need to be checked 777 for a match). Use a limit if you can, see below. 778 Example matches > 779 \(foo\)\@<!bar any "bar" that's not in "foobar" 780 \(\/\/.*\)\@<!in "in" which is not after "//" 781 < 782 \@123<! 783 Like "\@<!" but only look back 123 bytes. This avoids trying lots of 784 matches that are known to fail and make executing the pattern very 785 slow. 786 787 */\@>* 788 \@> Matches the preceding atom like matching a whole pattern. 789 Like "(?>pattern)" in Perl. 790 Example matches ~ 791 \(a*\)\@>a nothing (the "a*" takes all the "a"'s, there can't be 792 another one following) 793 794 This matches the preceding atom as if it was a pattern by itself. If 795 it doesn't match, there is no retry with shorter sub-matches or 796 anything. Observe this difference: "a*b" and "a*ab" both match 797 "aaab", but in the second case the "a*" matches only the first two 798 "a"s. "\(a*\)\@>ab" will not match "aaab", because the "a*" matches 799 the "aaa" (as many "a"s as possible), thus the "ab" can't match. 800 801 802 ============================================================================== 803 6. Ordinary atoms *pattern-atoms* 804 805 An ordinary atom can be: 806 807 */^* 808 ^ At beginning of pattern or after "\|", "\(", "\%(" or "\n": matches 809 start-of-line; at other positions, matches literal '^'. |/zero-width| 810 Example matches ~ 811 ^beep( the start of the C function "beep" (probably). 812 813 */\^* 814 \^ Matches literal '^'. Can be used at any position in the pattern, but 815 not inside []. 816 817 */\_^* 818 \_^ Matches start-of-line. |/zero-width| Can be used at any position in 819 the pattern, but not inside []. 820 Example matches ~ 821 \_s*\_^foo white space and blank lines and then "foo" at 822 start-of-line 823 824 */$* 825 $ At end of pattern or in front of "\|", "\)" or "\n" ('magic' on): 826 matches end-of-line <EOL>; at other positions, matches literal '$'. 827 |/zero-width| 828 829 */\$* 830 \$ Matches literal '$'. Can be used at any position in the pattern, but 831 not inside []. 832 833 */\_$* 834 \_$ Matches end-of-line. |/zero-width| Can be used at any position in the 835 pattern, but not inside []. Note that "a\_$b" never matches, since 836 "b" cannot match an end-of-line. Use "a\nb" instead |/\n|. 837 Example matches ~ 838 foo\_$\_s* "foo" at end-of-line and following white space and 839 blank lines 840 841 . (with 'nomagic': \.) */.* */\.* 842 Matches any single character, but not an end-of-line. 843 844 */\_.* 845 \_. Matches any single character or end-of-line. 846 Careful: `\_.*` matches all text to the end of the buffer! 847 848 */\<* 849 \< Matches the beginning of a word: The next char is the first char of a 850 word. The 'iskeyword' option specifies what is a word character. 851 |/zero-width| 852 853 */\>* 854 \> Matches the end of a word: The previous char is the last char of a 855 word. The 'iskeyword' option specifies what is a word character. 856 |/zero-width| 857 858 */\zs* 859 \zs Matches at any position, but not inside [], and sets the start of the 860 match there: The next char is the first char of the whole match. 861 |/zero-width| 862 Example: > 863 /^\s*\zsif 864 < matches an "if" at the start of a line, ignoring white space. 865 Can be used multiple times, the last one encountered in a matching 866 branch is used. Example: > 867 /\(.\{-}\zsFab\)\{3} 868 < Finds the third occurrence of "Fab". 869 This cannot be followed by a multi. *E888* 870 871 */\ze* 872 \ze Matches at any position, but not inside [], and sets the end of the 873 match there: The previous char is the last char of the whole match. 874 |/zero-width| 875 Can be used multiple times, the last one encountered in a matching 876 branch is used. 877 Example: "end\ze\(if\|for\)" matches the "end" in "endif" and 878 "endfor". 879 This cannot be followed by a multi. |E888| 880 881 */\%^* *start-of-file* 882 \%^ Matches start of the file. When matching with a string, matches the 883 start of the string. 884 For example, to find the first "VIM" in a file: > 885 /\%^\_.\{-}\zsVIM 886 < 887 */\%$* *end-of-file* 888 \%$ Matches end of the file. When matching with a string, matches the 889 end of the string. 890 Note that this does NOT find the last "VIM" in a file: > 891 /VIM\_.\{-}\%$ 892 < It will find the next VIM, because the part after it will always 893 match. This one will find the last "VIM" in the file: > 894 /VIM\ze\(\(VIM\)\@!\_.\)*\%$ 895 < This uses |/\@!| to ascertain that "VIM" does NOT match in any 896 position after the first "VIM". 897 Searching from the end of the file backwards is easier! 898 899 */\%V* 900 \%V Match inside the Visual area. When Visual mode has already been 901 stopped match in the area that |gv| would reselect. 902 This is a |/zero-width| match. To make sure the whole pattern is 903 inside the Visual area put it at the start and just before the end of 904 the pattern, e.g.: > 905 /\%Vfoo.*ba\%Vr 906 < This also works if only "foo bar" was Visually selected. This: > 907 /\%Vfoo.*bar\%V 908 < would match "foo bar" if the Visual selection continues after the "r". 909 Only works for the current buffer. 910 911 */\%#* *cursor-position* 912 \%# Matches with the cursor position. Only works when matching in a 913 buffer displayed in a window. 914 WARNING: When the cursor is moved after the pattern was used, the 915 result becomes invalid. Vim doesn't automatically update the matches. 916 This is especially relevant for syntax highlighting and 'hlsearch'. 917 In other words: When the cursor moves the display isn't updated for 918 this change. An update is done for lines which are changed (the whole 919 line is updated) or when using the |CTRL-L| command (the whole screen 920 is updated). Example, to highlight the word under the cursor: > 921 /\k*\%#\k* 922 < When 'hlsearch' is set and you move the cursor around and make changes 923 this will clearly show when the match is updated or not. 924 925 */\%'m* */\%<'m* */\%>'m* 926 \%'m Matches with the position of mark m. 927 \%<'m Matches before the position of mark m. 928 \%>'m Matches after the position of mark m. 929 Example, to highlight the text from mark 's to 'e: > 930 /.\%>'s.*\%<'e.. 931 < Note that two dots are required to include mark 'e in the match. That 932 is because "\%<'e" matches at the character before the 'e mark, and 933 since it's a |/zero-width| match it doesn't include that character. 934 WARNING: When the mark is moved after the pattern was used, the result 935 becomes invalid. Vim doesn't automatically update the matches. 936 Similar to moving the cursor for "\%#" |/\%#|. 937 938 */\%l* */\%>l* */\%<l* *E951* *E1204* 939 \%23l Matches in a specific line. 940 \%<23l Matches above a specific line (lower line number). 941 \%>23l Matches below a specific line (higher line number). 942 \%.l Matches at the cursor line. 943 \%<.l Matches above the cursor line. 944 \%>.l Matches below the cursor line. 945 These six can be used to match specific lines in a buffer. The "23" 946 can be any line number. The first line is 1. 947 WARNING: When inserting or deleting lines Vim does not automatically 948 update the matches. This means Syntax highlighting quickly becomes 949 wrong. Also when referring to the cursor position (".") and 950 the cursor moves the display isn't updated for this change. An update 951 is done when using the |CTRL-L| command (the whole screen is updated). 952 Example, to highlight the line where the cursor currently is: > 953 :exe '/\%' .. line(".") .. 'l' 954 < Alternatively use: > 955 /\%.l 956 < When 'hlsearch' is set and you move the cursor around and make changes 957 this will clearly show when the match is updated or not. 958 959 */\%c* */\%>c* */\%<c* 960 \%23c Matches in a specific column. 961 \%<23c Matches before a specific column. 962 \%>23c Matches after a specific column. 963 \%.c Matches at the cursor column. 964 \%<.c Matches before the cursor column. 965 \%>.c Matches after the cursor column. 966 These six can be used to match specific columns in a buffer or string. 967 The "23" can be any column number. The first column is 1. Actually, 968 the column is the byte number (thus it's not exactly right for 969 multibyte characters). 970 WARNING: When inserting or deleting text Vim does not automatically 971 update the matches. This means Syntax highlighting quickly becomes 972 wrong. Also when referring to the cursor position (".") and 973 the cursor moves the display isn't updated for this change. An update 974 is done when using the |CTRL-L| command (the whole screen is updated). 975 Example, to highlight the column where the cursor currently is: > 976 :exe '/\%' .. col(".") .. 'c' 977 < Alternatively use: > 978 /\%.c 979 < When 'hlsearch' is set and you move the cursor around and make changes 980 this will clearly show when the match is updated or not. 981 Example for matching a single byte in column 44: > 982 /\%>43c.\%<46c 983 < Note that "\%<46c" matches in column 45 when the "." matches a byte in 984 column 44. 985 */\%v* */\%>v* */\%<v* 986 \%23v Matches in a specific virtual column. 987 \%<23v Matches before a specific virtual column. 988 \%>23v Matches after a specific virtual column. 989 \%.v Matches at the current virtual column. 990 \%<.v Matches before the current virtual column. 991 \%>.v Matches after the current virtual column. 992 These six can be used to match specific virtual columns in a buffer or 993 string. When not matching with a buffer in a window, the option 994 values of the current window are used (e.g., 'tabstop'). 995 The "23" can be any column number. The first column is 1. 996 Note that some virtual column positions will never match, because they 997 are halfway through a tab or other character that occupies more than 998 one screen character. 999 WARNING: When inserting or deleting text Vim does not automatically 1000 update highlighted matches. This means Syntax highlighting quickly 1001 becomes wrong. Also when referring to the cursor position (".") and 1002 the cursor moves the display isn't updated for this change. An update 1003 is done when using the |CTRL-L| command (the whole screen is updated). 1004 Example, to highlight all the characters after virtual column 72: > 1005 /\%>72v.* 1006 < When 'hlsearch' is set and you move the cursor around and make changes 1007 this will clearly show when the match is updated or not. 1008 To match the text up to column 17: > 1009 /^.*\%17v 1010 < To match all characters after the current virtual column (where the 1011 cursor is): > 1012 /\%>.v.* 1013 < Column 17 is not included, because this is a |/zero-width| match. To 1014 include the column use: > 1015 /^.*\%17v. 1016 < This command does the same thing, but also matches when there is no 1017 character in column 17: > 1018 /^.*\%<18v. 1019 < Note that without the "^" to anchor the match in the first column, 1020 this will also highlight column 17: > 1021 /.*\%17v 1022 < Column 17 is highlighted by 'hlsearch' because there is another match 1023 where `.*` matches zero characters. 1024 1025 1026 Character classes: 1027 \i identifier character (see 'isident' option) */\i* 1028 \I like "\i", but excluding digits */\I* 1029 \k keyword character (see 'iskeyword' option) */\k* 1030 \K like "\k", but excluding digits */\K* 1031 \f file name character (see 'isfname' option) */\f* 1032 \F like "\f", but excluding digits */\F* 1033 \p printable character (see 'isprint' option) */\p* 1034 \P like "\p", but excluding digits */\P* 1035 1036 NOTE: the above also work for multibyte characters. The ones below only 1037 match ASCII characters, as indicated by the range. 1038 1039 *whitespace* *white-space* 1040 \s whitespace character: <Space> and <Tab> */\s* 1041 \S non-whitespace character; opposite of \s */\S* 1042 \d digit: [0-9] */\d* 1043 \D non-digit: [^0-9] */\D* 1044 \x hex digit: [0-9A-Fa-f] */\x* 1045 \X non-hex digit: [^0-9A-Fa-f] */\X* 1046 \o octal digit: [0-7] */\o* 1047 \O non-octal digit: [^0-7] */\O* 1048 \w word character: [0-9A-Za-z_] */\w* 1049 \W non-word character: [^0-9A-Za-z_] */\W* 1050 \h head of word character: [A-Za-z_] */\h* 1051 \H non-head of word character: [^A-Za-z_] */\H* 1052 \a alphabetic character: [A-Za-z] */\a* 1053 \A non-alphabetic character: [^A-Za-z] */\A* 1054 \l lowercase character: [a-z] */\l* 1055 \L non-lowercase character: [^a-z] */\L* 1056 \u uppercase character: [A-Z] */\u* 1057 \U non-uppercase character: [^A-Z] */\U* 1058 1059 NOTE: Using the atom is faster than the [] form. 1060 1061 NOTE: 'ignorecase', "\c" and "\C" are not used by character classes. 1062 1063 */\_* *E63* */\_i* */\_I* */\_k* */\_K* */\_f* */\_F* 1064 */\_p* */\_P* */\_s* */\_S* */\_d* */\_D* */\_x* */\_X* 1065 */\_o* */\_O* */\_w* */\_W* */\_h* */\_H* */\_a* */\_A* 1066 */\_l* */\_L* */\_u* */\_U* 1067 \_x Where "x" is any of the characters above: The character class with 1068 end-of-line added 1069 (end of character classes) 1070 1071 \e matches <Esc> */\e* 1072 \t matches <Tab> */\t* 1073 \r matches <CR> */\r* 1074 \b matches <BS> */\b* 1075 \n matches an end-of-line */\n* 1076 When matching in a string instead of buffer text a literal newline 1077 character is matched. 1078 1079 ~ matches the last given substitute string */~* */\~* 1080 1081 \(\) A pattern enclosed by escaped parentheses. */\(* */\(\)* */\)* 1082 E.g., "\(^a\)" matches 'a' at the start of a line. 1083 There can only be nine of these. You can use "\%(" to add more, but 1084 not counting it as a sub-expression. 1085 *E51* *E54* *E55* *E872* *E873* 1086 1087 \1 Matches the same string that was matched by */\1* *E65* 1088 the first sub-expression in \( and \). 1089 Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc. 1090 \2 Like "\1", but uses second sub-expression, */\2* 1091 ... */\3* 1092 \9 Like "\1", but uses ninth sub-expression. */\9* 1093 Note: The numbering of groups is done based on which "\(" comes first 1094 in the pattern (going left to right), NOT based on what is matched 1095 first. 1096 1097 \%(\) A pattern enclosed by escaped parentheses. */\%(\)* */\%(* *E53* 1098 Just like \(\), but without counting it as a sub-expression. This 1099 allows using more groups and it's a little bit faster. 1100 1101 x A single character, with no special meaning, matches itself 1102 1103 */\* */\\* 1104 \x A backslash followed by a single character, with no special meaning, 1105 is reserved for future expansions 1106 1107 [] (with 'nomagic': \[]) */[]* */\[]* */\_[]* */collection* *E76* 1108 \_[] 1109 A collection. This is a sequence of characters enclosed in square 1110 brackets. It matches any single character in the collection. 1111 Example matches ~ 1112 [xyz] any 'x', 'y' or 'z' 1113 [a-zA-Z]$ any alphabetic character at the end of a line 1114 \c[a-z]$ same 1115 [А-яЁё] Russian alphabet (with utf-8 and cp1251) 1116 1117 */[\n]* 1118 With "\_" prepended the collection also includes the end-of-line. 1119 The same can be done by including "\n" in the collection. The 1120 end-of-line is also matched when the collection starts with "^"! Thus 1121 "\_[^ab]" matches the end-of-line and any character but "a" and "b". 1122 This makes it Vi compatible: Without the "\_" or "\n" the collection 1123 does not match an end-of-line. 1124 *E769* 1125 When the ']' is not there Vim will not give an error message but 1126 assume no collection is used. Useful to search for '['. However, you 1127 do get E769 for internal searching. And be aware that in a 1128 `:substitute` command the whole command becomes the pattern. E.g. 1129 ":s/[/x/" searches for "[/x" and replaces it with nothing. It does 1130 not search for "[" and replaces it with "x"! 1131 1132 *E944* *E945* 1133 If the sequence begins with "^", it matches any single character NOT 1134 in the collection: "[^xyz]" matches anything but 'x', 'y' and 'z'. 1135 - If two characters in the sequence are separated by '-', this is 1136 shorthand for the full list of ASCII characters between them. E.g., 1137 "[0-9]" matches any decimal digit. If the starting character 1138 exceeds the ending character, e.g. [c-a], E944 occurs. Non-ASCII 1139 characters can be used, but the character values must not be more 1140 than 256 apart in the old regexp engine. For example, searching by 1141 [\u3000-\u4000] after setting re=1 emits a E945 error. Prepending 1142 \%#=2 will fix it. 1143 - A character class expression is evaluated to the set of characters 1144 belonging to that character class. The following character classes 1145 are supported: 1146 Name Func Contents ~ 1147 *[:alnum:]* [:alnum:] isalnum ASCII letters and digits 1148 *[:alpha:]* [:alpha:] isalpha ASCII letters 1149 *[:blank:]* [:blank:] space and tab 1150 *[:cntrl:]* [:cntrl:] iscntrl ASCII control characters 1151 *[:digit:]* [:digit:] decimal digits '0' to '9' 1152 *[:graph:]* [:graph:] isgraph ASCII printable characters excluding 1153 space 1154 *[:lower:]* [:lower:] (1) lowercase letters (all letters when 1155 'ignorecase' is used and the old 1156 engine is in use |two-engines|) 1157 *[:print:]* [:print:] (2) printable characters including space 1158 *[:punct:]* [:punct:] ispunct ASCII punctuation characters 1159 *[:space:]* [:space:] whitespace characters: space, tab, CR, 1160 NL, vertical tab, form feed 1161 *[:upper:]* [:upper:] (3) uppercase letters (all letters when 1162 'ignorecase' is used and the old 1163 engine is in use |two-engines|) 1164 *[:xdigit:]* [:xdigit:] hexadecimal digits: 0-9, a-f, A-F 1165 *[:return:]* [:return:] the <CR> character 1166 *[:tab:]* [:tab:] the <Tab> character 1167 *[:escape:]* [:escape:] the <Esc> character 1168 *[:backspace:]* [:backspace:] the <BS> character 1169 *[:ident:]* [:ident:] identifier character (same as "\i") 1170 *[:keyword:]* [:keyword:] keyword character (same as "\k") 1171 *[:fname:]* [:fname:] file name character (same as "\f") 1172 The square brackets in character class expressions are additional to 1173 the square brackets delimiting a collection. For example, the 1174 following is a plausible pattern for a UNIX filename: 1175 "[-./[:alnum:]_~]\+". That is, a list of at least one character, 1176 each of which is either '-', '.', '/', alphabetic, numeric, '_' or 1177 '~'. 1178 These items only work for 8-bit characters, except [:lower:] and 1179 [:upper:] also work for multibyte characters when using the new 1180 regexp engine. See |two-engines|. In the future these items may 1181 work for multibyte characters. For now, to get all "alpha" 1182 characters you can use: [[:lower:][:upper:]]. 1183 1184 The "Func" column shows what library function is used. The 1185 implementation depends on the system. Otherwise: 1186 (1) Uses islower() for ASCII and Vim builtin rules for other 1187 characters. 1188 (2) Uses Vim builtin rules 1189 (3) As with (1) but using isupper() 1190 */[[=* *[==]* 1191 - An equivalence class. This means that characters are matched that 1192 have almost the same meaning, e.g., when ignoring accents. This 1193 only works for Unicode, latin1 and latin9. The form is: 1194 [=a=] 1195 */[[.* *[..]* 1196 - A collation element. This currently simply accepts a single 1197 character in the form: 1198 [.a.] 1199 */\]* 1200 - To include a literal ']', '^', '-' or '\' in the collection, put a 1201 backslash before it: "[xyz\]]", "[\^xyz]", "[xy\-z]" and "[xyz\\]". 1202 (Note: POSIX does not support the use of a backslash this way). For 1203 ']' you can also make it the first character (following a possible 1204 "^"): "[]xyz]" or "[^]xyz]". 1205 For '-' you can also make it the first or last character: "[-xyz]", 1206 "[^-xyz]" or "[xyz-]". For '\' you can also let it be followed by 1207 any character that's not in "^]-\bdertnoUux". "[\xyz]" matches '\', 1208 'x', 'y' and 'z'. It's better to use "\\" though, future expansions 1209 may use other characters after '\'. 1210 - Omitting the trailing ] is not considered an error. "[]" works like 1211 "[]]", it matches the ']' character. 1212 - The following translations are accepted when the 'l' flag is not 1213 included in 'cpoptions': 1214 \e <Esc> 1215 \t <Tab> 1216 \r <CR> (NOT end-of-line!) 1217 \b <BS> 1218 \n line break, see above |/[\n]| 1219 \d123 decimal number of character 1220 \o40 octal number of character up to 0o377 1221 \x20 hexadecimal number of character up to 0xff 1222 \u20AC hex. number of multibyte character up to 0xffff 1223 \U1234 hex. number of multibyte character up to 8 characters 1224 0xffffffff |E1541| 1225 NOTE: The other backslash codes mentioned above do not work inside 1226 []! 1227 - Matching with a collection can be slow, because each character in 1228 the text has to be compared with each character in the collection. 1229 Use one of the other atoms above when possible. Example: "\d" is 1230 much faster than "[0-9]" and matches the same characters. However, 1231 the new |NFA| regexp engine deals with this better than the old one. 1232 1233 */\%[]* *E69* *E70* *E369* 1234 \%[] A sequence of optionally matched atoms. This always matches. 1235 It matches as much of the list of atoms it contains as possible. Thus 1236 it stops at the first atom that doesn't match. For example: > 1237 /r\%[ead] 1238 < matches "r", "re", "rea" or "read". The longest that matches is used. 1239 To match the Ex command "function", where "fu" is required and 1240 "nction" is optional, this would work: > 1241 /\<fu\%[nction]\> 1242 < The end-of-word atom "\>" is used to avoid matching "fu" in "full". 1243 It gets more complicated when the atoms are not ordinary characters. 1244 You don't often have to use it, but it is possible. Example: > 1245 /\<r\%[[eo]ad]\> 1246 < Matches the words "r", "re", "ro", "rea", "roa", "read" and "road". 1247 There can be no \(\), \%(\) or \z(\) items inside the [] and \%[] does 1248 not nest. 1249 To include a "[" use "[[]" and for "]" use []]", e.g.,: > 1250 /index\%[[[]0[]]] 1251 < matches "index" "index[", "index[0" and "index[0]". 1252 1253 */\%d* */\%x* */\%o* */\%u* */\%U* *E678* 1254 1255 \%d123 Matches the character specified with a decimal number. Must be 1256 followed by a non-digit. 1257 \%o40 Matches the character specified with an octal number up to 0o377. 1258 Numbers below 0o40 must be followed by a non-octal digit or a 1259 non-digit. 1260 \%x2a Matches the character specified with up to two hexadecimal characters. 1261 \%u20AC Matches the character specified with up to four hexadecimal 1262 characters. 1263 \%U1234abcd Matches the character specified with up to eight hexadecimal 1264 characters, up to 0x7fffffff (the maximum allowed value is INT_MAX 1265 |E1541|, but the maximum valid Unicode codepoint is U+10FFFF). 1266 1267 ============================================================================== 1268 7. Ignoring case in a pattern */ignorecase* 1269 1270 If the 'ignorecase' option is on, the case of normal letters is ignored. 1271 'smartcase' can be set to ignore case when the pattern contains lowercase 1272 letters only. 1273 */\c* */\C* 1274 When "\c" appears anywhere in the pattern, the whole pattern is handled like 1275 'ignorecase' is on. The actual value of 'ignorecase' and 'smartcase' is 1276 ignored. "\C" does the opposite: Force matching case for the whole pattern. 1277 Note that 'ignorecase', "\c" and "\C" are not used for the character classes. 1278 1279 Examples: 1280 pattern 'ignorecase' 'smartcase' matches ~ 1281 foo off - foo 1282 foo on - foo Foo FOO 1283 Foo on off foo Foo FOO 1284 Foo on on Foo 1285 \cfoo - - foo Foo FOO 1286 foo\C - - foo 1287 1288 Technical detail: *NL-used-for-Nul* 1289 <Nul> characters in the file are stored as <NL> in memory. In the display 1290 they are shown as "^@". The translation is done when reading and writing 1291 files. To match a <Nul> with a search pattern you can just enter CTRL-@ or 1292 "CTRL-V 000". This is probably just what you expect. Internally the 1293 character is replaced with a <NL> in the search pattern. What is unusual is 1294 that typing CTRL-V CTRL-J also inserts a <NL>, thus also searches for a <Nul> 1295 in the file. 1296 1297 *CR-used-for-NL* 1298 When 'fileformat' is "mac", <NL> characters in the file are stored as <CR> 1299 characters internally. In the text they are shown as "^J". Otherwise this 1300 works similar to the usage of <NL> for a <Nul>. 1301 1302 When working with expression evaluation, a <NL> character in the pattern 1303 matches a <NL> in the string. The use of "\n" (backslash n) to match a <NL> 1304 doesn't work there, it only works to match text in the buffer. 1305 1306 *pattern-multi-byte* *pattern-multibyte* 1307 Patterns will also work with multibyte characters, mostly as you would 1308 expect. But invalid bytes may cause trouble, a pattern with an invalid byte 1309 will probably never match. 1310 1311 ============================================================================== 1312 8. Composing characters *patterns-composing* 1313 1314 */\Z* 1315 When "\Z" appears anywhere in the pattern, all composing characters are 1316 ignored. Thus only the base characters need to match, the composing 1317 characters may be different and the number of composing characters may differ. 1318 Exception: If the pattern starts with one or more composing characters, these 1319 must match. 1320 */\%C* 1321 Use "\%C" to skip any composing characters. For example, the pattern "a" does 1322 not match in "càt" (where the a has the composing character 0x0300), but 1323 "a\%C" does. Note that this does not match "cát" (where the á is character 1324 0xe1, it does not have a compositing character). It does match "cat" (where 1325 the a is just an a). 1326 1327 When a composing character appears at the start of the pattern or after an 1328 item that doesn't include the composing character, a match is found at any 1329 character that includes this composing character. 1330 1331 When using a dot and a composing character, this works the same as the 1332 composing character by itself, except that it doesn't matter what comes before 1333 this. 1334 1335 The order of composing characters does not matter. Also, the text may have 1336 more composing characters than the pattern, it still matches. But all 1337 composing characters in the pattern must be found in the text. 1338 1339 Suppose B is a base character and x and y are composing characters: 1340 pattern text match ~ 1341 Bxy Bxy yes (perfect match) 1342 Bxy Byx yes (order ignored) 1343 Bxy By no (x missing) 1344 Bxy Bx no (y missing) 1345 Bx Bx yes (perfect match) 1346 Bx By no (x missing) 1347 Bx Bxy yes (extra y ignored) 1348 Bx Byx yes (extra y ignored) 1349 1350 ============================================================================== 1351 9. Compare with Perl patterns *perl-patterns* 1352 1353 Vim's regexes are most similar to Perl's, in terms of what you can do. The 1354 difference between them is mostly just notation; here's a summary of where 1355 they differ: 1356 1357 Capability in Vimspeak in Perlspeak ~ 1358 force case insensitivity \c (?i) 1359 force case sensitivity \C (?-i) 1360 backref-less grouping \%(atom\) (?:atom) 1361 conservative quantifiers \{-n,m} `*?,` +?, ??, {}? 1362 0-width match atom\@= (?=atom) 1363 0-width non-match atom\@! (?!atom) 1364 0-width preceding match atom\@<= (?<=atom) 1365 0-width preceding non-match atom\@<! (?<!atom) 1366 match without retry atom\@> (?>atom) 1367 1368 Vim and Perl handle newline characters inside a string a bit differently: 1369 1370 In Perl, ^ and $ only match at the very beginning and end of the text, 1371 by default, but you can set the 'm' flag, which lets them match at 1372 embedded newlines as well. You can also set the 's' flag, which causes 1373 a . to match newlines as well. (Both these flags can be changed inside 1374 a pattern using the same syntax used for the i flag above, BTW.) 1375 1376 On the other hand, Vim's ^ and $ always match at embedded newlines, and 1377 you get two separate atoms, \%^ and \%$, which only match at the very 1378 start and end of the text, respectively. Vim solves the second problem 1379 by giving you the \_ "modifier": put it in front of a . or a character 1380 class, and they will match newlines as well. 1381 1382 Finally, these constructs are unique to Perl: 1383 - execution of arbitrary code in the regex: (?{perl code}) 1384 - conditional expressions: (?(condition)true-expr|false-expr) 1385 1386 ...and these are unique to Vim: 1387 - changing the magic-ness of a pattern: \v \V \m \M 1388 (very useful for avoiding backslashitis) 1389 - sequence of optionally matching atoms: \%[atoms] 1390 - \& (which is to \| what "and" is to "or"; it forces several branches 1391 to match at one spot) 1392 - matching lines/columns by number: \%5l \%5c \%5v 1393 - setting the start and end of the match: \zs \ze 1394 1395 ============================================================================== 1396 10. Highlighting matches *match-highlight* 1397 1398 *syntax-vs-match* 1399 Note that the match highlight mechanism is independent 1400 of |syntax-highlighting|, which is (usually) a buffer-local 1401 highlighting, while matching is window-local, both methods 1402 can be freely mixed. Match highlighting functions give you 1403 a bit more flexibility in when and how to apply, but are 1404 typically only used for temporary highlighting, without strict 1405 rules. Both methods can be used to conceal text. 1406 1407 Thus the matching functions like |matchadd()| won't consider 1408 syntax rules and functions like |synconcealed()| and the 1409 other way around. 1410 1411 *:mat* *:match* 1412 :mat[ch] {group} /{pattern}/ 1413 Define a pattern to highlight in the current window. It will 1414 be highlighted with {group}. Example: > 1415 :highlight MyGroup ctermbg=green guibg=green 1416 :match MyGroup /TODO/ 1417 < Instead of // any character can be used to mark the start and 1418 end of the {pattern}. Watch out for using special characters, 1419 such as '"' and '|'. 1420 1421 {group} must exist at the moment this command is executed. 1422 1423 The {group} highlighting still applies when a character is 1424 to be highlighted for 'hlsearch', as the highlighting for 1425 matches is given higher priority than that of 'hlsearch'. 1426 Syntax highlighting (see 'syntax') is also overruled by 1427 matches. 1428 1429 Note that highlighting the last used search pattern with 1430 'hlsearch' is used in all windows, while the pattern defined 1431 with ":match" only exists in the current window. It is kept 1432 when switching to another buffer. 1433 1434 'ignorecase' does not apply, use |/\c| in the pattern to 1435 ignore case. Otherwise case is not ignored. 1436 1437 'redrawtime' defines the maximum time searched for pattern 1438 matches. 1439 1440 When matching end-of-line and Vim redraws only part of the 1441 display you may get unexpected results. That is because Vim 1442 looks for a match in the line where redrawing starts. 1443 1444 Also see |matcharg()| and |getmatches()|. The former returns 1445 the highlight group and pattern of a previous |:match| 1446 command. The latter returns a list with highlight groups and 1447 patterns defined by both |matchadd()| and |:match|. 1448 1449 Highlighting matches using |:match| are limited to three 1450 matches (aside from |:match|, |:2match| and |:3match| are 1451 available). |matchadd()| does not have this limitation and in 1452 addition makes it possible to prioritize matches. 1453 1454 Another example, which highlights all characters in virtual 1455 column 72 and more: > 1456 :highlight rightMargin term=bold ctermfg=blue guifg=blue 1457 :match rightMargin /.\%>72v/ 1458 < To highlight all character that are in virtual column 7: > 1459 :highlight col8 ctermbg=grey guibg=grey 1460 :match col8 /\%<8v.\%>7v/ 1461 < Note the use of two items to also match a character that 1462 occupies more than one virtual column, such as a TAB. 1463 1464 :mat[ch] 1465 :mat[ch] none 1466 Clear a previously defined match pattern. 1467 1468 1469 :2mat[ch] {group} /{pattern}/ *:2match* 1470 :2mat[ch] 1471 :2mat[ch] none 1472 :3mat[ch] {group} /{pattern}/ *:3match* 1473 :3mat[ch] 1474 :3mat[ch] none 1475 Just like |:match| above, but set a separate match. Thus 1476 there can be three matches active at the same time. The match 1477 with the lowest number has priority if several match at the 1478 same position. It uses the match id 3. 1479 The ":3match" command is used by (older Vims) |matchparen| 1480 plugin. You are suggested to use ":match" for manual matching 1481 and ":2match" for another plugin or even better make use of 1482 the more flexible |matchadd()| (and similar) functions instead. 1483 1484 ============================================================================== 1485 11. Fuzzy matching *fuzzy-matching* 1486 1487 Fuzzy matching scores how well a string matches a pattern when the pattern 1488 characters appear in order but not necessarily contiguously. 1489 1490 Example: > 1491 Pattern: "vim" 1492 Candidates: "vim" -> perfect 1493 "vimeo" -> good (v i m) 1494 "voice mail" -> weaker (v _ i _ _ _ m) 1495 "vintage" -> no match (no "m") 1496 < 1497 If the search string has multiple words, each word is matched separately and 1498 may appear in any order in the candidate. For example "get pat" matches 1499 "GetPattern", "PatternGet", "getPattern", "patGetter", "getSomePattern", 1500 "MatchpatternGet", etc. 1501 1502 The 'ignorecase' and 'smartcase' options do not apply, case is ignored if the 1503 pattern is all lower case. 1504 1505 Vim's implementation is based on the algorithm from the fzy project: 1506 https://github.com/jhawthorn/fzy 1507 1508 It uses dynamic programming to compute an optimal score for a given pattern 1509 and candidate. 1510 1511 The algorithm works in two stages: 1512 1513 1. Forward pass 1514 Scan the candidate left to right, tracking the best score for each 1515 pattern position. Matches score higher when they occur at the start 1516 of the candidate, the start of a word (space, underscore, dash, 1517 camelCase), or directly after the previous match. 1518 1519 2. Backward pass 1520 Start from the best-scoring end position and step back to find match 1521 positions, ensuring the alignment is optimal. 1522 1523 Vim extends the original algorithm to support multibyte codepoints, allowing 1524 correct matching for UTF-8 and other encodings. 1525 1526 Time complexity is O(pattern * candidate). Memory usage is proportional 1527 to the same. 1528 1529 The |matchfuzzy()| and |matchfuzzypos()| functions perform fuzzy searching in 1530 a List of strings. |matchfuzzy()| returns the matching strings, while 1531 |matchfuzzypos()| returns the matches along with their positions and scores. 1532 1533 The "f" flag of `:vimgrep` enables fuzzy matching. 1534 1535 To enable fuzzy matching for |ins-completion|, add "fuzzy" to the 1536 'completeopt' option. For |cmdline-completion|, add "fuzzy" to the 1537 'wildoptions' option. 1538 1539 vim:tw=78:ts=8:noet:ft=help:norl: