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