1 *pattern.txt* For Vim version 7.4. Last change: 2013 Jul 06
4 VIM REFERENCE MANUAL by Bram Moolenaar
7 Patterns and search commands *pattern-searches*
9 The very basics can be found in section |03.9| of the user manual. A few more
10 explanations are in chapter 27 |usr_27.txt|.
12 1. Search commands |search-commands|
13 2. The definition of a pattern |search-pattern|
15 4. Overview of pattern items |pattern-overview|
16 5. Multi items |pattern-multi-items|
17 6. Ordinary atoms |pattern-atoms|
18 7. Ignoring case in a pattern |/ignorecase|
19 8. Composing characters |patterns-composing|
20 9. Compare with Perl patterns |perl-patterns|
21 10. Highlighting matches |match-highlight|
23 ==============================================================================
24 1. Search commands *search-commands*
27 /{pattern}[/]<CR> Search forward for the [count]'th occurrence of
28 {pattern} |exclusive|.
30 /{pattern}/{offset}<CR> Search forward for the [count]'th occurrence of
31 {pattern} and go |{offset}| lines up or down.
35 /<CR> Search forward for the [count]'th occurrence of the
36 latest used pattern |last-pattern| with latest used
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.
44 ?{pattern}[?]<CR> Search backward for the [count]'th previous
45 occurrence of {pattern} |exclusive|.
47 ?{pattern}?{offset}<CR> Search backward for the [count]'th previous
48 occurrence of {pattern} and go |{offset}| lines up or
52 ?<CR> Search backward for the [count]'th occurrence of the
53 latest used pattern |last-pattern| with latest used
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.
61 n Repeat the latest "/" or "?" [count] times.
62 |last-pattern| {Vi: no count}
65 N Repeat the latest "/" or "?" [count] times in
66 opposite direction. |last-pattern| {Vi: no count}
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
75 3. the non-blank word under the cursor
76 4. the first non-blank word after the cursor,
78 Only whole keywords are searched for, like with the
79 command "/\<keyword\>". |exclusive| {not in Vi}
80 'ignorecase' is used, 'smartcase' is not.
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}
89 g* 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}
94 g# 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}
99 gd 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).
117 gD Goto global Declaration. When the cursor is on a
118 global variable that is defined in the file, this
119 command will jump to its declaration. This works just
120 like "gd", except that the search for the keyword
121 always starts in line 1. {not in Vi}
124 1gd Like "gd", but ignore matches inside a {} block that
125 ends before the cursor position. {not in Vi}
128 1gD Like "gD", but ignore matches inside a {} block that
129 ends before the cursor position. {not in Vi}
132 CTRL-C Interrupt current (search) command. Use CTRL-Break on
133 MS-DOS |dos-CTRL-Break|.
134 In Normal mode, any pending command is aborted.
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.
145 While typing the search pattern the current match will be shown if the
146 'incsearch' option is on. Remember that you still have to finish the search
147 command with <CR> to actually position the cursor at the displayed match. Or
148 use <Esc> to abandon the search.
150 All matches for the last used search pattern will be highlighted if you set
151 the 'hlsearch' option. This can be suspended with the |:nohlsearch| command.
153 When no match is found you get the error: *E486* Pattern not found
154 Note that for the |:global| command this behaves like a normal message, for Vi
155 compatibility. For the |:s| command the "e" flag can be used to avoid the
156 error message |:s_flags|.
158 *search-offset* *{offset}*
159 These commands search for the specified pattern. With "/" and "?" an
160 additional offset may be given. There are two types of offsets: line offsets
161 and character offsets. {the character offsets are not in Vi}
163 The offset gives the cursor position relative to the found match:
164 [num] [num] lines downwards, in column 1
165 +[num] [num] lines downwards, in column 1
166 -[num] [num] lines upwards, in column 1
167 e[+num] [num] characters to the right of the end of the match
168 e[-num] [num] characters to the left of the end of the match
169 s[+num] [num] characters to the right of the start of the match
170 s[-num] [num] characters to the left of the start of the match
171 b[+num] [num] identical to s[+num] above (mnemonic: begin)
172 b[-num] [num] identical to s[-num] above (mnemonic: begin)
173 ;{pattern} perform another search, see |//;|
175 If a '-' or '+' is given but [num] is omitted, a count of one will be used.
176 When including an offset with 'e', the search becomes inclusive (the
177 character the cursor lands on is included in operations).
181 pattern cursor position ~
182 /test/+1 one line below "test", in column 1
183 /test/e on the last t of "test"
184 /test/s+2 on the 's' of "test"
185 /test/b-3 three characters before "test"
187 If one of these commands is used after an operator, the characters between
188 the cursor position before and after the search is affected. However, if a
189 line offset is given, the whole lines between the two cursor positions are
192 An example of how to search for matches with a pattern and change the match
195 c//e change until end of match
196 bar<Esc> type replacement
197 //<CR> go to start of next match
198 c//e change until end of match
199 beep<Esc> type another replacement
203 A very special offset is ';' followed by another search command. For example: >
208 The first one first finds the next occurrence of "test 1", and then the first
209 occurrence of "test" after that.
211 This is like executing two search commands after each other, except that:
212 - It can be used as a single motion command after an operator.
213 - The direction for a following "n" or "N" command comes from the first
215 - When an error occurs the cursor is not moved at all.
218 The last used pattern and offset are remembered. They can be used to repeat
219 the search, possibly in another direction or with another count. Note that
220 two patterns are remembered: One for 'normal' search commands and one for the
221 substitute command ":s". Each time an empty pattern is given, the previously
222 used pattern is used. However, if there is no previous search command, a
223 previous substitute pattern is used, if possible.
225 The 'magic' option sticks with the last used pattern. If you change 'magic',
226 this will not change how the last used pattern will be interpreted.
227 The 'ignorecase' option does not do this. When 'ignorecase' is changed, it
228 will result in the pattern to match other text.
230 All matches for the last used search pattern will be highlighted if you set
231 the 'hlsearch' option.
233 To clear the last used search pattern: >
235 This will not set the pattern to an empty string, because that would match
236 everywhere. The pattern is really cleared, like when starting Vim.
238 The search usually skips matches that don't move the cursor. Whether the next
239 match is found at the next character or after the skipped match depends on the
240 'c' flag in 'cpoptions'. See |cpo-c|.
241 with 'c' flag: "/..." advances 1 to 3 characters
242 without 'c' flag: "/..." advances 1 character
243 The unpredictability with the 'c' flag is caused by starting the search in the
244 first column, skipping matches until one is found past the cursor position.
246 When searching backwards, searching starts at the start of the line, using the
247 'c' flag in 'cpoptions' as described above. Then the last match before the
248 cursor position is used.
250 In Vi the ":tag" command sets the last search pattern when the tag is searched
251 for. In Vim this is not done, the previous search pattern is still remembered,
252 unless the 't' flag is present in 'cpoptions'. The search pattern is always
253 put in the search history.
255 If the 'wrapscan' option is on (which is the default), searches wrap around
256 the end of the buffer. If 'wrapscan' is not set, the backward search stops
257 at the beginning and the forward search stops at the end of the buffer. If
258 'wrapscan' is set and the pattern was not found the error message "pattern
259 not found" is given, and the cursor will not be moved. If 'wrapscan' is not
260 set the message becomes "search hit BOTTOM without match" when searching
261 forward, or "search hit TOP without match" when searching backward. If
262 wrapscan is set and the search wraps around the end of the file the message
263 "search hit TOP, continuing at BOTTOM" or "search hit BOTTOM, continuing at
264 TOP" is given when searching backwards or forwards respectively. This can be
265 switched off by setting the 's' flag in the 'shortmess' option. The highlight
266 method 'w' is used for this message (default: standout).
269 You can limit the search command "/" to a certain range of lines by including
270 \%>l items. For example, to match the word "limit" below line 199 and above
275 Another way is to use the ":substitute" command with the 'c' flag. Example: >
277 This command will search from the cursor position until line 300 for
278 "Pattern". At the match, you will be asked to type a character. Type 'q' to
279 stop at this match, type 'n' to find the next match.
281 The "*", "#", "g*" and "g#" commands look for a word near the cursor in this
282 order, the first one that is found is used:
283 - The keyword currently under the cursor.
284 - The first keyword to the right of the cursor, in the same line.
285 - The WORD currently under the cursor.
286 - The first WORD to the right of the cursor, in the same line.
287 The keyword may only contain letters and characters in 'iskeyword'.
288 The WORD may contain any non-blanks (<Tab>s and/or <Space>s).
289 Note that if you type with ten fingers, the characters are easy to remember:
290 the "#" is under your left hand middle finger (search to the left and up) and
291 the "*" is under your right hand middle finger (search to the right and down).
292 (this depends on your keyboard layout though).
294 ==============================================================================
295 2. The definition of a pattern *search-pattern* *pattern* *[pattern]*
296 *regular-expression* *regexp* *Pattern*
299 For starters, read chapter 27 of the user manual |usr_27.txt|.
301 */bar* */\bar* */pattern*
302 1. A pattern is one or more branches, separated by "\|". It matches anything
303 that matches one of the branches. Example: "foo\|beep" matches "foo" and
304 matches "beep". If more than one branch matches, the first one is used.
308 or branch \| branch \| branch
312 2. A branch is one or more concats, separated by "\&". It matches the last
313 concat, but only if all the preceding concats also match at the same
315 "foobeep\&..." matches "foo" in "foobeep".
316 ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
320 or concat \& concat \& concat
324 3. A concat is one or more pieces, concatenated. It matches a match for the
325 first piece, followed by a match for the second piece, etc. Example:
326 "f[0-9]b", first matches "f", then a digit and then "b".
334 4. A piece is an atom, possibly followed by a multi, an indication of how many
335 times the atom can be matched. Example: "a*" matches any sequence of "a"
336 characters: "", "a", "aa", etc. See |/multi|.
342 5. An atom can be one of a long list of items. Many atoms match one character
343 in the text. It is often an ordinary character or a character class.
344 Braces can be used to make a pattern into an atom. The "\z(\)" construct
345 is only for syntax highlighting.
347 atom ::= ordinary-atom |/ordinary-atom|
348 or \( pattern \) |/\(|
349 or \%( pattern \) |/\%(|
350 or \z( pattern \) |/\z(|
353 */\%#=* *two-engines* *NFA*
354 Vim includes two regexp engines:
355 1. An old, backtracking engine that supports everything.
356 2. A new, NFA engine that works much faster on some patterns, but does not
359 Vim will automatically select the right engine for you. However, if you run
360 into a problem or want to specifically select one engine or the other, you can
361 prepend one of the following to the pattern:
363 \%#=0 Force automatic selection. Only has an effect when
364 'regexpengine' has been set to a non-zero value.
365 \%#=1 Force using the old engine.
366 \%#=2 Force using the NFA engine.
368 You can also use the 'regexpengine' option to change the default.
370 *E864* *E868* *E874* *E875* *E876* *E877* *E878*
371 If selecting the NFA engine and it runs into something that is not implemented
372 the pattern will not match. This is only useful when debugging Vim.
374 ==============================================================================
377 Some characters in the pattern are taken literally. They match with the same
378 character in the text. When preceded with a backslash however, these
379 characters get a special meaning.
381 Other characters have a special meaning without a backslash. They need to be
382 preceded with a backslash to match literally.
384 If a character is taken literally or not depends on the 'magic' option and the
385 items mentioned next.
387 Use of "\m" makes the pattern after it be interpreted as if 'magic' is set,
388 ignoring the actual value of the 'magic' option.
389 Use of "\M" makes the pattern after it be interpreted as if 'nomagic' is used.
391 Use of "\v" means that in the pattern after it all ASCII characters except
392 '0'-'9', 'a'-'z', 'A'-'Z' and '_' have a special meaning. "very magic"
394 Use of "\V" means that in the pattern after it only the backslash has a
395 special meaning. "very nomagic"
398 after: \v \m \M \V matches ~
400 $ $ $ \$ matches end-of-line
401 . . \. \. matches any character
402 * * \* \* any number of the previous atom
403 () \(\) \(\) \(\) grouping into an atom
404 | \| \| \| separating alternatives
405 \a \a \a \a alphabetic character
406 \\ \\ \\ \\ literal backslash
407 \. \. . . literal dot
411 {only Vim supports \m, \M, \v and \V}
413 It is recommended to always keep the 'magic' option at the default setting,
414 which is 'magic'. This avoids portability problems. To make a pattern immune
415 to the 'magic' option being set or not, put "\m" or "\M" at the start of the
418 ==============================================================================
419 4. Overview of pattern items *pattern-overview*
420 *E865* *E866* *E867* *E869*
422 Overview of multi items. */multi* *E61* *E62*
423 More explanation and examples below, follow the links. *E64* *E871*
426 'magic' 'nomagic' matches of the preceding atom ~
427 |/star| * \* 0 or more as many as possible
428 |/\+| \+ \+ 1 or more as many as possible (*)
429 |/\=| \= \= 0 or 1 as many as possible (*)
430 |/\?| \? \? 0 or 1 as many as possible (*)
432 |/\{| \{n,m} \{n,m} n to m as many as possible (*)
433 \{n} \{n} n exactly (*)
434 \{n,} \{n,} at least n as many as possible (*)
435 \{,m} \{,m} 0 to m as many as possible (*)
436 \{} \{} 0 or more as many as possible (same as *) (*)
438 |/\{-| \{-n,m} \{-n,m} n to m as few as possible (*)
439 \{-n} \{-n} n exactly (*)
440 \{-n,} \{-n,} at least n as few as possible (*)
441 \{-,m} \{-,m} 0 to m as few as possible (*)
442 \{-} \{-} 0 or more as few as possible (*)
445 |/\@>| \@> \@> 1, like matching a whole pattern (*)
446 |/\@=| \@= \@= nothing, requires a match |/zero-width| (*)
447 |/\@!| \@! \@! nothing, requires NO match |/zero-width| (*)
448 |/\@<=| \@<= \@<= nothing, requires a match behind |/zero-width| (*)
449 |/\@<!| \@<! \@<! nothing, requires NO match behind |/zero-width| (*)
454 Overview of ordinary atoms. */ordinary-atom*
455 More explanation and examples below, follow the links.
458 magic nomagic matches ~
459 |/^| ^ ^ start-of-line (at start of pattern) |/zero-width|
460 |/\^| \^ \^ literal '^'
461 |/\_^| \_^ \_^ start-of-line (used anywhere) |/zero-width|
462 |/$| $ $ end-of-line (at end of pattern) |/zero-width|
463 |/\$| \$ \$ literal '$'
464 |/\_$| \_$ \_$ end-of-line (used anywhere) |/zero-width|
465 |/.| . \. any single character (not an end-of-line)
466 |/\_.| \_. \_. any single character or end-of-line
467 |/\<| \< \< beginning of a word |/zero-width|
468 |/\>| \> \> end of a word |/zero-width|
469 |/\zs| \zs \zs anything, sets start of match
470 |/\ze| \ze \ze anything, sets end of match
471 |/\%^| \%^ \%^ beginning of file |/zero-width| *E71*
472 |/\%$| \%$ \%$ end of file |/zero-width|
473 |/\%V| \%V \%V inside Visual area |/zero-width|
474 |/\%#| \%# \%# cursor position |/zero-width|
475 |/\%'m| \%'m \%'m mark m position |/zero-width|
476 |/\%l| \%23l \%23l in line 23 |/zero-width|
477 |/\%c| \%23c \%23c in column 23 |/zero-width|
478 |/\%v| \%23v \%23v in virtual column 23 |/zero-width|
480 Character classes {not in Vi}: */character-classes*
481 |/\i| \i \i identifier character (see 'isident' option)
482 |/\I| \I \I like "\i", but excluding digits
483 |/\k| \k \k keyword character (see 'iskeyword' option)
484 |/\K| \K \K like "\k", but excluding digits
485 |/\f| \f \f file name character (see 'isfname' option)
486 |/\F| \F \F like "\f", but excluding digits
487 |/\p| \p \p printable character (see 'isprint' option)
488 |/\P| \P \P like "\p", but excluding digits
489 |/\s| \s \s whitespace character: <Space> and <Tab>
490 |/\S| \S \S non-whitespace character; opposite of \s
491 |/\d| \d \d digit: [0-9]
492 |/\D| \D \D non-digit: [^0-9]
493 |/\x| \x \x hex digit: [0-9A-Fa-f]
494 |/\X| \X \X non-hex digit: [^0-9A-Fa-f]
495 |/\o| \o \o octal digit: [0-7]
496 |/\O| \O \O non-octal digit: [^0-7]
497 |/\w| \w \w word character: [0-9A-Za-z_]
498 |/\W| \W \W non-word character: [^0-9A-Za-z_]
499 |/\h| \h \h head of word character: [A-Za-z_]
500 |/\H| \H \H non-head of word character: [^A-Za-z_]
501 |/\a| \a \a alphabetic character: [A-Za-z]
502 |/\A| \A \A non-alphabetic character: [^A-Za-z]
503 |/\l| \l \l lowercase character: [a-z]
504 |/\L| \L \L non-lowercase character: [^a-z]
505 |/\u| \u \u uppercase character: [A-Z]
506 |/\U| \U \U non-uppercase character [^A-Z]
507 |/\_| \_x \_x where x is any of the characters above: character
508 class with end-of-line included
509 (end of character classes)
515 |/\n| \n \n end-of-line
516 |/~| ~ \~ last given substitute string
517 |/\1| \1 \1 same string as matched by first \(\) {not in Vi}
518 |/\2| \2 \2 Like "\1", but uses second \(\)
520 |/\9| \9 \9 Like "\1", but uses ninth \(\)
522 |/\z1| \z1 \z1 only for syntax highlighting, see |:syn-ext-match|
524 |/\z1| \z9 \z9 only for syntax highlighting, see |:syn-ext-match|
526 x x a character with no special meaning matches itself
528 |/[]| [] \[] any character specified inside the []
529 |/\%[]| \%[] \%[] a sequence of optionally matched atoms
531 |/\c| \c \c ignore case, do not use the 'ignorecase' option
532 |/\C| \C \C match case, do not use the 'ignorecase' option
533 |/\Z| \Z \Z ignore differences in Unicode "combining characters".
534 Useful when searching voweled Hebrew or Arabic text.
536 |/\m| \m \m 'magic' on for the following chars in the pattern
537 |/\M| \M \M 'magic' off for the following chars in the pattern
538 |/\v| \v \v the following chars in the pattern are "very magic"
539 |/\V| \V \V the following chars in the pattern are "very nomagic"
540 |/\%#=| \%#=1 \%#=1 select regexp engine |/zero-width|
542 |/\%d| \%d \%d match specified decimal character (eg \%d123)
543 |/\%x| \%x \%x match specified hex character (eg \%x2a)
544 |/\%o| \%o \%o match specified octal character (eg \%o040)
545 |/\%u| \%u \%u match specified multibyte character (eg \%u20ac)
546 |/\%U| \%U \%U match specified large multibyte character (eg
552 \<[a-zA-Z_][a-zA-Z0-9_]*
553 An identifier (e.g., in a C program).
555 \(\.$\|\. \) A period followed by <EOL> or a space.
557 [.!?][])"']*\($\|[ ]\) A search pattern that finds the end of a sentence,
558 with almost the same definition as the ")" command.
560 cat\Z Both "cat" and "càt" ("a" followed by 0x0300)
561 Does not match "càt" (character 0x00e0), even
562 though it may look the same.
565 ==============================================================================
566 5. Multi items *pattern-multi-items*
568 An atom can be followed by an indication of how many times the atom can be
569 matched and in what way. This is called a multi. See |/multi| for an
572 */star* */\star* *E56*
573 * (use \* when 'magic' is not set)
574 Matches 0 or more of the preceding atom, as many as possible.
575 Example 'nomagic' matches ~
576 a* a\* "", "a", "aa", "aaa", etc.
577 .* \.\* anything, also an empty string, no end-of-line
578 \_.* \_.\* everything up to the end of the buffer
579 \_.*END \_.\*END everything up to and including the last "END"
582 Exception: When "*" is used at the start of the pattern or just after
583 "^" it matches the star character.
585 Be aware that repeating "\_." can match a lot of text and take a long
586 time. For example, "\_.*END" matches all text from the current
587 position to the last occurrence of "END" in the file. Since the "*"
588 will match as many as possible, this first skips over all lines until
589 the end of the file and then tries matching "END", backing up one
593 \+ Matches 1 or more of the preceding atom, as many as possible. {not in
596 ^.\+$ any non-empty line
597 \s\+ white space of at least one character
600 \= Matches 0 or 1 of the preceding atom, as many as possible. {not in Vi}
605 \? Just like \=. Cannot be used when searching backwards with the "?"
608 */\{* *E58* *E60* *E554* *E870*
609 \{n,m} Matches n to m of the preceding atom, as many as possible
610 \{n} Matches n of the preceding atom
611 \{n,} Matches at least n of the preceding atom, as many as possible
612 \{,m} Matches 0 to m of the preceding atom, as many as possible
613 \{} Matches 0 or more of the preceding atom, as many as possible (like *)
615 \{-n,m} matches n to m of the preceding atom, as few as possible
616 \{-n} matches n of the preceding atom
617 \{-n,} matches at least n of the preceding atom, as few as possible
618 \{-,m} matches 0 to m of the preceding atom, as few as possible
619 \{-} matches 0 or more of the preceding atom, as few as possible
620 {Vi does not have any of these}
622 n and m are positive decimal numbers or zero
624 If a "-" appears immediately after the "{", then a shortest match
625 first algorithm is used (see example below). In particular, "\{-}" is
626 the same as "*" but uses the shortest match first algorithm. BUT: A
627 match that starts earlier is preferred over a shorter match: "a\{-}b"
628 matches "aaab" in "xaaab".
631 ab\{2,3}c "abbc" or "abbbc"
633 ab\{2,}c "abbc", "abbbc", "abbbbc", etc.
634 ab\{,3}c "ac", "abc", "abbc" or "abbbc"
635 a[bc]\{3}d "abbbd", "abbcd", "acbcd", "acccd", etc.
636 a\(bc\)\{1,2}d "abcd" or "abcbcd"
637 a[bc]\{-}[cd] "abc" in "abcd"
638 a[bc]*[cd] "abcd" in "abcd"
640 The } may optionally be preceded with a backslash: \{n,m\}.
643 \@= Matches the preceding atom with zero width. {not in Vi}
644 Like "(?=pattern)" in Perl.
646 foo\(bar\)\@= "foo" in "foobar"
647 foo\(bar\)\@=foo nothing
649 When using "\@=" (or "^", "$", "\<", "\>") no characters are included
650 in the match. These items are only used to check if a match can be
651 made. This can be tricky, because a match with following items will
652 be done in the same position. The last example above will not match
653 "foobarfoo", because it tries match "foo" in the same position where
656 Note that using "\&" works the same as using "\@=": "foo\&.." is the
657 same as "\(foo\)\@=..". But using "\&" is easier, you don't need the
662 \@! Matches with zero width if the preceding atom does NOT match at the
663 current position. |/zero-width| {not in Vi}
664 Like "(?!pattern)" in Perl.
666 foo\(bar\)\@! any "foo" not followed by "bar"
667 a.\{-}p\@! "a", "ap", "app", "appp", etc. not immediately
669 if \(\(then\)\@!.\)*$ "if " not followed by "then"
671 Using "\@!" is tricky, because there are many places where a pattern
672 does not match. "a.*p\@!" will match from an "a" to the end of the
673 line, because ".*" can match all characters in the line and the "p"
674 doesn't match at the end of the line. "a.\{-}p\@!" will match any
675 "a", "ap", "app", etc. that isn't followed by a "p", because the "."
676 can match a "p" and "p\@!" doesn't match after that.
678 You can't use "\@!" to look for a non-match before the matching
679 position: "\(foo\)\@!bar" will match "bar" in "foobar", because at the
680 position where "bar" matches, "foo" does not match. To avoid matching
681 "foobar" you could use "\(foo\)\@!...bar", but that doesn't match a
682 bar at the start of a line. Use "\(foo\)\@<!bar".
684 Useful example: to find "foo" in a line that does not contain "bar": >
685 /^\%(.*bar\)\@!.*\zsfoo
686 < This pattern first checks that there is not a single position in the
687 line where "bar" matches. If ".*bar" matches somewhere the \@! will
688 reject the pattern. When there is no match any "foo" will be found.
689 The "\zs" is to have the match start just before "foo".
692 \@<= Matches with zero width if the preceding atom matches just before what
693 follows. |/zero-width| {not in Vi}
694 Like "(?<=pattern)" in Perl, but Vim allows non-fixed-width patterns.
696 \(an\_s\+\)\@<=file "file" after "an" and white space or an
698 For speed it's often much better to avoid this multi. Try using "\zs"
699 instead |/\zs|. To match the same as the above example:
701 At least set a limit for the look-behind, see below.
703 "\@<=" and "\@<!" check for matches just before what follows.
704 Theoretically these matches could start anywhere before this position.
705 But to limit the time needed, only the line where what follows matches
706 is searched, and one line before that (if there is one). This should
707 be sufficient to match most things and not be too slow.
708 The part of the pattern after "\@<=" and "\@<!" are checked for a
709 match first, thus things like "\1" don't work to reference \(\) inside
710 the preceding atom. It does work the other way around:
712 \1\@<=,\([a-z]\+\) ",abc" in "abc,abc"
715 Like "\@<=" but only look back 123 bytes. This avoids trying lots
716 of matches that are known to fail and make executing the pattern very
717 slow. Example, check if there is a "<" just before "span":
719 This will try matching "<" only one byte before "span", which is the
720 only place that works anyway.
721 After crossing a line boundary, the limit is relative to the end of
722 the line. Thus the characters at the start of the line with the match
723 are not counted (this is just to keep it simple).
724 The number zero is the same as no limit.
727 \@<! Matches with zero width if the preceding atom does NOT match just
728 before what follows. Thus this matches if there is no position in the
729 current or previous line where the atom matches such that it ends just
730 before what follows. |/zero-width| {not in Vi}
731 Like "(?<!pattern)" in Perl, but Vim allows non-fixed-width patterns.
732 The match with the preceding atom is made to end just before the match
733 with what follows, thus an atom that ends in ".*" will work.
734 Warning: This can be slow (because many positions need to be checked
735 for a match). Use a limit if you can, see below.
737 \(foo\)\@<!bar any "bar" that's not in "foobar"
738 \(\/\/.*\)\@<!in "in" which is not after "//"
741 Like "\@<!" but only look back 123 bytes. This avoids trying lots of
742 matches that are known to fail and make executing the pattern very
746 \@> Matches the preceding atom like matching a whole pattern. {not in Vi}
747 Like "(?>pattern)" in Perl.
749 \(a*\)\@>a nothing (the "a*" takes all the "a"'s, there can't be
750 another one following)
752 This matches the preceding atom as if it was a pattern by itself. If
753 it doesn't match, there is no retry with shorter sub-matches or
754 anything. Observe this difference: "a*b" and "a*ab" both match
755 "aaab", but in the second case the "a*" matches only the first two
756 "a"s. "\(a*\)\@>ab" will not match "aaab", because the "a*" matches
757 the "aaa" (as many "a"s as possible), thus the "ab" can't match.
760 ==============================================================================
761 6. Ordinary atoms *pattern-atoms*
763 An ordinary atom can be:
766 ^ At beginning of pattern or after "\|", "\(", "\%(" or "\n": matches
767 start-of-line; at other positions, matches literal '^'. |/zero-width|
769 ^beep( the start of the C function "beep" (probably).
772 \^ Matches literal '^'. Can be used at any position in the pattern.
775 \_^ Matches start-of-line. |/zero-width| Can be used at any position in
778 \_s*\_^foo white space and blank lines and then "foo" at
782 $ At end of pattern or in front of "\|", "\)" or "\n" ('magic' on):
783 matches end-of-line <EOL>; at other positions, matches literal '$'.
787 \$ Matches literal '$'. Can be used at any position in the pattern.
790 \_$ Matches end-of-line. |/zero-width| Can be used at any position in the
791 pattern. Note that "a\_$b" never matches, since "b" cannot match an
792 end-of-line. Use "a\nb" instead |/\n|.
794 foo\_$\_s* "foo" at end-of-line and following white space and
797 . (with 'nomagic': \.) */.* */\.*
798 Matches any single character, but not an end-of-line.
801 \_. Matches any single character or end-of-line.
802 Careful: "\_.*" matches all text to the end of the buffer!
805 \< Matches the beginning of a word: The next char is the first char of a
806 word. The 'iskeyword' option specifies what is a word character.
810 \> Matches the end of a word: The previous char is the last char of a
811 word. The 'iskeyword' option specifies what is a word character.
815 \zs Matches at any position, and sets the start of the match there: The
816 next char is the first char of the whole match. |/zero-width|
819 < matches an "if" at the start of a line, ignoring white space.
820 Can be used multiple times, the last one encountered in a matching
821 branch is used. Example: >
823 < Finds the third occurrence of "Fab".
824 {not in Vi} {not available when compiled without the |+syntax| feature}
826 \ze Matches at any position, and sets the end of the match there: The
827 previous char is the last char of the whole match. |/zero-width|
828 Can be used multiple times, the last one encountered in a matching
830 Example: "end\ze\(if\|for\)" matches the "end" in "endif" and
832 {not in Vi} {not available when compiled without the |+syntax| feature}
834 */\%^* *start-of-file*
835 \%^ Matches start of the file. When matching with a string, matches the
836 start of the string. {not in Vi}
837 For example, to find the first "VIM" in a file: >
841 \%$ Matches end of the file. When matching with a string, matches the
842 end of the string. {not in Vi}
843 Note that this does NOT find the last "VIM" in a file: >
845 < It will find the next VIM, because the part after it will always
846 match. This one will find the last "VIM" in the file: >
847 /VIM\ze\(\(VIM\)\@!\_.\)*\%$
848 < This uses |/\@!| to ascertain that "VIM" does NOT match in any
849 position after the first "VIM".
850 Searching from the end of the file backwards is easier!
853 \%V Match inside the Visual area. When Visual mode has already been
854 stopped match in the area that |gv| would reselect.
855 This is a |/zero-width| match. To make sure the whole pattern is
856 inside the Visual area put it at the start and end of the pattern,
859 < Only works for the current buffer.
861 */\%#* *cursor-position*
862 \%# Matches with the cursor position. Only works when matching in a
863 buffer displayed in a window. {not in Vi}
864 WARNING: When the cursor is moved after the pattern was used, the
865 result becomes invalid. Vim doesn't automatically update the matches.
866 This is especially relevant for syntax highlighting and 'hlsearch'.
867 In other words: When the cursor moves the display isn't updated for
868 this change. An update is done for lines which are changed (the whole
869 line is updated) or when using the |CTRL-L| command (the whole screen
870 is updated). Example, to highlight the word under the cursor: >
872 < When 'hlsearch' is set and you move the cursor around and make changes
873 this will clearly show when the match is updated or not.
875 */\%'m* */\%<'m* */\%>'m*
876 \%'m Matches with the position of mark m.
877 \%<'m Matches before the position of mark m.
878 \%>'m Matches after the position of mark m.
879 Example, to highlight the text from mark 's to 'e: >
881 < Note that two dots are required to include mark 'e in the match. That
882 is because "\%<'e" matches at the character before the 'e mark, and
883 since it's a |/zero-width| match it doesn't include that character.
885 WARNING: When the mark is moved after the pattern was used, the result
886 becomes invalid. Vim doesn't automatically update the matches.
887 Similar to moving the cursor for "\%#" |/\%#|.
889 */\%l* */\%>l* */\%<l*
890 \%23l Matches in a specific line.
891 \%<23l Matches above a specific line (lower line number).
892 \%>23l Matches below a specific line (higher line number).
893 These three can be used to match specific lines in a buffer. The "23"
894 can be any line number. The first line is 1. {not in Vi}
895 WARNING: When inserting or deleting lines Vim does not automatically
896 update the matches. This means Syntax highlighting quickly becomes
898 Example, to highlight the line where the cursor currently is: >
899 :exe '/\%' . line(".") . 'l.*'
900 < When 'hlsearch' is set and you move the cursor around and make changes
901 this will clearly show when the match is updated or not.
903 */\%c* */\%>c* */\%<c*
904 \%23c Matches in a specific column.
905 \%<23c Matches before a specific column.
906 \%>23c Matches after a specific column.
907 These three can be used to match specific columns in a buffer or
908 string. The "23" can be any column number. The first column is 1.
909 Actually, the column is the byte number (thus it's not exactly right
910 for multi-byte characters). {not in Vi}
911 WARNING: When inserting or deleting text Vim does not automatically
912 update the matches. This means Syntax highlighting quickly becomes
914 Example, to highlight the column where the cursor currently is: >
915 :exe '/\%' . col(".") . 'c'
916 < When 'hlsearch' is set and you move the cursor around and make changes
917 this will clearly show when the match is updated or not.
918 Example for matching a single byte in column 44: >
920 < Note that "\%<46c" matches in column 45 when the "." matches a byte in
922 */\%v* */\%>v* */\%<v*
923 \%23v Matches in a specific virtual column.
924 \%<23v Matches before a specific virtual column.
925 \%>23v Matches after a specific virtual column.
926 These three can be used to match specific virtual columns in a buffer
927 or string. When not matching with a buffer in a window, the option
928 values of the current window are used (e.g., 'tabstop').
929 The "23" can be any column number. The first column is 1.
930 Note that some virtual column positions will never match, because they
931 are halfway through a tab or other character that occupies more than
932 one screen character. {not in Vi}
933 WARNING: When inserting or deleting text Vim does not automatically
934 update highlighted matches. This means Syntax highlighting quickly
936 Example, to highlight all the characters after virtual column 72: >
938 < When 'hlsearch' is set and you move the cursor around and make changes
939 this will clearly show when the match is updated or not.
940 To match the text up to column 17: >
942 < Column 17 is included, because that's where the "\%17v" matches,
943 even though this is a |/zero-width| match. Adding a dot to match the
944 next character has the same result: >
946 < This command does the same thing, but also matches when there is no
947 character in column 17: >
951 Character classes: {not in Vi}
952 \i identifier character (see 'isident' option) */\i*
953 \I like "\i", but excluding digits */\I*
954 \k keyword character (see 'iskeyword' option) */\k*
955 \K like "\k", but excluding digits */\K*
956 \f file name character (see 'isfname' option) */\f*
957 \F like "\f", but excluding digits */\F*
958 \p printable character (see 'isprint' option) */\p*
959 \P like "\p", but excluding digits */\P*
961 NOTE: the above also work for multi-byte characters. The ones below only
962 match ASCII characters, as indicated by the range.
964 *whitespace* *white-space*
965 \s whitespace character: <Space> and <Tab> */\s*
966 \S non-whitespace character; opposite of \s */\S*
967 \d digit: [0-9] */\d*
968 \D non-digit: [^0-9] */\D*
969 \x hex digit: [0-9A-Fa-f] */\x*
970 \X non-hex digit: [^0-9A-Fa-f] */\X*
971 \o octal digit: [0-7] */\o*
972 \O non-octal digit: [^0-7] */\O*
973 \w word character: [0-9A-Za-z_] */\w*
974 \W non-word character: [^0-9A-Za-z_] */\W*
975 \h head of word character: [A-Za-z_] */\h*
976 \H non-head of word character: [^A-Za-z_] */\H*
977 \a alphabetic character: [A-Za-z] */\a*
978 \A non-alphabetic character: [^A-Za-z] */\A*
979 \l lowercase character: [a-z] */\l*
980 \L non-lowercase character: [^a-z] */\L*
981 \u uppercase character: [A-Z] */\u*
982 \U non-uppercase character: [^A-Z] */\U*
984 NOTE: Using the atom is faster than the [] form.
986 NOTE: 'ignorecase', "\c" and "\C" are not used by character classes.
988 */\_* *E63* */\_i* */\_I* */\_k* */\_K* */\_f* */\_F*
989 */\_p* */\_P* */\_s* */\_S* */\_d* */\_D* */\_x* */\_X*
990 */\_o* */\_O* */\_w* */\_W* */\_h* */\_H* */\_a* */\_A*
991 */\_l* */\_L* */\_u* */\_U*
992 \_x Where "x" is any of the characters above: The character class with
994 (end of character classes)
996 \e matches <Esc> */\e*
997 \t matches <Tab> */\t*
998 \r matches <CR> */\r*
999 \b matches <BS> */\b*
1000 \n matches an end-of-line */\n*
1001 When matching in a string instead of buffer text a literal newline
1002 character is matched.
1004 ~ matches the last given substitute string */~* */\~*
1006 \(\) A pattern enclosed by escaped parentheses. */\(* */\(\)* */\)*
1007 E.g., "\(^a\)" matches 'a' at the start of a line.
1008 *E51* *E54* *E55* *E872* *E873*
1010 \1 Matches the same string that was matched by */\1* *E65*
1011 the first sub-expression in \( and \). {not in Vi}
1012 Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc.
1013 \2 Like "\1", but uses second sub-expression, */\2*
1015 \9 Like "\1", but uses ninth sub-expression. */\9*
1016 Note: The numbering of groups is done based on which "\(" comes first
1017 in the pattern (going left to right), NOT based on what is matched
1020 \%(\) A pattern enclosed by escaped parentheses. */\%(\)* */\%(* *E53*
1021 Just like \(\), but without counting it as a sub-expression. This
1022 allows using more groups and it's a little bit faster.
1025 x A single character, with no special meaning, matches itself
1028 \x A backslash followed by a single character, with no special meaning,
1029 is reserved for future expansions
1031 [] (with 'nomagic': \[]) */[]* */\[]* */\_[]* */collection*
1033 A collection. This is a sequence of characters enclosed in brackets.
1034 It matches any single character in the collection.
1036 [xyz] any 'x', 'y' or 'z'
1037 [a-zA-Z]$ any alphabetic character at the end of a line
1039 [А-яЁё] Russian alphabet (with utf-8 and cp1251)
1042 With "\_" prepended the collection also includes the end-of-line.
1043 The same can be done by including "\n" in the collection. The
1044 end-of-line is also matched when the collection starts with "^"! Thus
1045 "\_[^ab]" matches the end-of-line and any character but "a" and "b".
1046 This makes it Vi compatible: Without the "\_" or "\n" the collection
1047 does not match an end-of-line.
1049 When the ']' is not there Vim will not give an error message but
1050 assume no collection is used. Useful to search for '['. However, you
1051 do get E769 for internal searching.
1053 If the sequence begins with "^", it matches any single character NOT
1054 in the collection: "[^xyz]" matches anything but 'x', 'y' and 'z'.
1055 - If two characters in the sequence are separated by '-', this is
1056 shorthand for the full list of ASCII characters between them. E.g.,
1057 "[0-9]" matches any decimal digit. Non-ASCII characters can be
1058 used, but the character values must not be more than 256 apart.
1059 - A character class expression is evaluated to the set of characters
1060 belonging to that character class. The following character classes
1063 *[:alnum:]* [:alnum:] letters and digits
1064 *[:alpha:]* [:alpha:] letters
1065 *[:blank:]* [:blank:] space and tab characters
1066 *[:cntrl:]* [:cntrl:] control characters
1067 *[:digit:]* [:digit:] decimal digits
1068 *[:graph:]* [:graph:] printable characters excluding space
1069 *[:lower:]* [:lower:] lowercase letters (all letters when
1070 'ignorecase' is used)
1071 *[:print:]* [:print:] printable characters including space
1072 *[:punct:]* [:punct:] punctuation characters
1073 *[:space:]* [:space:] whitespace characters
1074 *[:upper:]* [:upper:] uppercase letters (all letters when
1075 'ignorecase' is used)
1076 *[:xdigit:]* [:xdigit:] hexadecimal digits
1077 *[:return:]* [:return:] the <CR> character
1078 *[:tab:]* [:tab:] the <Tab> character
1079 *[:escape:]* [:escape:] the <Esc> character
1080 *[:backspace:]* [:backspace:] the <BS> character
1081 The brackets in character class expressions are additional to the
1082 brackets delimiting a collection. For example, the following is a
1083 plausible pattern for a UNIX filename: "[-./[:alnum:]_~]\+" That is,
1084 a list of at least one character, each of which is either '-', '.',
1085 '/', alphabetic, numeric, '_' or '~'.
1086 These items only work for 8-bit characters.
1088 - An equivalence class. This means that characters are matched that
1089 have almost the same meaning, e.g., when ignoring accents. This
1090 only works for Unicode, latin1 and latin9. The form is:
1093 - A collation element. This currently simply accepts a single
1094 character in the form:
1097 - To include a literal ']', '^', '-' or '\' in the collection, put a
1098 backslash before it: "[xyz\]]", "[\^xyz]", "[xy\-z]" and "[xyz\\]".
1099 (Note: POSIX does not support the use of a backslash this way). For
1100 ']' you can also make it the first character (following a possible
1101 "^"): "[]xyz]" or "[^]xyz]" {not in Vi}.
1102 For '-' you can also make it the first or last character: "[-xyz]",
1103 "[^-xyz]" or "[xyz-]". For '\' you can also let it be followed by
1104 any character that's not in "^]-\bdertnoUux". "[\xyz]" matches '\',
1105 'x', 'y' and 'z'. It's better to use "\\" though, future expansions
1106 may use other characters after '\'.
1107 - Omitting the trailing ] is not considered an error. "[]" works like
1108 "[]]", it matches the ']' character.
1109 - The following translations are accepted when the 'l' flag is not
1110 included in 'cpoptions' {not in Vi}:
1113 \r <CR> (NOT end-of-line!)
1115 \n line break, see above |/[\n]|
1116 \d123 decimal number of character
1117 \o40 octal number of character up to 0377
1118 \x20 hexadecimal number of character up to 0xff
1119 \u20AC hex. number of multibyte character up to 0xffff
1120 \U1234 hex. number of multibyte character up to 0xffffffff
1121 NOTE: The other backslash codes mentioned above do not work inside
1123 - Matching with a collection can be slow, because each character in
1124 the text has to be compared with each character in the collection.
1125 Use one of the other atoms above when possible. Example: "\d" is
1126 much faster than "[0-9]" and matches the same characters.
1128 */\%[]* *E69* *E70* *E369*
1129 \%[] A sequence of optionally matched atoms. This always matches.
1130 It matches as much of the list of atoms it contains as possible. Thus
1131 it stops at the first atom that doesn't match. For example: >
1133 < matches "r", "re", "rea" or "read". The longest that matches is used.
1134 To match the Ex command "function", where "fu" is required and
1135 "nction" is optional, this would work: >
1137 < The end-of-word atom "\>" is used to avoid matching "fu" in "full".
1138 It gets more complicated when the atoms are not ordinary characters.
1139 You don't often have to use it, but it is possible. Example: >
1141 < Matches the words "r", "re", "ro", "rea", "roa", "read" and "road".
1142 There can be no \(\), \%(\) or \z(\) items inside the [] and \%[] does
1144 To include a "[" use "[[]" and for "]" use []]", e.g.,: >
1146 < matches "index" "index[", "index[0" and "index[0]".
1147 {not available when compiled without the |+syntax| feature}
1149 */\%d* */\%x* */\%o* */\%u* */\%U* *E678*
1151 \%d123 Matches the character specified with a decimal number. Must be
1152 followed by a non-digit.
1153 \%o40 Matches the character specified with an octal number up to 0377.
1154 Numbers below 040 must be followed by a non-octal digit or a non-digit.
1155 \%x2a Matches the character specified with up to two hexadecimal characters.
1156 \%u20AC Matches the character specified with up to four hexadecimal
1158 \%U1234abcd Matches the character specified with up to eight hexadecimal
1161 ==============================================================================
1162 7. Ignoring case in a pattern */ignorecase*
1164 If the 'ignorecase' option is on, the case of normal letters is ignored.
1165 'smartcase' can be set to ignore case when the pattern contains lowercase
1168 When "\c" appears anywhere in the pattern, the whole pattern is handled like
1169 'ignorecase' is on. The actual value of 'ignorecase' and 'smartcase' is
1170 ignored. "\C" does the opposite: Force matching case for the whole pattern.
1171 {only Vim supports \c and \C}
1172 Note that 'ignorecase', "\c" and "\C" are not used for the character classes.
1175 pattern 'ignorecase' 'smartcase' matches ~
1177 foo on - foo Foo FOO
1178 Foo on off foo Foo FOO
1180 \cfoo - - foo Foo FOO
1183 Technical detail: *NL-used-for-Nul*
1184 <Nul> characters in the file are stored as <NL> in memory. In the display
1185 they are shown as "^@". The translation is done when reading and writing
1186 files. To match a <Nul> with a search pattern you can just enter CTRL-@ or
1187 "CTRL-V 000". This is probably just what you expect. Internally the
1188 character is replaced with a <NL> in the search pattern. What is unusual is
1189 that typing CTRL-V CTRL-J also inserts a <NL>, thus also searches for a <Nul>
1190 in the file. {Vi cannot handle <Nul> characters in the file at all}
1193 When 'fileformat' is "mac", <NL> characters in the file are stored as <CR>
1194 characters internally. In the text they are shown as "^J". Otherwise this
1195 works similar to the usage of <NL> for a <Nul>.
1197 When working with expression evaluation, a <NL> character in the pattern
1198 matches a <NL> in the string. The use of "\n" (backslash n) to match a <NL>
1199 doesn't work there, it only works to match text in the buffer.
1201 *pattern-multi-byte*
1202 Patterns will also work with multi-byte characters, mostly as you would
1203 expect. But invalid bytes may cause trouble, a pattern with an invalid byte
1204 will probably never match.
1206 ==============================================================================
1207 8. Composing characters *patterns-composing*
1210 When "\Z" appears anywhere in the pattern, composing characters are ignored.
1211 Thus only the base characters need to match, the composing characters may be
1212 different and the number of composing characters may differ. Only relevant
1213 when 'encoding' is "utf-8".
1214 Exception: If the pattern starts with one or more composing characters, these
1217 When a composing character appears at the start of the pattern of after an
1218 item that doesn't include the composing character, a match is found at any
1219 character that includes this composing character.
1221 When using a dot and a composing character, this works the same as the
1222 composing character by itself, except that it doesn't matter what comes before
1225 The order of composing characters does not matter. Also, the text may have
1226 more composing characters than the pattern, it still matches. But all
1227 composing characters in the pattern must be found in the text.
1229 Suppose B is a base character and x and y are composing characters:
1230 pattern text match ~
1231 Bxy Bxy yes (perfect match)
1232 Bxy Byx yes (order ignored)
1233 Bxy By no (x missing)
1234 Bxy Bx no (y missing)
1235 Bx Bx yes (perfect match)
1236 Bx By no (x missing)
1237 Bx Bxy yes (extra y ignored)
1238 Bx Byx yes (extra y ignored)
1240 ==============================================================================
1241 9. Compare with Perl patterns *perl-patterns*
1243 Vim's regexes are most similar to Perl's, in terms of what you can do. The
1244 difference between them is mostly just notation; here's a summary of where
1247 Capability in Vimspeak in Perlspeak ~
1248 ----------------------------------------------------------------
1249 force case insensitivity \c (?i)
1250 force case sensitivity \C (?-i)
1251 backref-less grouping \%(atom\) (?:atom)
1252 conservative quantifiers \{-n,m} *?, +?, ??, {}?
1253 0-width match atom\@= (?=atom)
1254 0-width non-match atom\@! (?!atom)
1255 0-width preceding match atom\@<= (?<=atom)
1256 0-width preceding non-match atom\@<! (?<!atom)
1257 match without retry atom\@> (?>atom)
1259 Vim and Perl handle newline characters inside a string a bit differently:
1261 In Perl, ^ and $ only match at the very beginning and end of the text,
1262 by default, but you can set the 'm' flag, which lets them match at
1263 embedded newlines as well. You can also set the 's' flag, which causes
1264 a . to match newlines as well. (Both these flags can be changed inside
1265 a pattern using the same syntax used for the i flag above, BTW.)
1267 On the other hand, Vim's ^ and $ always match at embedded newlines, and
1268 you get two separate atoms, \%^ and \%$, which only match at the very
1269 start and end of the text, respectively. Vim solves the second problem
1270 by giving you the \_ "modifier": put it in front of a . or a character
1271 class, and they will match newlines as well.
1273 Finally, these constructs are unique to Perl:
1274 - execution of arbitrary code in the regex: (?{perl code})
1275 - conditional expressions: (?(condition)true-expr|false-expr)
1277 ...and these are unique to Vim:
1278 - changing the magic-ness of a pattern: \v \V \m \M
1279 (very useful for avoiding backslashitis)
1280 - sequence of optionally matching atoms: \%[atoms]
1281 - \& (which is to \| what "and" is to "or"; it forces several branches
1282 to match at one spot)
1283 - matching lines/columns by number: \%5l \%5c \%5v
1284 - setting the start and end of the match: \zs \ze
1286 ==============================================================================
1287 10. Highlighting matches *match-highlight*
1290 :mat[ch] {group} /{pattern}/
1291 Define a pattern to highlight in the current window. It will
1292 be highlighted with {group}. Example: >
1293 :highlight MyGroup ctermbg=green guibg=green
1294 :match MyGroup /TODO/
1295 < Instead of // any character can be used to mark the start and
1296 end of the {pattern}. Watch out for using special characters,
1297 such as '"' and '|'.
1299 {group} must exist at the moment this command is executed.
1301 The {group} highlighting still applies when a character is
1302 to be highlighted for 'hlsearch', as the highlighting for
1303 matches is given higher priority than that of 'hlsearch'.
1304 Syntax highlighting (see 'syntax') is also overruled by
1307 Note that highlighting the last used search pattern with
1308 'hlsearch' is used in all windows, while the pattern defined
1309 with ":match" only exists in the current window. It is kept
1310 when switching to another buffer.
1312 'ignorecase' does not apply, use |/\c| in the pattern to
1313 ignore case. Otherwise case is not ignored.
1315 'redrawtime' defines the maximum time searched for pattern
1318 When matching end-of-line and Vim redraws only part of the
1319 display you may get unexpected results. That is because Vim
1320 looks for a match in the line where redrawing starts.
1322 Also see |matcharg()| and |getmatches()|. The former returns
1323 the highlight group and pattern of a previous |:match|
1324 command. The latter returns a list with highlight groups and
1325 patterns defined by both |matchadd()| and |:match|.
1327 Highlighting matches using |:match| are limited to three
1328 matches (aside from |:match|, |:2match| and |:3match|are
1329 available). |matchadd()| does not have this limitation and in
1330 addition makes it possible to prioritize matches.
1332 Another example, which highlights all characters in virtual
1333 column 72 and more: >
1334 :highlight rightMargin term=bold ctermfg=blue guifg=blue
1335 :match rightMargin /.\%>72v/
1336 < To highlight all character that are in virtual column 7: >
1337 :highlight col8 ctermbg=grey guibg=grey
1338 :match col8 /\%<8v.\%>7v/
1339 < Note the use of two items to also match a character that
1340 occupies more than one virtual column, such as a TAB.
1344 Clear a previously defined match pattern.
1347 :2mat[ch] {group} /{pattern}/ *:2match*
1350 :3mat[ch] {group} /{pattern}/ *:3match*
1353 Just like |:match| above, but set a separate match. Thus
1354 there can be three matches active at the same time. The match
1355 with the lowest number has priority if several match at the
1357 The ":3match" command is used by the |matchparen| plugin. You
1358 are suggested to use ":match" for manual matching and
1359 ":2match" for another plugin.
1362 vim:tw=78:ts=8:ft=help:norl: