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