blob: 2cc59218949b3418011f06b44705f92aa7c397eb [file] [log] [blame]
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001*pattern.txt* For Vim version 7.0aa. Last change: 2005 Sep 12
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*
259You cannot limit the search command "/" to a certain range of lines. A trick
260to do this anyway is to use the ":substitute" command with the 'c' flag.
261Example: >
262 :.,300s/Pattern//gc
263This command will search from the cursor position until line 300 for
264"Pattern". At the match, you will be asked to type a character. Type 'q' to
265stop at this match, type 'n' to find the next match.
266
267The "*", "#", "g*" and "g#" commands look for a word near the cursor in this
268order, the first one that is found is used:
269- The keyword currently under the cursor.
270- The first keyword to the right of the cursor, in the same line.
271- The WORD currently under the cursor.
272- The first WORD to the right of the cursor, in the same line.
273The keyword may only contain letters and characters in 'iskeyword'.
274The WORD may contain any non-blanks (<Tab>s and/or <Space>s).
275Note that if you type with ten fingers, the characters are easy to remember:
276the "#" is under your left hand middle finger (search to the left and up) and
277the "*" is under your right hand middle finger (search to the right and down).
278(this depends on your keyboard layout though).
279
280==============================================================================
2812. The definition of a pattern *search-pattern* *pattern* *[pattern]*
282 *regular-expression* *regexp* *Pattern*
Bram Moolenaarf1f8bc52005-03-07 23:20:08 +0000283 *E76* *E383* *E476*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
285For starters, read chapter 27 of the user manual |usr_27.txt|.
286
287 */bar* */\bar* */pattern*
2881. A pattern is one or more branches, separated by "\|". It matches anything
289 that matches one of the branches. Example: "foo\|beep" matches "foo" and
290 matches "beep". If more than one branch matches, the first one is used.
291
292 pattern ::= branch
293 or branch \| branch
294 or branch \| branch \| branch
295 etc.
296
297 */branch* */\&*
2982. A branch is one or more concats, separated by "\&". It matches the last
299 concat, but only if all the preceding concats also match at the same
300 position. Examples:
301 "foobeep\&..." matches "foo" in "foobeep".
302 ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
303
304 branch ::= concat
305 or concat \& concat
306 or concat \& concat \& concat
307 etc.
308
309 */concat*
3103. A concat is one or more pieces, concatenated. It matches a match for the
311 first piece, followed by a match for the second piece, etc. Example:
312 "f[0-9]b", first matches "f", then a digit and then "b".
313
314 concat ::= piece
315 or piece piece
316 or piece piece piece
317 etc.
318
319 */piece*
3204. A piece is an atom, possibly followed by a multi, an indication of how many
321 times the atom can be matched. Example: "a*" matches any sequence of "a"
322 characters: "", "a", "aa", etc. See |/multi|.
323
324 piece ::= atom
325 or atom multi
326
327 */atom*
3285. An atom can be one of a long list of items. Many atoms match one character
329 in the text. It is often an ordinary character or a character class.
330 Braces can be used to make a pattern into an atom. The "\z(\)" construct
331 is only for syntax highlighting.
332
333 atom ::= ordinary-atom |/ordinary-atom|
334 or \( pattern \) |/\(|
335 or \%( pattern \) |/\%(|
336 or \z( pattern \) |/\z(|
337
338
339==============================================================================
3404. Overview of pattern items *pattern-overview*
341
342Overview of multi items. */multi* *E61* *E62*
343More explanation and examples below, follow the links. *E64*
344
345 multi ~
346 'magic' 'nomagic' matches of the preceding atom ~
347|/star| * \* 0 or more as many as possible
348|/\+| \+ \+ 1 or more as many as possible (*)
349|/\=| \= \= 0 or 1 as many as possible (*)
350|/\?| \? \? 0 or 1 as many as possible (*)
351
352|/\{| \{n,m} \{n,m} n to m as many as possible (*)
353 \{n} \{n} n exactly (*)
354 \{n,} \{n,} at least n as many as possible (*)
355 \{,m} \{,m} 0 to m as many as possible (*)
356 \{} \{} 0 or more as many as possible (same as *) (*)
357
358|/\{-| \{-n,m} \{-n,m} n to m as few as possible (*)
359 \{-n} \{-n} n exactly (*)
360 \{-n,} \{-n,} at least n as few as possible (*)
361 \{-,m} \{-,m} 0 to m as few as possible (*)
362 \{-} \{-} 0 or more as few as possible (*)
363
364 *E59*
365|/\@>| \@> \@> 1, like matching a whole pattern (*)
366|/\@=| \@= \@= nothing, requires a match |/zero-width| (*)
367|/\@!| \@! \@! nothing, requires NO match |/zero-width| (*)
368|/\@<=| \@<= \@<= nothing, requires a match behind |/zero-width| (*)
369|/\@<!| \@<! \@<! nothing, requires NO match behind |/zero-width| (*)
370
371(*) {not in Vi}
372
373
374Overview of ordinary atoms. */ordinary-atom*
375More explanation and examples below, follow the links.
376
377 ordinary atom ~
378 magic nomagic matches ~
379|/^| ^ ^ start-of-line (at start of pattern) |/zero-width|
380|/\^| \^ \^ literal '^'
381|/\_^| \_^ \_^ start-of-line (used anywhere) |/zero-width|
382|/$| $ $ end-of-line (at end of pattern) |/zero-width|
383|/\$| \$ \$ literal '$'
384|/\_$| \_$ \_$ end-of-line (used anywhere) |/zero-width|
385|/.| . \. any single character (not an end-of-line)
386|/\_.| \_. \_. any single character or end-of-line
387|/\<| \< \< beginning of a word |/zero-width|
388|/\>| \> \> end of a word |/zero-width|
389|/\zs| \zs \zs anything, sets start of match
390|/\ze| \ze \ze anything, sets end of match
391|/\%^| \%^ \%^ beginning of file |/zero-width| *E71*
392|/\%$| \%$ \%$ end of file |/zero-width|
393|/\%#| \%# \%# cursor position |/zero-width|
394|/\%l| \%23l \%23l in line 23 |/zero-width|
395|/\%c| \%23c \%23c in column 23 |/zero-width|
396|/\%v| \%23v \%23v in virtual column 23 |/zero-width|
397
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000398Character classes {not in Vi}: */character-classes*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399|/\i| \i \i identifier character (see 'isident' option)
400|/\I| \I \I like "\i", but excluding digits
401|/\k| \k \k keyword character (see 'iskeyword' option)
402|/\K| \K \K like "\k", but excluding digits
403|/\f| \f \f file name character (see 'isfname' option)
404|/\F| \F \F like "\f", but excluding digits
405|/\p| \p \p printable character (see 'isprint' option)
406|/\P| \P \P like "\p", but excluding digits
407|/\s| \s \s whitespace character: <Space> and <Tab>
408|/\S| \S \S non-whitespace character; opposite of \s
409|/\d| \d \d digit: [0-9]
410|/\D| \D \D non-digit: [^0-9]
411|/\x| \x \x hex digit: [0-9A-Fa-f]
412|/\X| \X \X non-hex digit: [^0-9A-Fa-f]
413|/\o| \o \o octal digit: [0-7]
414|/\O| \O \O non-octal digit: [^0-7]
415|/\w| \w \w word character: [0-9A-Za-z_]
416|/\W| \W \W non-word character: [^0-9A-Za-z_]
417|/\h| \h \h head of word character: [A-Za-z_]
418|/\H| \H \H non-head of word character: [^A-Za-z_]
419|/\a| \a \a alphabetic character: [A-Za-z]
420|/\A| \A \A non-alphabetic character: [^A-Za-z]
421|/\l| \l \l lowercase character: [a-z]
422|/\L| \L \L non-lowercase character: [^a-z]
423|/\u| \u \u uppercase character: [A-Z]
424|/\U| \U \U non-uppercase character [^A-Z]
425|/\_| \_x \_x where x is any of the characters above: character
426 class with end-of-line included
427(end of character classes)
428
429|/\e| \e \e <Esc>
430|/\t| \t \t <Tab>
431|/\r| \r \r <CR>
432|/\b| \b \b <BS>
433|/\n| \n \n end-of-line
434|/~| ~ \~ last given substitute string
435|/\1| \1 \1 same string as matched by first \(\) {not in Vi}
436|/\2| \2 \2 Like "\1", but uses second \(\)
437 ...
438|/\9| \9 \9 Like "\1", but uses ninth \(\)
439 *E68*
440|/\z1| \z1 \z1 only for syntax highlighting, see |:syn-ext-match|
441 ...
442|/\z1| \z9 \z9 only for syntax highlighting, see |:syn-ext-match|
443
444 x x a character with no special meaning matches itself
445
446|/[]| [] \[] any character specified inside the []
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000447|/\%[]| \%[] \%[] a sequence of optionally matched atoms
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448
449|/\c| \c \c ignore case
450|/\C| \C \C match case
451|/\m| \m \m 'magic' on for the following chars in the pattern
452|/\M| \M \M 'magic' off for the following chars in the pattern
453|/\v| \v \v the following chars in the pattern are "very magic"
454|/\V| \V \V the following chars in the pattern are "very nomagic"
455|/\Z| \Z \Z ignore differences in Unicode "combining characters".
456 Useful when searching voweled Hebrew or Arabic text.
457
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000458|/\%d| \%d \%d match specified decimal character (eg \%d123
459|/\%x| \%x \%x match specified hex character (eg \%x2a)
460|/\%o| \%o \%o match specified octal character (eg \%o040)
461|/\%u| \%u \%u match specified multibyte character (eg \%u20ac)
462|/\%U| \%U \%U match specified large multibyte character (eg
463 \%U12345678)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464
465Example matches ~
466\<\I\i* or
467\<\h\w*
468\<[a-zA-Z_][a-zA-Z0-9_]*
469 An identifier (e.g., in a C program).
470
471\(\.$\|\. \) A period followed by <EOL> or a space.
472
473[.!?][])"']*\($\|[ ]\) A search pattern that finds the end of a sentence,
474 with almost the same definition as the ")" command.
475
476cat\Z Both "cat" and "càt" ("a" followed by 0x0300)
477 Does not match "càt" (character 0x00e0), even
478 though it may look the same.
479
480
481==============================================================================
4823. Magic */magic*
483
484Some characters in the pattern are taken literally. They match with the same
485character in the text. When preceded with a backslash however, these
486characters get a special meaning.
487
488Other characters have a special meaning without a backslash. They need to be
489preceded with a backslash to match literally.
490
491If a character is taken literally or not depends on the 'magic' option and the
492items mentioned next.
493 */\m* */\M*
494Use of "\m" makes the pattern after it be interpreted as if 'magic' is set,
495ignoring the actual value of the 'magic' option.
496Use of "\M" makes the pattern after it be interpreted as if 'nomagic' is used.
497 */\v* */\V*
498Use of "\v" means that in the pattern after it all ASCII characters except
499'0'-'9', 'a'-'z', 'A'-'Z' and '_' have a special meaning. "very magic"
500
501Use of "\V" means that in the pattern after it only the backslash has a
502special meaning. "very nomagic"
503
504Examples:
505after: \v \m \M \V matches ~
506 'magic' 'nomagic'
507 $ $ $ \$ matches end-of-line
508 . . \. \. matches any character
509 * * \* \* any number of the previous atom
510 () \(\) \(\) \(\) grouping into an atom
511 | \| \| \| separating alternatives
512 \a \a \a \a alphabetic character
513 \\ \\ \\ \\ literal backslash
514 \. \. . . literal dot
515 \{ { { { literal '{'
516 a a a a literal 'a'
517
518{only Vim supports \m, \M, \v and \V}
519
520It is recommended to always keep the 'magic' option at the default setting,
521which is 'magic'. This avoids portability problems. To make a pattern immune
522to the 'magic' option being set or not, put "\m" or "\M" at the start of the
523pattern.
524
525
526==============================================================================
5275. Multi items *pattern-multi-items*
528
529An atom can be followed by an indication of how many times the atom can be
530matched and in what way. This is called a multi. See |/multi| for an
531overview.
532
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 */star* */\star* *E56*
534* (use \* when 'magic' is not set)
535 Matches 0 or more of the preceding atom, as many as possible.
536 Example 'nomagic' matches ~
537 a* a\* "", "a", "aa", "aaa", etc.
538 .* \.\* anything, also an empty string, no end-of-line
539 \_.* \_.\* everything up to the end of the buffer
540 \_.*END \_.\*END everything up to and including the last "END"
541 in the buffer
542
543 Exception: When "*" is used at the start of the pattern or just after
544 "^" it matches the star character.
545
546 Be aware that repeating "\_." can match a lot of text and take a long
547 time. For example, "\_.*END" matches all text from the current
548 position to the last occurrence of "END" in the file. Since the "*"
549 will match as many as possible, this first skips over all lines until
550 the end of the file and then tries matching "END", backing up one
551 character at a time.
552
553 */\+* *E57*
554\+ Matches 1 or more of the preceding atom, as many as possible. {not in
555 Vi}
556 Example matches ~
557 ^.\+$ any non-empty line
558 \s\+ white space of at least one character
559
560 */\=*
561\= Matches 0 or 1 of the preceding atom, as many as possible. {not in Vi}
562 Example matches ~
563 foo\= "fo" and "foo"
564
565 */\?*
566\? Just like \=. Cannot be used when searching backwards with the "?"
567 command. {not in Vi}
568
569 */\{* *E58* *E60* *E554*
570\{n,m} Matches n to m of the preceding atom, as many as possible
571\{n} Matches n of the preceding atom
572\{n,} Matches at least n of the preceding atom, as many as possible
573\{,m} Matches 0 to m of the preceding atom, as many as possible
574\{} Matches 0 or more of the preceding atom, as many as possible (like *)
575 */\{-*
576\{-n,m} matches n to m of the preceding atom, as few as possible
577\{-n} matches n of the preceding atom
578\{-n,} matches at least n of the preceding atom, as few as possible
579\{-,m} matches 0 to m of the preceding atom, as few as possible
580\{-} matches 0 or more of the preceding atom, as few as possible
581 {Vi does not have any of these}
582
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000583 n and m are positive decimal numbers or zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
585 If a "-" appears immediately after the "{", then a shortest match
586 first algorithm is used (see example below). In particular, "\{-}" is
587 the same as "*" but uses the shortest match first algorithm. BUT: A
588 match that starts earlier is preferred over a shorter match: "a\{-}b"
589 matches "aaab" in "xaaab".
590
591 Example matches ~
592 ab\{2,3}c "abbc" or "abbbc"
593 a\{5} "aaaaa".
594 ab\{2,}c "abbc", "abbbc", "abbbbc", etc
595 ab\{,3}c "ac", "abc", "abbc" or "abbbc".
596 a[bc]\{3}d "abbbd", "abbcd", "acbcd", "acccd", etc.
597 a\(bc\)\{1,2}d "abcd" or "abcbcd"
598 a[bc]\{-}[cd] "abc" in "abcd"
599 a[bc]*[cd] "abcd" in "abcd"
600
601 The } may optionally be preceded with a backslash: \{n,m\}.
602
603 */\@=*
604\@= Matches the preceding atom with zero width. {not in Vi}
605 Like "(?=pattern)" in Perl.
606 Example matches ~
607 foo\(bar\)\@= "foo" in "foobar"
608 foo\(bar\)\@=foo nothing
609 */zero-width*
610 When using "\@=" (or "^", "$", "\<", "\>") no characters are included
611 in the match. These items are only used to check if a match can be
612 made. This can be tricky, because a match with following items will
613 be done in the same position. The last example above will not match
614 "foobarfoo", because it tries match "foo" in the same position where
615 "bar" matched.
616
617 Note that using "\&" works the same as using "\@=": "foo\&.." is the
618 same as "\(foo\)\@=..". But using "\&" is easier, you don't need the
619 braces.
620
621
622 */\@!*
623\@! Matches with zero width if the preceding atom does NOT match at the
624 current position. |/zero-width| {not in Vi}
625 Like '(?!pattern)" in Perl.
626 Example matches ~
627 foo\(bar\)\@! any "foo" not followed by "bar"
628 a.\{-}p\@! "a", "ap", "app", etc. not followed by a "p"
629 if \(\(then\)\@!.\)*$ "if " not followed by "then"
630
631 Using "\@!" is tricky, because there are many places where a pattern
632 does not match. "a.*p\@!" will match from an "a" to the end of the
633 line, because ".*" can match all characters in the line and the "p"
634 doesn't match at the end of the line. "a.\{-}p\@!" will match any
635 "a", "ap", "aap", etc. that isn't followed by a "p", because the "."
636 can match a "p" and "p\@!" doesn't match after that.
637
638 You can't use "\@!" to look for a non-match before the matching
639 position: "\(foo\)\@!bar" will match "bar" in "foobar", because at the
640 position where "bar" matches, "foo" does not match. To avoid matching
641 "foobar" you could use "\(foo\)\@!...bar", but that doesn't match a
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000642 bar at the start of a line. Use "\(foo\)\@<!bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644 */\@<=*
645\@<= Matches with zero width if the preceding atom matches just before what
646 follows. |/zero-width| {not in Vi}
647 Like '(?<=pattern)" in Perl, but Vim allows non-fixed-width patterns.
648 Example matches ~
649 \(an\_s\+\)\@<=file "file" after "an" and white space or an
650 end-of-line
651 For speed it's often much better to avoid this multi. Try using "\zs"
652 instead |/\zs|. To match the same as the above example:
653 an\_s\+\zsfile
654
655 "\@<=" and "\@<!" check for matches just before what follows.
656 Theoretically these matches could start anywhere before this position.
657 But to limit the time needed, only the line where what follows matches
658 is searched, and one line before that (if there is one). This should
659 be sufficient to match most things and not be too slow.
660 The part of the pattern after "\@<=" and "\@<!" are checked for a
661 match first, thus things like "\1" don't work to reference \(\) inside
662 the preceding atom. It does work the other way around:
663 Example matches ~
664 \1\@<=,\([a-z]\+\) ",abc" in "abc,abc"
665
666 */\@<!*
667\@<! Matches with zero width if the preceding atom does NOT match just
668 before what follows. Thus this matches if there is no position in the
669 current or previous line where the atom matches such that it ends just
670 before what follows. |/zero-width| {not in Vi}
671 Like '(?<!pattern)" in Perl, but Vim allows non-fixed-width patterns.
672 The match with the preceding atom is made to end just before the match
673 with what follows, thus an atom that ends in ".*" will work.
674 Warning: This can be slow (because many positions need to be checked
675 for a match).
676 Example matches ~
677 \(foo\)\@<!bar any "bar" that's not in "foobar"
678 \(\/\/.*\)\@\<!in "in" which is not after "//"
679
680 */\@>*
681\@> Matches the preceding atom like matching a whole pattern. {not in Vi}
682 Like '(?>pattern)" in Perl.
683 Example matches ~
684 \(a*\)\@>a nothing (the "a*" takes all the "a"'s, there can't be
685 another one following)
686
687 This matches the preceding atom as if it was a pattern by itself. If
688 it doesn't match, there is no retry with shorter sub-matches or
689 anything. Observe this difference: "a*b" and "a*ab" both match
690 "aaab", but in the second case the "a*" matches only the first two
691 "a"s. "\(a*\)\@>ab" will not match "aaab", because the "a*" matches
692 the "aaa" (as many "a"s as possible), thus the "ab" can't match.
693
694
695==============================================================================
6966. Ordinary atoms *pattern-atoms*
697
698An ordinary atom can be:
699
700 */^*
701^ At beginning of pattern or after "\|", "\(", "\%(" or "\n": matches
702 start-of-line; at other positions, matches literal '^'. |/zero-width|
703 Example matches ~
704 ^beep( the start of the C function "beep" (probably).
705
706 */\^*
707\^ Matches literal '^'. Can be used at any position in the pattern.
708
709 */\_^*
710\_^ Matches start-of-line. |/zero-width| Can be used at any position in
711 the pattern.
712 Example matches ~
713 \_s*\_^foo white space and blank lines and then "foo" at
714 start-of-line
715
716 */$*
717$ At end of pattern or in front of "\|" or "\)" ("|" or ")" after "\v"):
718 matches end-of-line <EOL>; at other positions, matches literal '$'.
719 |/zero-width|
720
721 */\$*
722\$ Matches literal '$'. Can be used at any position in the pattern.
723
724 */\_$*
725\_$ Matches end-of-line. |/zero-width| Can be used at any position in the
726 pattern. Note that "a\_$b" never matches, since "b" cannot match an
727 end-of-line. Use "a\nb" instead |/\n|.
728 Example matches ~
729 foo\_$\_s* "foo" at end-of-line and following white space and
730 blank lines
731
732. (with 'nomagic': \.) */.* */\.*
733 Matches any single character, but not an end-of-line.
734
735 */\_.*
736\_. Matches any single character or end-of-line.
737 Careful: "\_.*" matches all text to the end of the buffer!
738
739 */\<*
740\< Matches the beginning of a word: The next char is the first char of a
741 word. The 'iskeyword' option specifies what is a word character.
742 |/zero-width|
743
744 */\>*
745\> Matches the end of a word: The previous char is the last char of a
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000746 word. The 'iskeyword' option specifies what is a word character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 |/zero-width|
748
749 */\zs*
750\zs Matches at any position, and sets the start of the match there: The
751 next char is the first char of the whole match. |/zero-width|
752 Example: >
753 /^\s*\zsif
754< matches an "if" at the start of a line, ignoring white space.
755 Can be used multiple times, the last one encountered in a matching
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000756 branch is used. Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 /\(.\{-}\zsFab\)\{3}
758< Finds the third occurrence of "Fab".
759 {not in Vi} {not available when compiled without the +syntax feature}
760 */\ze*
761\ze Matches at any position, and sets the end of the match there: The
762 previous char is the last char of the whole match. |/zero-width|
763 Can be used multiple times, the last one encountered in a matching
764 branch is used.
765 Example: "end\ze\(if\|for\)" matches the "end" in "endif" and
766 "endfor".
767 {not in Vi} {not available when compiled without the +syntax feature}
768
769 */\%^* *start-of-file*
770\%^ Matches start of the file. When matching with a string, matches the
771 start of the string. {not in Vi}
772 For example, to find the first "VIM" in a file: >
773 /\%^\_.\{-}\zsVIM
774<
775 */\%$* *end-of-file*
776\%$ Matches end of the file. When matching with a string, matches the
777 end of the string. {not in Vi}
778 Note that this does NOT find the last "VIM" in a file: >
779 /VIM\_.\{-}\%$
780< It will find the next VIM, because the part after it will always
781 match. This one will find the last "VIM" in the file: >
782 /VIM\ze\(\(VIM\)\@!\_.\)*\%$
783< This uses |/\@!| to ascertain that "VIM" does NOT match in any
784 position after the first "VIM".
785 Searching from the end of the file backwards is easier!
786
787 */\%#* *cursor-position*
788\%# Matches with the cursor position. Only works when matching in a
789 buffer displayed in a window. {not in Vi}
790 WARNING: When the cursor is moved after the pattern was used, the
791 result becomes invalid. Vim doesn't automatically update the matches.
792 This is especially relevant for syntax highlighting and 'hlsearch'.
793 In other words: When the cursor moves the display isn't updated for
794 this change. An update is done for lines which are changed (the whole
795 line is updated) or when using the |CTRL-L| command (the whole screen
796 is updated). Example, to highlight the word under the cursor: >
797 /\k*\%#\k*
798< When 'hlsearch' is set and you move the cursor around and make changes
799 this will clearly show when the match is updated or not.
800
801 */\%l* */\%>l* */\%<l*
802\%23l Matches in a specific line.
803\%<23l Matches above a specific line.
804\%>23l Matches below a specific line.
805 These three can be used to match specific lines in a buffer. The "23"
806 can be any line number. The first line is 1. {not in Vi}
807 WARNING: When inserting or deleting lines Vim does not automatically
808 update the matches. This means Syntax highlighting quickly becomes
809 wrong.
810 Example, to highlight the line where the cursor currently is: >
811 :exe '/\%' . line(".") . 'l.*'
812< When 'hlsearch' is set and you move the cursor around and make changes
813 this will clearly show when the match is updated or not.
814
815 */\%c* */\%>c* */\%<c*
816\%23c Matches in a specific column.
817\%<23c Matches before a specific column.
818\%>23c Matches after a specific column.
819 These three can be used to match specific columns in a buffer or
820 string. The "23" can be any column number. The first column is 1.
821 Actually, the column is the byte number (thus it's not exactly right
822 for multi-byte characters). {not in Vi}
823 WARNING: When inserting or deleting text Vim does not automatically
824 update the matches. This means Syntax highlighting quickly becomes
825 wrong.
826 Example, to highlight the column where the cursor currently is: >
827 :exe '/\%' . col(".") . 'c'
828< When 'hlsearch' is set and you move the cursor around and make changes
829 this will clearly show when the match is updated or not.
830 Example for matching a single byte in column 44: >
831 /\%>43c.\%<46c
832< Note that "\%<46c" matches in column 45 when the "." matches a byte in
833 column 44.
834 */\%v* */\%>v* */\%<v*
835\%23v Matches in a specific virtual column.
836\%<23v Matches before a specific virtual column.
837\%>23v Matches after a specific virtual column.
838 These three can be used to match specific virtual columns in a buffer
839 or string. When not matching with a buffer in a window, the option
840 values of the current window are used (e.g., 'tabstop').
841 The "23" can be any column number. The first column is 1.
842 Note that some virtual column positions will never match, because they
843 are halfway a Tab or other character that occupies more than one
844 screen character. {not in Vi}
845 WARNING: When inserting or deleting text Vim does not automatically
Bram Moolenaarde934d72005-05-22 22:09:40 +0000846 update highlighted matches. This means Syntax highlighting quickly
847 becomes wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 Example, to highlight the all characters after virtual column 72: >
849 /\%>72v.*
850< When 'hlsearch' is set and you move the cursor around and make changes
851 this will clearly show when the match is updated or not.
852 To match the text up to column 17: >
853 /.*\%17v
854< Column 17 is not included, because that's where the "\%17v" matches,
855 and since this is a |/zero-width| match, column 17 isn't included in
856 the match. This does the same: >
857 /.*\%<18v
858<
859
860Character classes: {not in Vi}
861\i identifier character (see 'isident' option) */\i*
862\I like "\i", but excluding digits */\I*
863\k keyword character (see 'iskeyword' option) */\k*
864\K like "\k", but excluding digits */\K*
865\f file name character (see 'isfname' option) */\f*
866\F like "\f", but excluding digits */\F*
867\p printable character (see 'isprint' option) */\p*
868\P like "\p", but excluding digits */\P*
869
870NOTE: the above also work for multi-byte characters. The ones below only
871match ASCII characters, as indicated by the range.
872
873 *whitespace* *white-space*
874\s whitespace character: <Space> and <Tab> */\s*
875\S non-whitespace character; opposite of \s */\S*
876\d digit: [0-9] */\d*
877\D non-digit: [^0-9] */\D*
878\x hex digit: [0-9A-Fa-f] */\x*
879\X non-hex digit: [^0-9A-Fa-f] */\X*
880\o octal digit: [0-7] */\o*
881\O non-octal digit: [^0-7] */\O*
882\w word character: [0-9A-Za-z_] */\w*
883\W non-word character: [^0-9A-Za-z_] */\W*
884\h head of word character: [A-Za-z_] */\h*
885\H non-head of word character: [^A-Za-z_] */\H*
886\a alphabetic character: [A-Za-z] */\a*
887\A non-alphabetic character: [^A-Za-z] */\A*
888\l lowercase character: [a-z] */\l*
889\L non-lowercase character: [^a-z] */\L*
890\u uppercase character: [A-Z] */\u*
891\U non-uppercase character [^A-Z] */\U*
892
893 NOTE: Using the atom is faster than the [] form.
894
895 NOTE: 'ignorecase', "\c" and "\C" are not used by character classes.
896
897 */\_* *E63* */\_i* */\_I* */\_k* */\_K* */\_f* */\_F*
898 */\_p* */\_P* */\_s* */\_S* */\_d* */\_D* */\_x* */\_X*
899 */\_o* */\_O* */\_w* */\_W* */\_h* */\_H* */\_a* */\_A*
900 */\_l* */\_L* */\_u* */\_U*
901\_x Where "x" is any of the characters above: The character class with
902 end-of-line added
903(end of character classes)
904
905\e matches <Esc> */\e*
906\t matches <Tab> */\t*
907\r matches <CR> */\r*
908\b matches <BS> */\b*
909\n matches an end-of-line */\n*
910 When matching in a string instead of buffer text a literal newline
911 character is matched.
912
913~ matches the last given substitute string */~* */\~*
914
915\(\) A pattern enclosed by escaped parentheses. */\(* */\(\)* */\)*
916 E.g., "\(^a\)" matches 'a' at the start of a line. *E51* *E54* *E55*
917
918\1 Matches the same string that was matched by */\1* *E65*
919 the first sub-expression in \( and \). {not in Vi}
920 Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc.
921\2 Like "\1", but uses second sub-expression, */\2*
922 ... */\3*
923\9 Like "\1", but uses ninth sub-expression. */\9*
924 Note: The numbering of groups is done based on which "\(" comes first
925 in the pattern (going left to right), NOT based on what is matched
926 first.
927
928\%(\) A pattern enclosed by escaped parentheses. */\%(\)* */\%(* *E53*
929 Just like \(\), but without counting it as a sub-expression. This
930 allows using more groups and it's a little bit faster.
931 {not in Vi}
932
933x A single character, with no special meaning, matches itself
934
935 */\* */\\*
936\x A backslash followed by a single character, with no special meaning,
937 is reserved for future expansions
938
939[] (with 'nomagic': \[]) */[]* */\[]* */\_[]* */collection*
940\_[]
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000941 A collection. This is a sequence of characters enclosed in brackets.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 It matches any single character in the collection.
943 Example matches ~
944 [xyz] any 'x', 'y' or 'z'
945 [a-zA-Z]$ any alphabetic character at the end of a line
946 \c[a-z]$ same
947
948 With "\_" prepended the collection also includes the end-of-line.
949 The same can be done by including "\n" in the collection. The
950 end-of-line is also matched when the collection starts with "^"! Thus
951 "\_[^ab]" matches the end-of-line and any character but "a" and "b".
952 This makes it Vi compatible: Without the "\_" or "\n" the collection
953 does not match an end-of-line.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000954 *E769*
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000955 When the ']' is not there Vim will not give an error message but
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000956 assume no collection is used. Useful to search for '['. However, you
957 do get E769 for internal searching.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 If the sequence begins with "^", it matches any single character NOT
960 in the collection: "[^xyz]" matches anything but 'x', 'y' and 'z'.
961 - If two characters in the sequence are separated by '-', this is
962 shorthand for the full list of ASCII characters between them. E.g.,
963 "[0-9]" matches any decimal digit.
964 - A character class expression is evaluated to the set of characters
965 belonging to that character class. The following character classes
966 are supported:
967 Name Contents ~
968*[:alnum:]* [:alnum:] letters and digits
969*[:alpha:]* [:alpha:] letters
970*[:blank:]* [:blank:] space and tab characters
971*[:cntrl:]* [:cntrl:] control characters
972*[:digit:]* [:digit:] decimal digits
973*[:graph:]* [:graph:] printable characters excluding space
974*[:lower:]* [:lower:] lowercase letters (all letters when
975 'ignorecase' is used)
976*[:print:]* [:print:] printable characters including space
977*[:punct:]* [:punct:] punctuation characters
978*[:space:]* [:space:] whitespace characters
979*[:upper:]* [:upper:] uppercase letters (all letters when
980 'ignorecase' is used)
981*[:xdigit:]* [:xdigit:] hexadecimal digits
982*[:return:]* [:return:] the <CR> character
983*[:tab:]* [:tab:] the <Tab> character
984*[:escape:]* [:escape:] the <Esc> character
985*[:backspace:]* [:backspace:] the <BS> character
986 The brackets in character class expressions are additional to the
987 brackets delimiting a collection. For example, the following is a
988 plausible pattern for a UNIX filename: "[-./[:alnum:]_~]\+" That is,
989 a list of at least one character, each of which is either '-', '.',
990 '/', alphabetic, numeric, '_' or '~'.
991 These items only work for 8-bit characters.
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000992 */[[=* *[==]*
993 - An equivalence class. This means that characters are matched that
994 have almost the same meaning, e.g., when ignoring accents. The form
995 is:
996 [=a=]
997 Currrently this is only implemented for latin1. Also works for the
998 latin1 characters in utf-8 and latin9.
999 */[[.* *[..]*
1000 - A collation element. This currently simply accepts a single
1001 character in the form:
1002 [.a.]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 */\]*
1004 - To include a literal ']', '^', '-' or '\' in the collection, put a
1005 backslash before it: "[xyz\]]", "[\^xyz]", "[xy\-z]" and "[xyz\\]".
1006 (Note: POSIX does not support the use of a backslash this way). For
1007 ']' you can also make it the first character (following a possible
1008 "^"): "[]xyz]" or "[^]xyz]" {not in Vi}.
1009 For '-' you can also make it the first or last character: "[-xyz]",
1010 "[^-xyz]" or "[xyz-]". For '\' you can also let it be followed by
1011 any character that's not in "^]-\bertn". "[\xyz]" matches '\', 'x',
1012 'y' and 'z'. It's better to use "\\" though, future expansions may
1013 use other characters after '\'.
1014 - The following translations are accepted when the 'l' flag is not
1015 included in 'cpoptions' {not in Vi}:
1016 \e <Esc>
1017 \t <Tab>
1018 \r <CR> (NOT end-of-line!)
1019 \b <BS>
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001020 \d123 decimal number of character
1021 \o40 octal number of character up to 0377
1022 \x20 hexadecimal number of character up to 0xff
1023 \u20AC hex. number of multibyte character up to 0xffff
1024 \U1234 hex. number of multibyte character up to 0xffffffff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 NOTE: The other backslash codes mentioned above do not work inside
1026 []!
1027 - Matching with a collection can be slow, because each character in
1028 the text has to be compared with each character in the collection.
1029 Use one of the other atoms above when possible. Example: "\d" is
1030 much faster than "[0-9]" and matches the same characters.
1031
1032 */\%[]* *E69* *E70* *E369*
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001033\%[] A sequence of optionally matched atoms. This always matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 It matches as much of the list of atoms it contains as possible. Thus
1035 it stops at the first atom that doesn't match. For example: >
1036 /r\%[ead]
1037< matches "r", "re", "rea" or "read". The longest that matches is used.
1038 To match the Ex command "function", where "fu" is required and
1039 "nction" is optional, this would work: >
1040 /\<fu\%[nction]\>
1041< The end-of-word atom "\>" is used to avoid matching "fu" in "full".
1042 It gets more complicated when the atoms are not ordinary characters.
1043 You don't often have to use it, but it is possible. Example: >
1044 /\<r\%[[eo]ad]\>
1045< Matches the words "r", "re", "ro", "rea", "roa", "read" and "road".
1046 {not available when compiled without the +syntax feature}
1047
Bram Moolenaar677ee682005-01-27 14:41:15 +00001048 */\%d* */\%x* */\%o* */\%u* */\%U* *E678*
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001049
1050\%d123 Matches the character specified with a decimal number. Must be
1051 followed by a non-digit.
1052\%o40 Matches the character specified with an octal number up to 0377.
1053 Numbers below 040 must be followed by a non-octal digit or a non-digit.
1054\%x2a Matches the character specified with up to two hexadecimal characters.
1055\%u20AC Matches the character specified with up to four hexadecimal
1056 characters.
1057\%U1234abcd Matches the character specified with up to eight hexadecimal
1058 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
1060==============================================================================
10617. Ignoring case in a pattern */ignorecase*
1062
1063If the 'ignorecase' option is on, the case of normal letters is ignored.
1064'smartcase' can be set to ignore case when the pattern contains lowercase
1065letters only.
1066 */\c* */\C*
1067When "\c" appears anywhere in the pattern, the whole pattern is handled like
1068'ignorecase' is on. The actual value of 'ignorecase' and 'smartcase' is
1069ignored. "\C" does the opposite: Force matching case for the whole pattern.
1070{only Vim supports \c and \C}
1071Note that 'ignorecase', "\c" and "\C" are not used for the character classes.
1072
1073Examples:
1074 pattern 'ignorecase' 'smartcase' matches ~
1075 foo off - foo
1076 foo on - foo Foo FOO
1077 Foo on off foo Foo FOO
1078 Foo on on Foo
1079 \cfoo - - foo Foo FOO
1080 foo\C - - foo
1081
1082 */\Z*
1083When "\Z" appears anywhere in the pattern, composing characters are ignored.
1084Thus only the base characters need to match, the composing characters may be
1085different and the number of composing characters may differ. Only relevant
1086when 'encoding' is "utf-8".
1087
1088Technical detail: *NL-used-for-Nul*
1089<Nul> characters in the file are stored as <NL> in memory. In the display
1090they are shown as "^@". The translation is done when reading and writing
1091files. To match a <Nul> with a search pattern you can just enter CTRL-@ or
1092"CTRL-V 000". This is probably just what you expect. Internally the
1093character is replaced with a <NL> in the search pattern. What is unusual is
1094that typing CTRL-V CTRL-J also inserts a <NL>, thus also searches for a <Nul>
1095in the file. {Vi cannot handle <Nul> characters in the file at all}
1096
1097 *CR-used-for-NL*
1098When 'fileformat' is "mac", <NL> characters in the file are stored as <CR>
1099characters internally. In the display they are shown as "^M". Otherwise this
1100works similar to the usage of <NL> for a <Nul>.
1101
1102When working with expression evaluation, a <NL> character in the pattern
1103matches a <NL> in the string. The use of "\n" (backslash n) to match a <NL>
1104doesn't work there, it only works to match text in the buffer.
1105
1106 *pattern-multi-byte*
1107Patterns will also work with multi-byte characters, mostly as you would
1108expect. But invalid bytes may cause trouble, a pattern with an invalid byte
1109will probably never match.
1110
1111==============================================================================
11128. Compare with Perl patterns *perl-patterns*
1113
1114Vim's regexes are most similar to Perl's, in terms of what you can do. The
1115difference between them is mostly just notation; here's a summary of where
1116they differ:
1117
1118Capability in Vimspeak in Perlspeak ~
1119----------------------------------------------------------------
1120force case insensitivity \c (?i)
1121force case sensitivity \C (?-i)
1122backref-less grouping \%(atom) (?:atom)
1123conservative quantifiers \{-n,m} *?, +?, ??, {}?
11240-width match atom\@= (?=atom)
11250-width non-match atom\@! (?!atom)
11260-width preceding match atom\@<= (?<=atom)
11270-width preceding non-match atom\@<! (?<!atom)
1128match without retry atom\@> (?>atom)
1129
1130Vim and Perl handle newline characters inside a string a bit differently:
1131
1132In Perl, ^ and $ only match at the very beginning and end of the text,
1133by default, but you can set the 'm' flag, which lets them match at
1134embedded newlines as well. You can also set the 's' flag, which causes
1135a . to match newlines as well. (Both these flags can be changed inside
1136a pattern using the same syntax used for the i flag above, BTW.)
1137
1138On the other hand, Vim's ^ and $ always match at embedded newlines, and
1139you get two separate atoms, \%^ and \%$, which only match at the very
1140start and end of the text, respectively. Vim solves the second problem
1141by giving you the \_ "modifier": put it in front of a . or a character
1142class, and they will match newlines as well.
1143
1144Finally, these constructs are unique to Perl:
1145- execution of arbitrary code in the regex: (?{perl code})
1146- conditional expressions: (?(condition)true-expr|false-expr)
1147
1148...and these are unique to Vim:
1149- changing the magic-ness of a pattern: \v \V \m \M
1150 (very useful for avoiding backslashitis)
1151- sequence of optionally matching atoms: \%[atoms]
1152- \& (which is to \| what "and" is to "or"; it forces several branches
1153 to match at one spot)
1154- matching lines/columns by number: \%5l \%5c \%5v
1155- limiting the "return value" of a regex: \zs \ze
1156
1157==============================================================================
11589. Highlighting matches *match-highlight*
1159
1160 *:mat* *:match*
1161:mat[ch] {group} /{pattern}/
1162 Define a pattern to highlight in the current window. It will
1163 be highlighted with {group}. Example: >
1164 :highlight MyGroup ctermbg=green guibg=green
1165 :match MyGroup /TODO/
1166< Instead of // any character can be used to mark the start and
1167 end of the {pattern}. Watch out for using special characters,
1168 such as '"' and '|'.
1169 {group} must exist at the moment this command is executed.
1170 The match overrides the 'hlsearch' highlighting.
1171 'ignorecase' does not apply, use |/\c| in the pattern to
1172 ignore case. Otherwise case is not ignored.
1173 Note that highlighting the last used search pattern with
1174 'hlsearch' is used in all windows, while the pattern defined
1175 with ":match" only exists in the current window. It is kept
1176 when switching to another buffer.
1177 Another example, which highlights all characters in virtual
1178 column 72 and more: >
1179 :highlight rightMargin term=bold ctermfg=blue guifg=blue
1180 :match rightMargin /.\%>72v/
1181< To highlight all character that are in virtual column 7: >
1182 :highlight col8 ctermbg=grey guibg=grey
1183 :match col8 /\%<8v.\%>7v/
1184< Note the use of two items to also match a character that
1185 occupies more than one virtual column, such as a TAB.
1186
1187:mat[ch]
1188:mat[ch] none
1189 Clear a previously defined match pattern.
1190
1191 vim:tw=78:ts=8:ft=help:norl: