1*pattern.txt* For Vim version 7.2. Last change: 2008 Nov 18 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 121. Search commands |search-commands| 132. The definition of a pattern |search-pattern| 143. Magic |/magic| 154. Overview of pattern items |pattern-overview| 165. Multi items |pattern-multi-items| 176. Ordinary atoms |pattern-atoms| 187. Ignoring case in a pattern |/ignorecase| 198. Composing characters |patterns-composing| 209. Compare with Perl patterns |perl-patterns| 2110. Highlighting matches |match-highlight| 22 23============================================================================== 241. Search commands *search-commands* *E486* 25 26 */* 27/{pattern}[/]<CR> Search forward for the [count]'th occurrence of 28 {pattern} |exclusive|. 29 30/{pattern}/{offset}<CR> Search forward for the [count]'th occurrence of 31 {pattern} and go |{offset}| lines up or down. 32 |linewise|. 33 34 */<CR>* 35/<CR> Search forward for the [count]'th occurrence of the 36 latest used pattern |last-pattern| with latest used 37 |{offset}|. 38 39//{offset}<CR> Search forward for the [count]'th occurrence of the 40 latest used pattern |last-pattern| with new 41 |{offset}|. If {offset} is empty no offset is used. 42 43 *?* 44?{pattern}[?]<CR> Search backward for the [count]'th previous 45 occurrence of {pattern} |exclusive|. 46 47?{pattern}?{offset}<CR> Search backward for the [count]'th previous 48 occurrence of {pattern} and go |{offset}| lines up or 49 down |linewise|. 50 51 *?<CR>* 52?<CR> Search backward for the [count]'th occurrence of the 53 latest used pattern |last-pattern| with latest used 54 |{offset}|. 55 56??{offset}<CR> Search backward for the [count]'th occurrence of the 57 latest used pattern |last-pattern| with new 58 |{offset}|. If {offset} is empty no offset is used. 59 60 *n* 61n Repeat the latest "/" or "?" [count] times. 62 |last-pattern| {Vi: no count} 63 64 *N* 65N Repeat the latest "/" or "?" [count] times in 66 opposite direction. |last-pattern| {Vi: no count} 67 68 *star* *E348* *E349* 69* Search forward for the [count]'th occurrence of the 70 word nearest to the cursor. The word used for the 71 search is the first of: 72 1. the keyword under the cursor |'iskeyword'| 73 2. the first keyword after the cursor, in the 74 current line 75 3. the non-blank word under the cursor 76 4. the first non-blank word after the cursor, 77 in the current line 78 Only whole keywords are searched for, like with the 79 command "/\<keyword\>". |exclusive| {not in Vi} 80 'ignorecase' is used, 'smartcase' is not. 81 82 *#* 83# Same as "*", but search backward. The pound sign 84 (character 163) also works. If the "#" key works as 85 backspace, try using "stty erase <BS>" before starting 86 Vim (<BS> is CTRL-H or a real backspace). {not in Vi} 87 88 *gstar* 89g* Like "*", but don't put "\<" and "\>" around the word. 90 This makes the search also find matches that are not a 91 whole word. {not in Vi} 92 93 *g#* 94g# Like "#", but don't put "\<" and "\>" around the word. 95 This makes the search also find matches that are not a 96 whole word. {not in Vi} 97 98 *gd* 99gd Goto local Declaration. When the cursor is on a local 100 variable, this command will jump to its declaration. 101 First Vim searches for the start of the current 102 function, just like "[[". If it is not found the 103 search stops in line 1. If it is found, Vim goes back 104 until a blank line is found. From this position Vim 105 searches for the keyword under the cursor, like with 106 "*", but lines that look like a comment are ignored 107 (see 'comments' option). 108 Note that this is not guaranteed to work, Vim does not 109 really check the syntax, it only searches for a match 110 with the keyword. If included files also need to be 111 searched use the commands listed in |include-search|. 112 After this command |n| searches forward for the next 113 match (not backward). 114 {not in Vi} 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. {not in Vi} 122 123 *1gd* 1241gd Like "gd", but ignore matches inside a {} block that 125 ends before the cursor position. {not in Vi} 126 127 *1gD* 1281gD Like "gD", but ignore matches inside a {} block that 129 ends before the cursor position. {not in Vi} 130 131 *CTRL-C* 132CTRL-C Interrupt current (search) command. Use CTRL-Break on 133 MS-DOS |dos-CTRL-Break|. 134 In Normal mode, any pending command is aborted. 135 136 *:noh* *:nohlsearch* 137:noh[lsearch] Stop the highlighting for the 'hlsearch' option. It 138 is automatically turned back on when using a search 139 command, or setting the 'hlsearch' option. 140 This command doesn't work in an autocommand, because 141 the highlighting state is saved and restored when 142 executing autocommands |autocmd-searchpat|. 143 Same thing for when invoking a user function. 144 145While 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 147command with <CR> to actually position the cursor at the displayed match. Or 148use <Esc> to abandon the search. 149 150All matches for the last used search pattern will be highlighted if you set 151the 'hlsearch' option. This can be suspended with the |:nohlsearch| command. 152 153 *search-offset* *{offset}* 154These commands search for the specified pattern. With "/" and "?" an 155additional offset may be given. There are two types of offsets: line offsets 156and character offsets. {the character offsets are not in Vi} 157 158The offset gives the cursor position relative to the found match: 159 [num] [num] lines downwards, in column 1 160 +[num] [num] lines downwards, in column 1 161 -[num] [num] lines upwards, in column 1 162 e[+num] [num] characters to the right of the end of the match 163 e[-num] [num] characters to the left of the end of the match 164 s[+num] [num] characters to the right of the start of the match 165 s[-num] [num] characters to the left of the start of the match 166 b[+num] [num] identical to s[+num] above (mnemonic: begin) 167 b[-num] [num] identical to s[-num] above (mnemonic: begin) 168 ;{pattern} perform another search, see |//;| 169 170If a '-' or '+' is given but [num] is omitted, a count of one will be used. 171When including an offset with 'e', the search becomes inclusive (the 172character the cursor lands on is included in operations). 173 174Examples: 175 176pattern cursor position ~ 177/test/+1 one line below "test", in column 1 178/test/e on the last t of "test" 179/test/s+2 on the 's' of "test" 180/test/b-3 three characters before "test" 181 182If one of these commands is used after an operator, the characters between 183the cursor position before and after the search is affected. However, if a 184line offset is given, the whole lines between the two cursor positions are 185affected. 186 187An example of how to search for matches with a pattern and change the match 188with another word: > 189 /foo<CR> find "foo" 190 c//e change until end of match 191 bar<Esc> type replacement 192 //<CR> go to start of next match 193 c//e change until end of match 194 beep<Esc> type another replacement 195 etc. 196< 197 *//;* *E386* 198A very special offset is ';' followed by another search command. For example: > 199 200 /test 1/;/test 201 /test.*/+1;?ing? 202 203The first one first finds the next occurrence of "test 1", and then the first 204occurrence of "test" after that. 205 206This is like executing two search commands after each other, except that: 207- It can be used as a single motion command after an operator. 208- The direction for a following "n" or "N" command comes from the first 209 search command. 210- When an error occurs the cursor is not moved at all. 211 212 *last-pattern* 213The last used pattern and offset are remembered. They can be used to repeat 214the search, possibly in another direction or with another count. Note that 215two patterns are remembered: One for 'normal' search commands and one for the 216substitute command ":s". Each time an empty pattern is given, the previously 217used pattern is used. 218 219The 'magic' option sticks with the last used pattern. If you change 'magic', 220this will not change how the last used pattern will be interpreted. 221The 'ignorecase' option does not do this. When 'ignorecase' is changed, it 222will result in the pattern to match other text. 223 224All matches for the last used search pattern will be highlighted if you set 225the 'hlsearch' option. 226 227To clear the last used search pattern: > 228 :let @/ = "" 229This will not set the pattern to an empty string, because that would match 230everywhere. The pattern is really cleared, like when starting Vim. 231 232The search usually skips matches that don't move the cursor. Whether the next 233match is found at the next character or after the skipped match depends on the 234'c' flag in 'cpoptions'. See |cpo-c|. 235 with 'c' flag: "/..." advances 1 to 3 characters 236 without 'c' flag: "/..." advances 1 character 237The unpredictability with the 'c' flag is caused by starting the search in the 238first column, skipping matches until one is found past the cursor position. 239 240When searching backwards, searching starts at the start of the line, using the 241'c' flag in 'cpoptions' as described above. Then the last match before the 242cursor position is used. 243 244In Vi the ":tag" command sets the last search pattern when the tag is searched 245for. In Vim this is not done, the previous search pattern is still remembered, 246unless the 't' flag is present in 'cpoptions'. The search pattern is always 247put in the search history. 248 249If the 'wrapscan' option is on (which is the default), searches wrap around 250the end of the buffer. If 'wrapscan' is not set, the backward search stops 251at the beginning and the forward search stops at the end of the buffer. If 252'wrapscan' is set and the pattern was not found the error message "pattern 253not found" is given, and the cursor will not be moved. If 'wrapscan' is not 254set the message becomes "search hit BOTTOM without match" when searching 255forward, or "search hit TOP without match" when searching backward. If 256wrapscan is set and the search wraps around the end of the file the message 257"search hit TOP, continuing at BOTTOM" or "search hit BOTTOM, continuing at 258TOP" is given when searching backwards or forwards respectively. This can be 259switched off by setting the 's' flag in the 'shortmess' option. The highlight 260method 'w' is used for this message (default: standout). 261 262 *search-range* 263You can limit the search command "/" to a certain range of lines by including 264\%>l items. For example, to match the word "limit" below line 199 and above 265line 300: > 266 /\%>199l\%<300llimit 267Also see |/\%>l|. 268 269Another way is to use the ":substitute" command with the 'c' flag. Example: > 270 :.,300s/Pattern//gc 271This command will search from the cursor position until line 300 for 272"Pattern". At the match, you will be asked to type a character. Type 'q' to 273stop at this match, type 'n' to find the next match. 274 275The "*", "#", "g*" and "g#" commands look for a word near the cursor in this 276order, the first one that is found is used: 277- The keyword currently under the cursor. 278- The first keyword to the right of the cursor, in the same line. 279- The WORD currently under the cursor. 280- The first WORD to the right of the cursor, in the same line. 281The keyword may only contain letters and characters in 'iskeyword'. 282The WORD may contain any non-blanks (<Tab>s and/or <Space>s). 283Note that if you type with ten fingers, the characters are easy to remember: 284the "#" is under your left hand middle finger (search to the left and up) and 285the "*" is under your right hand middle finger (search to the right and down). 286(this depends on your keyboard layout though). 287 288============================================================================== 2892. The definition of a pattern *search-pattern* *pattern* *[pattern]* 290 *regular-expression* *regexp* *Pattern* 291 *E76* *E383* *E476* 292 293For starters, read chapter 27 of the user manual |usr_27.txt|. 294 295 */bar* */\bar* */pattern* 2961. A pattern is one or more branches, separated by "\|". It matches anything 297 that matches one of the branches. Example: "foo\|beep" matches "foo" and 298 matches "beep". If more than one branch matches, the first one is used. 299 300 pattern ::= branch 301 or branch \| branch 302 or branch \| branch \| branch 303 etc. 304 305 */branch* */\&* 3062. A branch is one or more concats, separated by "\&". It matches the last 307 concat, but only if all the preceding concats also match at the same 308 position. Examples: 309 "foobeep\&..." matches "foo" in "foobeep". 310 ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" 311 312 branch ::= concat 313 or concat \& concat 314 or concat \& concat \& concat 315 etc. 316 317 */concat* 3183. A concat is one or more pieces, concatenated. It matches a match for the 319 first piece, followed by a match for the second piece, etc. Example: 320 "f[0-9]b", first matches "f", then a digit and then "b". 321 322 concat ::= piece 323 or piece piece 324 or piece piece piece 325 etc. 326 327 */piece* 3284. A piece is an atom, possibly followed by a multi, an indication of how many 329 times the atom can be matched. Example: "a*" matches any sequence of "a" 330 characters: "", "a", "aa", etc. See |/multi|. 331 332 piece ::= atom 333 or atom multi 334 335 */atom* 3365. An atom can be one of a long list of items. Many atoms match one character 337 in the text. It is often an ordinary character or a character class. 338 Braces can be used to make a pattern into an atom. The "\z(\)" construct 339 is only for syntax highlighting. 340 341 atom ::= ordinary-atom |/ordinary-atom| 342 or \( pattern \) |/\(| 343 or \%( pattern \) |/\%(| 344 or \z( pattern \) |/\z(| 345 346 347============================================================================== 3483. Magic */magic* 349 350Some characters in the pattern are taken literally. They match with the same 351character in the text. When preceded with a backslash however, these 352characters get a special meaning. 353 354Other characters have a special meaning without a backslash. They need to be 355preceded with a backslash to match literally. 356 357If a character is taken literally or not depends on the 'magic' option and the 358items mentioned next. 359 */\m* */\M* 360Use of "\m" makes the pattern after it be interpreted as if 'magic' is set, 361ignoring the actual value of the 'magic' option. 362Use of "\M" makes the pattern after it be interpreted as if 'nomagic' is used. 363 */\v* */\V* 364Use of "\v" means that in the pattern after it all ASCII characters except 365'0'-'9', 'a'-'z', 'A'-'Z' and '_' have a special meaning. "very magic" 366 367Use of "\V" means that in the pattern after it only the backslash has a 368special meaning. "very nomagic" 369 370Examples: 371after: \v \m \M \V matches ~ 372 'magic' 'nomagic' 373 $ $ $ \$ matches end-of-line 374 . . \. \. matches any character 375 * * \* \* any number of the previous atom 376 () \(\) \(\) \(\) grouping into an atom 377 | \| \| \| separating alternatives 378 \a \a \a \a alphabetic character 379 \\ \\ \\ \\ literal backslash 380 \. \. . . literal dot 381 \{ { { { literal '{' 382 a a a a literal 'a' 383 384{only Vim supports \m, \M, \v and \V} 385 386It is recommended to always keep the 'magic' option at the default setting, 387which is 'magic'. This avoids portability problems. To make a pattern immune 388to the 'magic' option being set or not, put "\m" or "\M" at the start of the 389pattern. 390 391============================================================================== 3924. Overview of pattern items *pattern-overview* 393 394Overview of multi items. */multi* *E61* *E62* 395More explanation and examples below, follow the links. *E64* 396 397 multi ~ 398 'magic' 'nomagic' matches of the preceding atom ~ 399|/star| * \* 0 or more as many as possible 400|/\+| \+ \+ 1 or more as many as possible (*) 401|/\=| \= \= 0 or 1 as many as possible (*) 402|/\?| \? \? 0 or 1 as many as possible (*) 403 404|/\{| \{n,m} \{n,m} n to m as many as possible (*) 405 \{n} \{n} n exactly (*) 406 \{n,} \{n,} at least n as many as possible (*) 407 \{,m} \{,m} 0 to m as many as possible (*) 408 \{} \{} 0 or more as many as possible (same as *) (*) 409 410|/\{-| \{-n,m} \{-n,m} n to m as few as possible (*) 411 \{-n} \{-n} n exactly (*) 412 \{-n,} \{-n,} at least n as few as possible (*) 413 \{-,m} \{-,m} 0 to m as few as possible (*) 414 \{-} \{-} 0 or more as few as possible (*) 415 416 *E59* 417|/\@>| \@> \@> 1, like matching a whole pattern (*) 418|/\@=| \@= \@= nothing, requires a match |/zero-width| (*) 419|/\@!| \@! \@! nothing, requires NO match |/zero-width| (*) 420|/\@<=| \@<= \@<= nothing, requires a match behind |/zero-width| (*) 421|/\@<!| \@<! \@<! nothing, requires NO match behind |/zero-width| (*) 422 423(*) {not in Vi} 424 425 426Overview of ordinary atoms. */ordinary-atom* 427More explanation and examples below, follow the links. 428 429 ordinary atom ~ 430 magic nomagic matches ~ 431|/^| ^ ^ start-of-line (at start of pattern) |/zero-width| 432|/\^| \^ \^ literal '^' 433|/\_^| \_^ \_^ start-of-line (used anywhere) |/zero-width| 434|/$| $ $ end-of-line (at end of pattern) |/zero-width| 435|/\$| \$ \$ literal '$' 436|/\_$| \_$ \_$ end-of-line (used anywhere) |/zero-width| 437|/.| . \. any single character (not an end-of-line) 438|/\_.| \_. \_. any single character or end-of-line 439|/\<| \< \< beginning of a word |/zero-width| 440|/\>| \> \> end of a word |/zero-width| 441|/\zs| \zs \zs anything, sets start of match 442|/\ze| \ze \ze anything, sets end of match 443|/\%^| \%^ \%^ beginning of file |/zero-width| *E71* 444|/\%$| \%$ \%$ end of file |/zero-width| 445|/\%V| \%V \%V inside Visual area |/zero-width| 446|/\%#| \%# \%# cursor position |/zero-width| 447|/\%'m| \%'m \%'m mark m position |/zero-width| 448|/\%l| \%23l \%23l in line 23 |/zero-width| 449|/\%c| \%23c \%23c in column 23 |/zero-width| 450|/\%v| \%23v \%23v in virtual column 23 |/zero-width| 451 452Character classes {not in Vi}: */character-classes* 453|/\i| \i \i identifier character (see 'isident' option) 454|/\I| \I \I like "\i", but excluding digits 455|/\k| \k \k keyword character (see 'iskeyword' option) 456|/\K| \K \K like "\k", but excluding digits 457|/\f| \f \f file name character (see 'isfname' option) 458|/\F| \F \F like "\f", but excluding digits 459|/\p| \p \p printable character (see 'isprint' option) 460|/\P| \P \P like "\p", but excluding digits 461|/\s| \s \s whitespace character: <Space> and <Tab> 462|/\S| \S \S non-whitespace character; opposite of \s 463|/\d| \d \d digit: [0-9] 464|/\D| \D \D non-digit: [^0-9] 465|/\x| \x \x hex digit: [0-9A-Fa-f] 466|/\X| \X \X non-hex digit: [^0-9A-Fa-f] 467|/\o| \o \o octal digit: [0-7] 468|/\O| \O \O non-octal digit: [^0-7] 469|/\w| \w \w word character: [0-9A-Za-z_] 470|/\W| \W \W non-word character: [^0-9A-Za-z_] 471|/\h| \h \h head of word character: [A-Za-z_] 472|/\H| \H \H non-head of word character: [^A-Za-z_] 473|/\a| \a \a alphabetic character: [A-Za-z] 474|/\A| \A \A non-alphabetic character: [^A-Za-z] 475|/\l| \l \l lowercase character: [a-z] 476|/\L| \L \L non-lowercase character: [^a-z] 477|/\u| \u \u uppercase character: [A-Z] 478|/\U| \U \U non-uppercase character [^A-Z] 479|/\_| \_x \_x where x is any of the characters above: character 480 class with end-of-line included 481(end of character classes) 482 483|/\e| \e \e <Esc> 484|/\t| \t \t <Tab> 485|/\r| \r \r <CR> 486|/\b| \b \b <BS> 487|/\n| \n \n end-of-line 488|/~| ~ \~ last given substitute string 489|/\1| \1 \1 same string as matched by first \(\) {not in Vi} 490|/\2| \2 \2 Like "\1", but uses second \(\) 491 ... 492|/\9| \9 \9 Like "\1", but uses ninth \(\) 493 *E68* 494|/\z1| \z1 \z1 only for syntax highlighting, see |:syn-ext-match| 495 ... 496|/\z1| \z9 \z9 only for syntax highlighting, see |:syn-ext-match| 497 498 x x a character with no special meaning matches itself 499 500|/[]| [] \[] any character specified inside the [] 501|/\%[]| \%[] \%[] a sequence of optionally matched atoms 502 503|/\c| \c \c ignore case, do not use the 'ignorecase' option 504|/\C| \C \C match case, do not use the 'ignorecase' option 505|/\m| \m \m 'magic' on for the following chars in the pattern 506|/\M| \M \M 'magic' off for the following chars in the pattern 507|/\v| \v \v the following chars in the pattern are "very magic" 508|/\V| \V \V the following chars in the pattern are "very nomagic" 509|/\Z| \Z \Z ignore differences in Unicode "combining characters". 510 Useful when searching voweled Hebrew or Arabic text. 511 512|/\%d| \%d \%d match specified decimal character (eg \%d123) 513|/\%x| \%x \%x match specified hex character (eg \%x2a) 514|/\%o| \%o \%o match specified octal character (eg \%o040) 515|/\%u| \%u \%u match specified multibyte character (eg \%u20ac) 516|/\%U| \%U \%U match specified large multibyte character (eg 517 \%U12345678) 518 519Example matches ~ 520\<\I\i* or 521\<\h\w* 522\<[a-zA-Z_][a-zA-Z0-9_]* 523 An identifier (e.g., in a C program). 524 525\(\.$\|\. \) A period followed by <EOL> or a space. 526 527[.!?][])"']*\($\|[ ]\) A search pattern that finds the end of a sentence, 528 with almost the same definition as the ")" command. 529 530cat\Z Both "cat" and "càt" ("a" followed by 0x0300) 531 Does not match "càt" (character 0x00e0), even 532 though it may look the same. 533 534 535============================================================================== 5365. Multi items *pattern-multi-items* 537 538An atom can be followed by an indication of how many times the atom can be 539matched and in what way. This is called a multi. See |/multi| for an 540overview. 541 542 */star* */\star* *E56* 543* (use \* when 'magic' is not set) 544 Matches 0 or more of the preceding atom, as many as possible. 545 Example 'nomagic' matches ~ 546 a* a\* "", "a", "aa", "aaa", etc. 547 .* \.\* anything, also an empty string, no end-of-line 548 \_.* \_.\* everything up to the end of the buffer 549 \_.*END \_.\*END everything up to and including the last "END" 550 in the buffer 551 552 Exception: When "*" is used at the start of the pattern or just after 553 "^" it matches the star character. 554 555 Be aware that repeating "\_." can match a lot of text and take a long 556 time. For example, "\_.*END" matches all text from the current 557 position to the last occurrence of "END" in the file. Since the "*" 558 will match as many as possible, this first skips over all lines until 559 the end of the file and then tries matching "END", backing up one 560 character at a time. 561 562 */\+* *E57* 563\+ Matches 1 or more of the preceding atom, as many as possible. {not in 564 Vi} 565 Example matches ~ 566 ^.\+$ any non-empty line 567 \s\+ white space of at least one character 568 569 */\=* 570\= Matches 0 or 1 of the preceding atom, as many as possible. {not in Vi} 571 Example matches ~ 572 foo\= "fo" and "foo" 573 574 */\?* 575\? Just like \=. Cannot be used when searching backwards with the "?" 576 command. {not in Vi} 577 578 */\{* *E58* *E60* *E554* 579\{n,m} Matches n to m of the preceding atom, as many as possible 580\{n} Matches n of the preceding atom 581\{n,} Matches at least n of the preceding atom, as many as possible 582\{,m} Matches 0 to m of the preceding atom, as many as possible 583\{} Matches 0 or more of the preceding atom, as many as possible (like *) 584 */\{-* 585\{-n,m} matches n to m of the preceding atom, as few as possible 586\{-n} matches n of the preceding atom 587\{-n,} matches at least n of the preceding atom, as few as possible 588\{-,m} matches 0 to m of the preceding atom, as few as possible 589\{-} matches 0 or more of the preceding atom, as few as possible 590 {Vi does not have any of these} 591 592 n and m are positive decimal numbers or zero 593 *non-greedy* 594 If a "-" appears immediately after the "{", then a shortest match 595 first algorithm is used (see example below). In particular, "\{-}" is 596 the same as "*" but uses the shortest match first algorithm. BUT: A 597 match that starts earlier is preferred over a shorter match: "a\{-}b" 598 matches "aaab" in "xaaab". 599 600 Example matches ~ 601 ab\{2,3}c "abbc" or "abbbc" 602 a\{5} "aaaaa" 603 ab\{2,}c "abbc", "abbbc", "abbbbc", etc. 604 ab\{,3}c "ac", "abc", "abbc" or "abbbc" 605 a[bc]\{3}d "abbbd", "abbcd", "acbcd", "acccd", etc. 606 a\(bc\)\{1,2}d "abcd" or "abcbcd" 607 a[bc]\{-}[cd] "abc" in "abcd" 608 a[bc]*[cd] "abcd" in "abcd" 609 610 The } may optionally be preceded with a backslash: \{n,m\}. 611 612 */\@=* 613\@= Matches the preceding atom with zero width. {not in Vi} 614 Like "(?=pattern)" in Perl. 615 Example matches ~ 616 foo\(bar\)\@= "foo" in "foobar" 617 foo\(bar\)\@=foo nothing 618 */zero-width* 619 When using "\@=" (or "^", "$", "\<", "\>") no characters are included 620 in the match. These items are only used to check if a match can be 621 made. This can be tricky, because a match with following items will 622 be done in the same position. The last example above will not match 623 "foobarfoo", because it tries match "foo" in the same position where 624 "bar" matched. 625 626 Note that using "\&" works the same as using "\@=": "foo\&.." is the 627 same as "\(foo\)\@=..". But using "\&" is easier, you don't need the 628 braces. 629 630 631 */\@!* 632\@! Matches with zero width if the preceding atom does NOT match at the 633 current position. |/zero-width| {not in Vi} 634 Like '(?!pattern)" in Perl. 635 Example matches ~ 636 foo\(bar\)\@! any "foo" not followed by "bar" 637 a.\{-}p\@! "a", "ap", "app", etc. not followed by a "p" 638 if \(\(then\)\@!.\)*$ "if " not followed by "then" 639 640 Using "\@!" is tricky, because there are many places where a pattern 641 does not match. "a.*p\@!" will match from an "a" to the end of the 642 line, because ".*" can match all characters in the line and the "p" 643 doesn't match at the end of the line. "a.\{-}p\@!" will match any 644 "a", "ap", "aap", etc. that isn't followed by a "p", because the "." 645 can match a "p" and "p\@!" doesn't match after that. 646 647 You can't use "\@!" to look for a non-match before the matching 648 position: "\(foo\)\@!bar" will match "bar" in "foobar", because at the 649 position where "bar" matches, "foo" does not match. To avoid matching 650 "foobar" you could use "\(foo\)\@!...bar", but that doesn't match a 651 bar at the start of a line. Use "\(foo\)\@<!bar". 652 653 */\@<=* 654\@<= Matches with zero width if the preceding atom matches just before what 655 follows. |/zero-width| {not in Vi} 656 Like '(?<=pattern)" in Perl, but Vim allows non-fixed-width patterns. 657 Example matches ~ 658 \(an\_s\+\)\@<=file "file" after "an" and white space or an 659 end-of-line 660 For speed it's often much better to avoid this multi. Try using "\zs" 661 instead |/\zs|. To match the same as the above example: 662 an\_s\+\zsfile 663 664 "\@<=" and "\@<!" check for matches just before what follows. 665 Theoretically these matches could start anywhere before this position. 666 But to limit the time needed, only the line where what follows matches 667 is searched, and one line before that (if there is one). This should 668 be sufficient to match most things and not be too slow. 669 The part of the pattern after "\@<=" and "\@<!" are checked for a 670 match first, thus things like "\1" don't work to reference \(\) inside 671 the preceding atom. It does work the other way around: 672 Example matches ~ 673 \1\@<=,\([a-z]\+\) ",abc" in "abc,abc" 674 675 */\@<!* 676\@<! Matches with zero width if the preceding atom does NOT match just 677 before what follows. Thus this matches if there is no position in the 678 current or previous line where the atom matches such that it ends just 679 before what follows. |/zero-width| {not in Vi} 680 Like '(?<!pattern)" in Perl, but Vim allows non-fixed-width patterns. 681 The match with the preceding atom is made to end just before the match 682 with what follows, thus an atom that ends in ".*" will work. 683 Warning: This can be slow (because many positions need to be checked 684 for a match). 685 Example matches ~ 686 \(foo\)\@<!bar any "bar" that's not in "foobar" 687 \(\/\/.*\)\@<!in "in" which is not after "//" 688 689 */\@>* 690\@> Matches the preceding atom like matching a whole pattern. {not in Vi} 691 Like "(?>pattern)" in Perl. 692 Example matches ~ 693 \(a*\)\@>a nothing (the "a*" takes all the "a"'s, there can't be 694 another one following) 695 696 This matches the preceding atom as if it was a pattern by itself. If 697 it doesn't match, there is no retry with shorter sub-matches or 698 anything. Observe this difference: "a*b" and "a*ab" both match 699 "aaab", but in the second case the "a*" matches only the first two 700 "a"s. "\(a*\)\@>ab" will not match "aaab", because the "a*" matches 701 the "aaa" (as many "a"s as possible), thus the "ab" can't match. 702 703 704============================================================================== 7056. Ordinary atoms *pattern-atoms* 706 707An ordinary atom can be: 708 709 */^* 710^ At beginning of pattern or after "\|", "\(", "\%(" or "\n": matches 711 start-of-line; at other positions, matches literal '^'. |/zero-width| 712 Example matches ~ 713 ^beep( the start of the C function "beep" (probably). 714 715 */\^* 716\^ Matches literal '^'. Can be used at any position in the pattern. 717 718 */\_^* 719\_^ Matches start-of-line. |/zero-width| Can be used at any position in 720 the pattern. 721 Example matches ~ 722 \_s*\_^foo white space and blank lines and then "foo" at 723 start-of-line 724 725 */$* 726$ At end of pattern or in front of "\|", "\)" or "\n" ('magic' on): 727 matches end-of-line <EOL>; at other positions, matches literal '$'. 728 |/zero-width| 729 730 */\$* 731\$ Matches literal '$'. Can be used at any position in the pattern. 732 733 */\_$* 734\_$ Matches end-of-line. |/zero-width| Can be used at any position in the 735 pattern. Note that "a\_$b" never matches, since "b" cannot match an 736 end-of-line. Use "a\nb" instead |/\n|. 737 Example matches ~ 738 foo\_$\_s* "foo" at end-of-line and following white space and 739 blank lines 740 741. (with 'nomagic': \.) */.* */\.* 742 Matches any single character, but not an end-of-line. 743 744 */\_.* 745\_. Matches any single character or end-of-line. 746 Careful: "\_.*" matches all text to the end of the buffer! 747 748 */\<* 749\< Matches the beginning of a word: The next char is the first char of a 750 word. The 'iskeyword' option specifies what is a word character. 751 |/zero-width| 752 753 */\>* 754\> Matches the end of a word: The previous char is the last char of a 755 word. The 'iskeyword' option specifies what is a word character. 756 |/zero-width| 757 758 */\zs* 759\zs Matches at any position, and sets the start of the match there: The 760 next char is the first char of the whole match. |/zero-width| 761 Example: > 762 /^\s*\zsif 763< matches an "if" at the start of a line, ignoring white space. 764 Can be used multiple times, the last one encountered in a matching 765 branch is used. Example: > 766 /\(.\{-}\zsFab\)\{3} 767< Finds the third occurrence of "Fab". 768 {not in Vi} {not available when compiled without the +syntax feature} 769 */\ze* 770\ze Matches at any position, and sets the end of the match there: The 771 previous char is the last char of the whole match. |/zero-width| 772 Can be used multiple times, the last one encountered in a matching 773 branch is used. 774 Example: "end\ze\(if\|for\)" matches the "end" in "endif" and 775 "endfor". 776 {not in Vi} {not available when compiled without the +syntax feature} 777 778 */\%^* *start-of-file* 779\%^ Matches start of the file. When matching with a string, matches the 780 start of the string. {not in Vi} 781 For example, to find the first "VIM" in a file: > 782 /\%^\_.\{-}\zsVIM 783< 784 */\%$* *end-of-file* 785\%$ Matches end of the file. When matching with a string, matches the 786 end of the string. {not in Vi} 787 Note that this does NOT find the last "VIM" in a file: > 788 /VIM\_.\{-}\%$ 789< It will find the next VIM, because the part after it will always 790 match. This one will find the last "VIM" in the file: > 791 /VIM\ze\(\(VIM\)\@!\_.\)*\%$ 792< This uses |/\@!| to ascertain that "VIM" does NOT match in any 793 position after the first "VIM". 794 Searching from the end of the file backwards is easier! 795 796 */\%V* 797\%V Match inside the Visual area. When Visual mode has already been 798 stopped match in the area that |gv| would reselect. 799 This is a |/zero-width| match. To make sure the whole pattern is 800 inside the Visual area put it at the start and end of the pattern, 801 e.g.: > 802 /\%Vfoo.*bar\%V 803< Only works for the current buffer. 804 805 */\%#* *cursor-position* 806\%# Matches with the cursor position. Only works when matching in a 807 buffer displayed in a window. {not in Vi} 808 WARNING: When the cursor is moved after the pattern was used, the 809 result becomes invalid. Vim doesn't automatically update the matches. 810 This is especially relevant for syntax highlighting and 'hlsearch'. 811 In other words: When the cursor moves the display isn't updated for 812 this change. An update is done for lines which are changed (the whole 813 line is updated) or when using the |CTRL-L| command (the whole screen 814 is updated). Example, to highlight the word under the cursor: > 815 /\k*\%#\k* 816< When 'hlsearch' is set and you move the cursor around and make changes 817 this will clearly show when the match is updated or not. 818 819 */\%'m* */\%<'m* */\%>'m* 820\%'m Matches with the position of mark m. 821\%<'m Matches before the position of mark m. 822\%>'m Matches after the position of mark m. 823 Example, to highlight the text from mark 's to 'e: > 824 /.\%>'s.*\%<'e.. 825< Note that two dots are required to include mark 'e in the match. That 826 is because "\%<'e" matches at the character before the 'e mark, and 827 since it's a |/zero-width| match it doesn't include that character. 828 {not in Vi} 829 WARNING: When the mark is moved after the pattern was used, the result 830 becomes invalid. Vim doesn't automatically update the matches. 831 Similar to moving the cursor for "\%#" |/\%#|. 832 833 */\%l* */\%>l* */\%<l* 834\%23l Matches in a specific line. 835\%<23l Matches above a specific line (lower line number). 836\%>23l Matches below a specific line (higher line number). 837 These three can be used to match specific lines in a buffer. The "23" 838 can be any line number. The first line is 1. {not in Vi} 839 WARNING: When inserting or deleting lines Vim does not automatically 840 update the matches. This means Syntax highlighting quickly becomes 841 wrong. 842 Example, to highlight the line where the cursor currently is: > 843 :exe '/\%' . line(".") . 'l.*' 844< When 'hlsearch' is set and you move the cursor around and make changes 845 this will clearly show when the match is updated or not. 846 847 */\%c* */\%>c* */\%<c* 848\%23c Matches in a specific column. 849\%<23c Matches before a specific column. 850\%>23c Matches after a specific column. 851 These three can be used to match specific columns in a buffer or 852 string. The "23" can be any column number. The first column is 1. 853 Actually, the column is the byte number (thus it's not exactly right 854 for multi-byte characters). {not in Vi} 855 WARNING: When inserting or deleting text Vim does not automatically 856 update the matches. This means Syntax highlighting quickly becomes 857 wrong. 858 Example, to highlight the column where the cursor currently is: > 859 :exe '/\%' . col(".") . 'c' 860< When 'hlsearch' is set and you move the cursor around and make changes 861 this will clearly show when the match is updated or not. 862 Example for matching a single byte in column 44: > 863 /\%>43c.\%<46c 864< Note that "\%<46c" matches in column 45 when the "." matches a byte in 865 column 44. 866 */\%v* */\%>v* */\%<v* 867\%23v Matches in a specific virtual column. 868\%<23v Matches before a specific virtual column. 869\%>23v Matches after a specific virtual column. 870 These three can be used to match specific virtual columns in a buffer 871 or string. When not matching with a buffer in a window, the option 872 values of the current window are used (e.g., 'tabstop'). 873 The "23" can be any column number. The first column is 1. 874 Note that some virtual column positions will never match, because they 875 are halfway through a tab or other character that occupies more than 876 one screen character. {not in Vi} 877 WARNING: When inserting or deleting text Vim does not automatically 878 update highlighted matches. This means Syntax highlighting quickly 879 becomes wrong. 880 Example, to highlight all the characters after virtual column 72: > 881 /\%>72v.* 882< When 'hlsearch' is set and you move the cursor around and make changes 883 this will clearly show when the match is updated or not. 884 To match the text up to column 17: > 885 /.*\%17v 886< Column 17 is included, because that's where the "\%17v" matches, 887 even though this is a |/zero-width| match. Adding a dot to match the 888 next character has the same result: > 889 /.*\%17v. 890< This command does the same thing, but also matches when there is no 891 character in column 17: > 892 /.*\%<18v. 893< 894 895Character classes: {not in Vi} 896\i identifier character (see 'isident' option) */\i* 897\I like "\i", but excluding digits */\I* 898\k keyword character (see 'iskeyword' option) */\k* 899\K like "\k", but excluding digits */\K* 900\f file name character (see 'isfname' option) */\f* 901\F like "\f", but excluding digits */\F* 902\p printable character (see 'isprint' option) */\p* 903\P like "\p", but excluding digits */\P* 904 905NOTE: the above also work for multi-byte characters. The ones below only 906match ASCII characters, as indicated by the range. 907 908 *whitespace* *white-space* 909\s whitespace character: <Space> and <Tab> */\s* 910\S non-whitespace character; opposite of \s */\S* 911\d digit: [0-9] */\d* 912\D non-digit: [^0-9] */\D* 913\x hex digit: [0-9A-Fa-f] */\x* 914\X non-hex digit: [^0-9A-Fa-f] */\X* 915\o octal digit: [0-7] */\o* 916\O non-octal digit: [^0-7] */\O* 917\w word character: [0-9A-Za-z_] */\w* 918\W non-word character: [^0-9A-Za-z_] */\W* 919\h head of word character: [A-Za-z_] */\h* 920\H non-head of word character: [^A-Za-z_] */\H* 921\a alphabetic character: [A-Za-z] */\a* 922\A non-alphabetic character: [^A-Za-z] */\A* 923\l lowercase character: [a-z] */\l* 924\L non-lowercase character: [^a-z] */\L* 925\u uppercase character: [A-Z] */\u* 926\U non-uppercase character [^A-Z] */\U* 927 928 NOTE: Using the atom is faster than the [] form. 929 930 NOTE: 'ignorecase', "\c" and "\C" are not used by character classes. 931 932 */\_* *E63* */\_i* */\_I* */\_k* */\_K* */\_f* */\_F* 933 */\_p* */\_P* */\_s* */\_S* */\_d* */\_D* */\_x* */\_X* 934 */\_o* */\_O* */\_w* */\_W* */\_h* */\_H* */\_a* */\_A* 935 */\_l* */\_L* */\_u* */\_U* 936\_x Where "x" is any of the characters above: The character class with 937 end-of-line added 938(end of character classes) 939 940\e matches <Esc> */\e* 941\t matches <Tab> */\t* 942\r matches <CR> */\r* 943\b matches <BS> */\b* 944\n matches an end-of-line */\n* 945 When matching in a string instead of buffer text a literal newline 946 character is matched. 947 948~ matches the last given substitute string */~* */\~* 949 950\(\) A pattern enclosed by escaped parentheses. */\(* */\(\)* */\)* 951 E.g., "\(^a\)" matches 'a' at the start of a line. *E51* *E54* *E55* 952 953\1 Matches the same string that was matched by */\1* *E65* 954 the first sub-expression in \( and \). {not in Vi} 955 Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc. 956\2 Like "\1", but uses second sub-expression, */\2* 957 ... */\3* 958\9 Like "\1", but uses ninth sub-expression. */\9* 959 Note: The numbering of groups is done based on which "\(" comes first 960 in the pattern (going left to right), NOT based on what is matched 961 first. 962 963\%(\) A pattern enclosed by escaped parentheses. */\%(\)* */\%(* *E53* 964 Just like \(\), but without counting it as a sub-expression. This 965 allows using more groups and it's a little bit faster. 966 {not in Vi} 967 968x A single character, with no special meaning, matches itself 969 970 */\* */\\* 971\x A backslash followed by a single character, with no special meaning, 972 is reserved for future expansions 973 974[] (with 'nomagic': \[]) */[]* */\[]* */\_[]* */collection* 975\_[] 976 A collection. This is a sequence of characters enclosed in brackets. 977 It matches any single character in the collection. 978 Example matches ~ 979 [xyz] any 'x', 'y' or 'z' 980 [a-zA-Z]$ any alphabetic character at the end of a line 981 \c[a-z]$ same 982 */[\n]* 983 With "\_" prepended the collection also includes the end-of-line. 984 The same can be done by including "\n" in the collection. The 985 end-of-line is also matched when the collection starts with "^"! Thus 986 "\_[^ab]" matches the end-of-line and any character but "a" and "b". 987 This makes it Vi compatible: Without the "\_" or "\n" the collection 988 does not match an end-of-line. 989 *E769* 990 When the ']' is not there Vim will not give an error message but 991 assume no collection is used. Useful to search for '['. However, you 992 do get E769 for internal searching. 993 994 If the sequence begins with "^", it matches any single character NOT 995 in the collection: "[^xyz]" matches anything but 'x', 'y' and 'z'. 996 - If two characters in the sequence are separated by '-', this is 997 shorthand for the full list of ASCII characters between them. E.g., 998 "[0-9]" matches any decimal digit. 999 - A character class expression is evaluated to the set of characters 1000 belonging to that character class. The following character classes 1001 are supported: 1002 Name Contents ~ 1003*[:alnum:]* [:alnum:] letters and digits 1004*[:alpha:]* [:alpha:] letters 1005*[:blank:]* [:blank:] space and tab characters 1006*[:cntrl:]* [:cntrl:] control characters 1007*[:digit:]* [:digit:] decimal digits 1008*[:graph:]* [:graph:] printable characters excluding space 1009*[:lower:]* [:lower:] lowercase letters (all letters when 1010 'ignorecase' is used) 1011*[:print:]* [:print:] printable characters including space 1012*[:punct:]* [:punct:] punctuation characters 1013*[:space:]* [:space:] whitespace characters 1014*[:upper:]* [:upper:] uppercase letters (all letters when 1015 'ignorecase' is used) 1016*[:xdigit:]* [:xdigit:] hexadecimal digits 1017*[:return:]* [:return:] the <CR> character 1018*[:tab:]* [:tab:] the <Tab> character 1019*[:escape:]* [:escape:] the <Esc> character 1020*[:backspace:]* [:backspace:] the <BS> character 1021 The brackets in character class expressions are additional to the 1022 brackets delimiting a collection. For example, the following is a 1023 plausible pattern for a UNIX filename: "[-./[:alnum:]_~]\+" That is, 1024 a list of at least one character, each of which is either '-', '.', 1025 '/', alphabetic, numeric, '_' or '~'. 1026 These items only work for 8-bit characters. 1027 */[[=* *[==]* 1028 - An equivalence class. This means that characters are matched that 1029 have almost the same meaning, e.g., when ignoring accents. The form 1030 is: 1031 [=a=] 1032 Currently this is only implemented for latin1. Also works for the 1033 latin1 characters in utf-8 and latin9. 1034 */[[.* *[..]* 1035 - A collation element. This currently simply accepts a single 1036 character in the form: 1037 [.a.] 1038 */\]* 1039 - To include a literal ']', '^', '-' or '\' in the collection, put a 1040 backslash before it: "[xyz\]]", "[\^xyz]", "[xy\-z]" and "[xyz\\]". 1041 (Note: POSIX does not support the use of a backslash this way). For 1042 ']' you can also make it the first character (following a possible 1043 "^"): "[]xyz]" or "[^]xyz]" {not in Vi}. 1044 For '-' you can also make it the first or last character: "[-xyz]", 1045 "[^-xyz]" or "[xyz-]". For '\' you can also let it be followed by 1046 any character that's not in "^]-\bertn". "[\xyz]" matches '\', 'x', 1047 'y' and 'z'. It's better to use "\\" though, future expansions may 1048 use other characters after '\'. 1049 - The following translations are accepted when the 'l' flag is not 1050 included in 'cpoptions' {not in Vi}: 1051 \e <Esc> 1052 \t <Tab> 1053 \r <CR> (NOT end-of-line!) 1054 \b <BS> 1055 \n line break, see above |/[\n]| 1056 \d123 decimal number of character 1057 \o40 octal number of character up to 0377 1058 \x20 hexadecimal number of character up to 0xff 1059 \u20AC hex. number of multibyte character up to 0xffff 1060 \U1234 hex. number of multibyte character up to 0xffffffff 1061 NOTE: The other backslash codes mentioned above do not work inside 1062 []! 1063 - Matching with a collection can be slow, because each character in 1064 the text has to be compared with each character in the collection. 1065 Use one of the other atoms above when possible. Example: "\d" is 1066 much faster than "[0-9]" and matches the same characters. 1067 1068 */\%[]* *E69* *E70* *E369* 1069\%[] A sequence of optionally matched atoms. This always matches. 1070 It matches as much of the list of atoms it contains as possible. Thus 1071 it stops at the first atom that doesn't match. For example: > 1072 /r\%[ead] 1073< matches "r", "re", "rea" or "read". The longest that matches is used. 1074 To match the Ex command "function", where "fu" is required and 1075 "nction" is optional, this would work: > 1076 /\<fu\%[nction]\> 1077< The end-of-word atom "\>" is used to avoid matching "fu" in "full". 1078 It gets more complicated when the atoms are not ordinary characters. 1079 You don't often have to use it, but it is possible. Example: > 1080 /\<r\%[[eo]ad]\> 1081< Matches the words "r", "re", "ro", "rea", "roa", "read" and "road". 1082 There can be no \(\), \%(\) or \z(\) items inside the [] and \%[] does 1083 not nest. 1084 To include a "[" use "[[]" and for "]" use []]", e.g.,: > 1085 /index\%[[[]0[]]] 1086< matches "index" "index[", "index[0" and "index[0]". 1087 {not available when compiled without the +syntax feature} 1088 1089 */\%d* */\%x* */\%o* */\%u* */\%U* *E678* 1090 1091\%d123 Matches the character specified with a decimal number. Must be 1092 followed by a non-digit. 1093\%o40 Matches the character specified with an octal number up to 0377. 1094 Numbers below 040 must be followed by a non-octal digit or a non-digit. 1095\%x2a Matches the character specified with up to two hexadecimal characters. 1096\%u20AC Matches the character specified with up to four hexadecimal 1097 characters. 1098\%U1234abcd Matches the character specified with up to eight hexadecimal 1099 characters. 1100 1101============================================================================== 11027. Ignoring case in a pattern */ignorecase* 1103 1104If the 'ignorecase' option is on, the case of normal letters is ignored. 1105'smartcase' can be set to ignore case when the pattern contains lowercase 1106letters only. 1107 */\c* */\C* 1108When "\c" appears anywhere in the pattern, the whole pattern is handled like 1109'ignorecase' is on. The actual value of 'ignorecase' and 'smartcase' is 1110ignored. "\C" does the opposite: Force matching case for the whole pattern. 1111{only Vim supports \c and \C} 1112Note that 'ignorecase', "\c" and "\C" are not used for the character classes. 1113 1114Examples: 1115 pattern 'ignorecase' 'smartcase' matches ~ 1116 foo off - foo 1117 foo on - foo Foo FOO 1118 Foo on off foo Foo FOO 1119 Foo on on Foo 1120 \cfoo - - foo Foo FOO 1121 foo\C - - foo 1122 1123Technical detail: *NL-used-for-Nul* 1124<Nul> characters in the file are stored as <NL> in memory. In the display 1125they are shown as "^@". The translation is done when reading and writing 1126files. To match a <Nul> with a search pattern you can just enter CTRL-@ or 1127"CTRL-V 000". This is probably just what you expect. Internally the 1128character is replaced with a <NL> in the search pattern. What is unusual is 1129that typing CTRL-V CTRL-J also inserts a <NL>, thus also searches for a <Nul> 1130in the file. {Vi cannot handle <Nul> characters in the file at all} 1131 1132 *CR-used-for-NL* 1133When 'fileformat' is "mac", <NL> characters in the file are stored as <CR> 1134characters internally. In the text they are shown as "^J". Otherwise this 1135works similar to the usage of <NL> for a <Nul>. 1136 1137When working with expression evaluation, a <NL> character in the pattern 1138matches a <NL> in the string. The use of "\n" (backslash n) to match a <NL> 1139doesn't work there, it only works to match text in the buffer. 1140 1141 *pattern-multi-byte* 1142Patterns will also work with multi-byte characters, mostly as you would 1143expect. But invalid bytes may cause trouble, a pattern with an invalid byte 1144will probably never match. 1145 1146============================================================================== 11478. Composing characters *patterns-composing* 1148 1149 */\Z* 1150When "\Z" appears anywhere in the pattern, composing characters are ignored. 1151Thus only the base characters need to match, the composing characters may be 1152different and the number of composing characters may differ. Only relevant 1153when 'encoding' is "utf-8". 1154 1155When a composing character appears at the start of the pattern of after an 1156item that doesn't include the composing character, a match is found at any 1157character that includes this composing character. 1158 1159When using a dot and a composing character, this works the same as the 1160composing character by itself, except that it doesn't matter what comes before 1161this. 1162 1163The order of composing characters matters, even though changing the order 1164doesn't change what a character looks like. This may change in the future. 1165 1166============================================================================== 11679. Compare with Perl patterns *perl-patterns* 1168 1169Vim's regexes are most similar to Perl's, in terms of what you can do. The 1170difference between them is mostly just notation; here's a summary of where 1171they differ: 1172 1173Capability in Vimspeak in Perlspeak ~ 1174---------------------------------------------------------------- 1175force case insensitivity \c (?i) 1176force case sensitivity \C (?-i) 1177backref-less grouping \%(atom\) (?:atom) 1178conservative quantifiers \{-n,m} *?, +?, ??, {}? 11790-width match atom\@= (?=atom) 11800-width non-match atom\@! (?!atom) 11810-width preceding match atom\@<= (?<=atom) 11820-width preceding non-match atom\@<! (?<!atom) 1183match without retry atom\@> (?>atom) 1184 1185Vim and Perl handle newline characters inside a string a bit differently: 1186 1187In Perl, ^ and $ only match at the very beginning and end of the text, 1188by default, but you can set the 'm' flag, which lets them match at 1189embedded newlines as well. You can also set the 's' flag, which causes 1190a . to match newlines as well. (Both these flags can be changed inside 1191a pattern using the same syntax used for the i flag above, BTW.) 1192 1193On the other hand, Vim's ^ and $ always match at embedded newlines, and 1194you get two separate atoms, \%^ and \%$, which only match at the very 1195start and end of the text, respectively. Vim solves the second problem 1196by giving you the \_ "modifier": put it in front of a . or a character 1197class, and they will match newlines as well. 1198 1199Finally, these constructs are unique to Perl: 1200- execution of arbitrary code in the regex: (?{perl code}) 1201- conditional expressions: (?(condition)true-expr|false-expr) 1202 1203...and these are unique to Vim: 1204- changing the magic-ness of a pattern: \v \V \m \M 1205 (very useful for avoiding backslashitis) 1206- sequence of optionally matching atoms: \%[atoms] 1207- \& (which is to \| what "and" is to "or"; it forces several branches 1208 to match at one spot) 1209- matching lines/columns by number: \%5l \%5c \%5v 1210- setting the start and end of the match: \zs \ze 1211 1212============================================================================== 121310. Highlighting matches *match-highlight* 1214 1215 *:mat* *:match* 1216:mat[ch] {group} /{pattern}/ 1217 Define a pattern to highlight in the current window. It will 1218 be highlighted with {group}. Example: > 1219 :highlight MyGroup ctermbg=green guibg=green 1220 :match MyGroup /TODO/ 1221< Instead of // any character can be used to mark the start and 1222 end of the {pattern}. Watch out for using special characters, 1223 such as '"' and '|'. 1224 1225 {group} must exist at the moment this command is executed. 1226 1227 The {group} highlighting still applies when a character is 1228 to be highlighted for 'hlsearch', as the highlighting for 1229 matches is given higher priority than that of 'hlsearch'. 1230 Syntax highlighting (see 'syntax') is also overruled by 1231 matches. 1232 1233 Note that highlighting the last used search pattern with 1234 'hlsearch' is used in all windows, while the pattern defined 1235 with ":match" only exists in the current window. It is kept 1236 when switching to another buffer. 1237 1238 'ignorecase' does not apply, use |/\c| in the pattern to 1239 ignore case. Otherwise case is not ignored. 1240 1241 'redrawtime' defines the maximum time searched for pattern 1242 matches. 1243 1244 When matching end-of-line and Vim redraws only part of the 1245 display you may get unexpected results. That is because Vim 1246 looks for a match in the line where redrawing starts. 1247 1248 Also see |matcharg()| and |getmatches()|. The former returns 1249 the highlight group and pattern of a previous |:match| 1250 command. The latter returns a list with highlight groups and 1251 patterns defined by both |matchadd()| and |:match|. 1252 1253 Highlighting matches using |:match| are limited to three 1254 matches (aside from |:match|, |:2match| and |:3match|are 1255 available). |matchadd()| does not have this limitation and in 1256 addition makes it possible to prioritize matches. 1257 1258 Another example, which highlights all characters in virtual 1259 column 72 and more: > 1260 :highlight rightMargin term=bold ctermfg=blue guifg=blue 1261 :match rightMargin /.\%>72v/ 1262< To highlight all character that are in virtual column 7: > 1263 :highlight col8 ctermbg=grey guibg=grey 1264 :match col8 /\%<8v.\%>7v/ 1265< Note the use of two items to also match a character that 1266 occupies more than one virtual column, such as a TAB. 1267 1268:mat[ch] 1269:mat[ch] none 1270 Clear a previously defined match pattern. 1271 1272 1273:2mat[ch] {group} /{pattern}/ *:2match* 1274:2mat[ch] 1275:2mat[ch] none 1276:3mat[ch] {group} /{pattern}/ *:3match* 1277:3mat[ch] 1278:3mat[ch] none 1279 Just like |:match| above, but set a separate match. Thus 1280 there can be three matches active at the same time. The match 1281 with the lowest number has priority if several match at the 1282 same position. 1283 The ":3match" command is used by the |matchparen| plugin. You 1284 are suggested to use ":match" for manual matching and 1285 ":2match" for another plugin. 1286 1287 1288 vim:tw=78:ts=8:ft=help:norl: 1289