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