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