blob: 9930a3b80b6dc5fc1ec026f37f250503492fc4c3 [file] [log] [blame]
Bram Moolenaar251e1912011-06-19 05:09:16 +02001*pattern.txt* For Vim version 7.3. Last change: 2011 May 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"
Bram Moolenaar251e1912011-06-19 05:09:16 +0200638 a.\{-}p\@! "a", "ap", "aap", "app", etc. not immediately
639 followed by a "p"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if \(\(then\)\@!.\)*$ "if " not followed by "then"
641
642 Using "\@!" is tricky, because there are many places where a pattern
643 does not match. "a.*p\@!" will match from an "a" to the end of the
644 line, because ".*" can match all characters in the line and the "p"
645 doesn't match at the end of the line. "a.\{-}p\@!" will match any
646 "a", "ap", "aap", etc. that isn't followed by a "p", because the "."
647 can match a "p" and "p\@!" doesn't match after that.
648
649 You can't use "\@!" to look for a non-match before the matching
650 position: "\(foo\)\@!bar" will match "bar" in "foobar", because at the
651 position where "bar" matches, "foo" does not match. To avoid matching
652 "foobar" you could use "\(foo\)\@!...bar", but that doesn't match a
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000653 bar at the start of a line. Use "\(foo\)\@<!bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar8e5af3e2011-04-28 19:02:44 +0200655 Useful example: to find "foo" in a line that does not contain "bar": >
656 /^\%(.*bar\)\@!.*\zsfoo
657< This pattern first checks that there is not a single position in the
658 line where "bar" matches. If ".*bar" matches somewhere the \@! will
659 reject the pattern. When there is no match any "foo" will be found.
660 The "\zs" is to have the match start just before "foo".
661
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 */\@<=*
663\@<= Matches with zero width if the preceding atom matches just before what
664 follows. |/zero-width| {not in Vi}
665 Like '(?<=pattern)" in Perl, but Vim allows non-fixed-width patterns.
666 Example matches ~
667 \(an\_s\+\)\@<=file "file" after "an" and white space or an
668 end-of-line
669 For speed it's often much better to avoid this multi. Try using "\zs"
670 instead |/\zs|. To match the same as the above example:
671 an\_s\+\zsfile
672
673 "\@<=" and "\@<!" check for matches just before what follows.
674 Theoretically these matches could start anywhere before this position.
675 But to limit the time needed, only the line where what follows matches
676 is searched, and one line before that (if there is one). This should
677 be sufficient to match most things and not be too slow.
678 The part of the pattern after "\@<=" and "\@<!" are checked for a
679 match first, thus things like "\1" don't work to reference \(\) inside
680 the preceding atom. It does work the other way around:
681 Example matches ~
682 \1\@<=,\([a-z]\+\) ",abc" in "abc,abc"
683
684 */\@<!*
685\@<! Matches with zero width if the preceding atom does NOT match just
686 before what follows. Thus this matches if there is no position in the
687 current or previous line where the atom matches such that it ends just
688 before what follows. |/zero-width| {not in Vi}
689 Like '(?<!pattern)" in Perl, but Vim allows non-fixed-width patterns.
690 The match with the preceding atom is made to end just before the match
691 with what follows, thus an atom that ends in ".*" will work.
692 Warning: This can be slow (because many positions need to be checked
693 for a match).
694 Example matches ~
695 \(foo\)\@<!bar any "bar" that's not in "foobar"
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000696 \(\/\/.*\)\@<!in "in" which is not after "//"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698 */\@>*
699\@> Matches the preceding atom like matching a whole pattern. {not in Vi}
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000700 Like "(?>pattern)" in Perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 Example matches ~
702 \(a*\)\@>a nothing (the "a*" takes all the "a"'s, there can't be
703 another one following)
704
705 This matches the preceding atom as if it was a pattern by itself. If
706 it doesn't match, there is no retry with shorter sub-matches or
707 anything. Observe this difference: "a*b" and "a*ab" both match
708 "aaab", but in the second case the "a*" matches only the first two
709 "a"s. "\(a*\)\@>ab" will not match "aaab", because the "a*" matches
710 the "aaa" (as many "a"s as possible), thus the "ab" can't match.
711
712
713==============================================================================
7146. Ordinary atoms *pattern-atoms*
715
716An ordinary atom can be:
717
718 */^*
719^ At beginning of pattern or after "\|", "\(", "\%(" or "\n": matches
720 start-of-line; at other positions, matches literal '^'. |/zero-width|
721 Example matches ~
722 ^beep( the start of the C function "beep" (probably).
723
724 */\^*
725\^ Matches literal '^'. Can be used at any position in the pattern.
726
727 */\_^*
728\_^ Matches start-of-line. |/zero-width| Can be used at any position in
729 the pattern.
730 Example matches ~
731 \_s*\_^foo white space and blank lines and then "foo" at
732 start-of-line
733
734 */$*
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000735$ At end of pattern or in front of "\|", "\)" or "\n" ('magic' on):
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 matches end-of-line <EOL>; at other positions, matches literal '$'.
737 |/zero-width|
738
739 */\$*
740\$ Matches literal '$'. Can be used at any position in the pattern.
741
742 */\_$*
743\_$ Matches end-of-line. |/zero-width| Can be used at any position in the
744 pattern. Note that "a\_$b" never matches, since "b" cannot match an
745 end-of-line. Use "a\nb" instead |/\n|.
746 Example matches ~
747 foo\_$\_s* "foo" at end-of-line and following white space and
748 blank lines
749
750. (with 'nomagic': \.) */.* */\.*
751 Matches any single character, but not an end-of-line.
752
753 */\_.*
754\_. Matches any single character or end-of-line.
755 Careful: "\_.*" matches all text to the end of the buffer!
756
757 */\<*
758\< Matches the beginning of a word: The next char is the first char of a
759 word. The 'iskeyword' option specifies what is a word character.
760 |/zero-width|
761
762 */\>*
763\> Matches the end of a word: The previous char is the last char of a
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000764 word. The 'iskeyword' option specifies what is a word character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 |/zero-width|
766
767 */\zs*
768\zs Matches at any position, and sets the start of the match there: The
769 next char is the first char of the whole match. |/zero-width|
770 Example: >
771 /^\s*\zsif
772< matches an "if" at the start of a line, ignoring white space.
773 Can be used multiple times, the last one encountered in a matching
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000774 branch is used. Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 /\(.\{-}\zsFab\)\{3}
776< Finds the third occurrence of "Fab".
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 */\ze*
779\ze Matches at any position, and sets the end of the match there: The
780 previous char is the last char of the whole match. |/zero-width|
781 Can be used multiple times, the last one encountered in a matching
782 branch is used.
783 Example: "end\ze\(if\|for\)" matches the "end" in "endif" and
784 "endfor".
Bram Moolenaardb84e452010-08-15 13:50:43 +0200785 {not in Vi} {not available when compiled without the |+syntax| feature}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
787 */\%^* *start-of-file*
788\%^ Matches start of the file. When matching with a string, matches the
789 start of the string. {not in Vi}
790 For example, to find the first "VIM" in a file: >
791 /\%^\_.\{-}\zsVIM
792<
793 */\%$* *end-of-file*
794\%$ Matches end of the file. When matching with a string, matches the
795 end of the string. {not in Vi}
796 Note that this does NOT find the last "VIM" in a file: >
797 /VIM\_.\{-}\%$
798< It will find the next VIM, because the part after it will always
799 match. This one will find the last "VIM" in the file: >
800 /VIM\ze\(\(VIM\)\@!\_.\)*\%$
801< This uses |/\@!| to ascertain that "VIM" does NOT match in any
802 position after the first "VIM".
803 Searching from the end of the file backwards is easier!
804
Bram Moolenaar33aec762006-01-22 23:30:12 +0000805 */\%V*
806\%V Match inside the Visual area. When Visual mode has already been
807 stopped match in the area that |gv| would reselect.
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100808 This is a |/zero-width| match. To make sure the whole pattern is
809 inside the Visual area put it at the start and end of the pattern,
810 e.g.: >
811 /\%Vfoo.*bar\%V
812< Only works for the current buffer.
Bram Moolenaar33aec762006-01-22 23:30:12 +0000813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 */\%#* *cursor-position*
815\%# Matches with the cursor position. Only works when matching in a
816 buffer displayed in a window. {not in Vi}
817 WARNING: When the cursor is moved after the pattern was used, the
818 result becomes invalid. Vim doesn't automatically update the matches.
819 This is especially relevant for syntax highlighting and 'hlsearch'.
820 In other words: When the cursor moves the display isn't updated for
821 this change. An update is done for lines which are changed (the whole
822 line is updated) or when using the |CTRL-L| command (the whole screen
823 is updated). Example, to highlight the word under the cursor: >
824 /\k*\%#\k*
825< When 'hlsearch' is set and you move the cursor around and make changes
826 this will clearly show when the match is updated or not.
827
Bram Moolenaar33aec762006-01-22 23:30:12 +0000828 */\%'m* */\%<'m* */\%>'m*
829\%'m Matches with the position of mark m.
830\%<'m Matches before the position of mark m.
831\%>'m Matches after the position of mark m.
832 Example, to highlight the text from mark 's to 'e: >
833 /.\%>'s.*\%<'e..
834< Note that two dots are required to include mark 'e in the match. That
835 is because "\%<'e" matches at the character before the 'e mark, and
836 since it's a |/zero-width| match it doesn't include that character.
837 {not in Vi}
838 WARNING: When the mark is moved after the pattern was used, the result
839 becomes invalid. Vim doesn't automatically update the matches.
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000840 Similar to moving the cursor for "\%#" |/\%#|.
Bram Moolenaar33aec762006-01-22 23:30:12 +0000841
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 */\%l* */\%>l* */\%<l*
843\%23l Matches in a specific line.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000844\%<23l Matches above a specific line (lower line number).
845\%>23l Matches below a specific line (higher line number).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 These three can be used to match specific lines in a buffer. The "23"
847 can be any line number. The first line is 1. {not in Vi}
848 WARNING: When inserting or deleting lines Vim does not automatically
849 update the matches. This means Syntax highlighting quickly becomes
850 wrong.
851 Example, to highlight the line where the cursor currently is: >
852 :exe '/\%' . line(".") . 'l.*'
853< When 'hlsearch' is set and you move the cursor around and make changes
854 this will clearly show when the match is updated or not.
855
856 */\%c* */\%>c* */\%<c*
857\%23c Matches in a specific column.
858\%<23c Matches before a specific column.
859\%>23c Matches after a specific column.
860 These three can be used to match specific columns in a buffer or
861 string. The "23" can be any column number. The first column is 1.
862 Actually, the column is the byte number (thus it's not exactly right
863 for multi-byte characters). {not in Vi}
864 WARNING: When inserting or deleting text Vim does not automatically
865 update the matches. This means Syntax highlighting quickly becomes
866 wrong.
867 Example, to highlight the column where the cursor currently is: >
868 :exe '/\%' . col(".") . 'c'
869< When 'hlsearch' is set and you move the cursor around and make changes
870 this will clearly show when the match is updated or not.
871 Example for matching a single byte in column 44: >
872 /\%>43c.\%<46c
873< Note that "\%<46c" matches in column 45 when the "." matches a byte in
874 column 44.
875 */\%v* */\%>v* */\%<v*
876\%23v Matches in a specific virtual column.
877\%<23v Matches before a specific virtual column.
878\%>23v Matches after a specific virtual column.
879 These three can be used to match specific virtual columns in a buffer
880 or string. When not matching with a buffer in a window, the option
881 values of the current window are used (e.g., 'tabstop').
882 The "23" can be any column number. The first column is 1.
883 Note that some virtual column positions will never match, because they
Bram Moolenaar69c2f172007-05-12 14:57:31 +0000884 are halfway through a tab or other character that occupies more than
885 one screen character. {not in Vi}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 WARNING: When inserting or deleting text Vim does not automatically
Bram Moolenaarde934d72005-05-22 22:09:40 +0000887 update highlighted matches. This means Syntax highlighting quickly
888 becomes wrong.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000889 Example, to highlight all the characters after virtual column 72: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 /\%>72v.*
891< When 'hlsearch' is set and you move the cursor around and make changes
892 this will clearly show when the match is updated or not.
893 To match the text up to column 17: >
894 /.*\%17v
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100895< Column 17 is included, because that's where the "\%17v" matches,
896 even though this is a |/zero-width| match. Adding a dot to match the
897 next character has the same result: >
898 /.*\%17v.
899< This command does the same thing, but also matches when there is no
900 character in column 17: >
901 /.*\%<18v.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902<
903
904Character classes: {not in Vi}
905\i identifier character (see 'isident' option) */\i*
906\I like "\i", but excluding digits */\I*
907\k keyword character (see 'iskeyword' option) */\k*
908\K like "\k", but excluding digits */\K*
909\f file name character (see 'isfname' option) */\f*
910\F like "\f", but excluding digits */\F*
911\p printable character (see 'isprint' option) */\p*
912\P like "\p", but excluding digits */\P*
913
914NOTE: the above also work for multi-byte characters. The ones below only
915match ASCII characters, as indicated by the range.
916
917 *whitespace* *white-space*
918\s whitespace character: <Space> and <Tab> */\s*
919\S non-whitespace character; opposite of \s */\S*
920\d digit: [0-9] */\d*
921\D non-digit: [^0-9] */\D*
922\x hex digit: [0-9A-Fa-f] */\x*
923\X non-hex digit: [^0-9A-Fa-f] */\X*
924\o octal digit: [0-7] */\o*
925\O non-octal digit: [^0-7] */\O*
926\w word character: [0-9A-Za-z_] */\w*
927\W non-word character: [^0-9A-Za-z_] */\W*
928\h head of word character: [A-Za-z_] */\h*
929\H non-head of word character: [^A-Za-z_] */\H*
930\a alphabetic character: [A-Za-z] */\a*
931\A non-alphabetic character: [^A-Za-z] */\A*
932\l lowercase character: [a-z] */\l*
933\L non-lowercase character: [^a-z] */\L*
934\u uppercase character: [A-Z] */\u*
935\U non-uppercase character [^A-Z] */\U*
936
937 NOTE: Using the atom is faster than the [] form.
938
939 NOTE: 'ignorecase', "\c" and "\C" are not used by character classes.
940
941 */\_* *E63* */\_i* */\_I* */\_k* */\_K* */\_f* */\_F*
942 */\_p* */\_P* */\_s* */\_S* */\_d* */\_D* */\_x* */\_X*
943 */\_o* */\_O* */\_w* */\_W* */\_h* */\_H* */\_a* */\_A*
944 */\_l* */\_L* */\_u* */\_U*
945\_x Where "x" is any of the characters above: The character class with
946 end-of-line added
947(end of character classes)
948
949\e matches <Esc> */\e*
950\t matches <Tab> */\t*
951\r matches <CR> */\r*
952\b matches <BS> */\b*
953\n matches an end-of-line */\n*
954 When matching in a string instead of buffer text a literal newline
955 character is matched.
956
957~ matches the last given substitute string */~* */\~*
958
959\(\) A pattern enclosed by escaped parentheses. */\(* */\(\)* */\)*
960 E.g., "\(^a\)" matches 'a' at the start of a line. *E51* *E54* *E55*
961
962\1 Matches the same string that was matched by */\1* *E65*
963 the first sub-expression in \( and \). {not in Vi}
964 Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc.
965\2 Like "\1", but uses second sub-expression, */\2*
966 ... */\3*
967\9 Like "\1", but uses ninth sub-expression. */\9*
968 Note: The numbering of groups is done based on which "\(" comes first
969 in the pattern (going left to right), NOT based on what is matched
970 first.
971
972\%(\) A pattern enclosed by escaped parentheses. */\%(\)* */\%(* *E53*
973 Just like \(\), but without counting it as a sub-expression. This
974 allows using more groups and it's a little bit faster.
975 {not in Vi}
976
977x A single character, with no special meaning, matches itself
978
979 */\* */\\*
980\x A backslash followed by a single character, with no special meaning,
981 is reserved for future expansions
982
983[] (with 'nomagic': \[]) */[]* */\[]* */\_[]* */collection*
984\_[]
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000985 A collection. This is a sequence of characters enclosed in brackets.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 It matches any single character in the collection.
987 Example matches ~
988 [xyz] any 'x', 'y' or 'z'
989 [a-zA-Z]$ any alphabetic character at the end of a line
990 \c[a-z]$ same
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000991 */[\n]*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 With "\_" prepended the collection also includes the end-of-line.
993 The same can be done by including "\n" in the collection. The
994 end-of-line is also matched when the collection starts with "^"! Thus
995 "\_[^ab]" matches the end-of-line and any character but "a" and "b".
996 This makes it Vi compatible: Without the "\_" or "\n" the collection
997 does not match an end-of-line.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000998 *E769*
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000999 When the ']' is not there Vim will not give an error message but
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00001000 assume no collection is used. Useful to search for '['. However, you
1001 do get E769 for internal searching.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 If the sequence begins with "^", it matches any single character NOT
1004 in the collection: "[^xyz]" matches anything but 'x', 'y' and 'z'.
1005 - If two characters in the sequence are separated by '-', this is
1006 shorthand for the full list of ASCII characters between them. E.g.,
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02001007 "[0-9]" matches any decimal digit. Non-ASCII characters can be
1008 used, but the character values must not be more than 256 apart.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 - A character class expression is evaluated to the set of characters
1010 belonging to that character class. The following character classes
1011 are supported:
1012 Name Contents ~
1013*[:alnum:]* [:alnum:] letters and digits
1014*[:alpha:]* [:alpha:] letters
1015*[:blank:]* [:blank:] space and tab characters
1016*[:cntrl:]* [:cntrl:] control characters
1017*[:digit:]* [:digit:] decimal digits
1018*[:graph:]* [:graph:] printable characters excluding space
1019*[:lower:]* [:lower:] lowercase letters (all letters when
1020 'ignorecase' is used)
1021*[:print:]* [:print:] printable characters including space
1022*[:punct:]* [:punct:] punctuation characters
1023*[:space:]* [:space:] whitespace characters
1024*[:upper:]* [:upper:] uppercase letters (all letters when
1025 'ignorecase' is used)
1026*[:xdigit:]* [:xdigit:] hexadecimal digits
1027*[:return:]* [:return:] the <CR> character
1028*[:tab:]* [:tab:] the <Tab> character
1029*[:escape:]* [:escape:] the <Esc> character
1030*[:backspace:]* [:backspace:] the <BS> character
1031 The brackets in character class expressions are additional to the
1032 brackets delimiting a collection. For example, the following is a
1033 plausible pattern for a UNIX filename: "[-./[:alnum:]_~]\+" That is,
1034 a list of at least one character, each of which is either '-', '.',
1035 '/', alphabetic, numeric, '_' or '~'.
1036 These items only work for 8-bit characters.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001037 */[[=* *[==]*
1038 - An equivalence class. This means that characters are matched that
1039 have almost the same meaning, e.g., when ignoring accents. The form
1040 is:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001041 [=a=]
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001042 Currently this is only implemented for latin1. Also works for the
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001043 latin1 characters in utf-8 and latin9.
1044 */[[.* *[..]*
1045 - A collation element. This currently simply accepts a single
1046 character in the form:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001047 [.a.]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 */\]*
1049 - To include a literal ']', '^', '-' or '\' in the collection, put a
1050 backslash before it: "[xyz\]]", "[\^xyz]", "[xy\-z]" and "[xyz\\]".
1051 (Note: POSIX does not support the use of a backslash this way). For
1052 ']' you can also make it the first character (following a possible
1053 "^"): "[]xyz]" or "[^]xyz]" {not in Vi}.
1054 For '-' you can also make it the first or last character: "[-xyz]",
1055 "[^-xyz]" or "[xyz-]". For '\' you can also let it be followed by
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02001056 any character that's not in "^]-\bdertnoUux". "[\xyz]" matches '\',
1057 'x', 'y' and 'z'. It's better to use "\\" though, future expansions
1058 may use other characters after '\'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 - The following translations are accepted when the 'l' flag is not
1060 included in 'cpoptions' {not in Vi}:
1061 \e <Esc>
1062 \t <Tab>
1063 \r <CR> (NOT end-of-line!)
1064 \b <BS>
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00001065 \n line break, see above |/[\n]|
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001066 \d123 decimal number of character
1067 \o40 octal number of character up to 0377
1068 \x20 hexadecimal number of character up to 0xff
1069 \u20AC hex. number of multibyte character up to 0xffff
1070 \U1234 hex. number of multibyte character up to 0xffffffff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 NOTE: The other backslash codes mentioned above do not work inside
1072 []!
1073 - Matching with a collection can be slow, because each character in
1074 the text has to be compared with each character in the collection.
1075 Use one of the other atoms above when possible. Example: "\d" is
1076 much faster than "[0-9]" and matches the same characters.
1077
1078 */\%[]* *E69* *E70* *E369*
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001079\%[] A sequence of optionally matched atoms. This always matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 It matches as much of the list of atoms it contains as possible. Thus
1081 it stops at the first atom that doesn't match. For example: >
1082 /r\%[ead]
1083< matches "r", "re", "rea" or "read". The longest that matches is used.
1084 To match the Ex command "function", where "fu" is required and
1085 "nction" is optional, this would work: >
1086 /\<fu\%[nction]\>
1087< The end-of-word atom "\>" is used to avoid matching "fu" in "full".
1088 It gets more complicated when the atoms are not ordinary characters.
1089 You don't often have to use it, but it is possible. Example: >
1090 /\<r\%[[eo]ad]\>
1091< Matches the words "r", "re", "ro", "rea", "roa", "read" and "road".
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00001092 There can be no \(\), \%(\) or \z(\) items inside the [] and \%[] does
1093 not nest.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00001094 To include a "[" use "[[]" and for "]" use []]", e.g.,: >
1095 /index\%[[[]0[]]]
1096< matches "index" "index[", "index[0" and "index[0]".
Bram Moolenaardb84e452010-08-15 13:50:43 +02001097 {not available when compiled without the |+syntax| feature}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098
Bram Moolenaar677ee682005-01-27 14:41:15 +00001099 */\%d* */\%x* */\%o* */\%u* */\%U* *E678*
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001100
1101\%d123 Matches the character specified with a decimal number. Must be
1102 followed by a non-digit.
1103\%o40 Matches the character specified with an octal number up to 0377.
1104 Numbers below 040 must be followed by a non-octal digit or a non-digit.
1105\%x2a Matches the character specified with up to two hexadecimal characters.
1106\%u20AC Matches the character specified with up to four hexadecimal
1107 characters.
1108\%U1234abcd Matches the character specified with up to eight hexadecimal
1109 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110
1111==============================================================================
11127. Ignoring case in a pattern */ignorecase*
1113
1114If the 'ignorecase' option is on, the case of normal letters is ignored.
1115'smartcase' can be set to ignore case when the pattern contains lowercase
1116letters only.
1117 */\c* */\C*
1118When "\c" appears anywhere in the pattern, the whole pattern is handled like
1119'ignorecase' is on. The actual value of 'ignorecase' and 'smartcase' is
1120ignored. "\C" does the opposite: Force matching case for the whole pattern.
1121{only Vim supports \c and \C}
1122Note that 'ignorecase', "\c" and "\C" are not used for the character classes.
1123
1124Examples:
1125 pattern 'ignorecase' 'smartcase' matches ~
1126 foo off - foo
1127 foo on - foo Foo FOO
1128 Foo on off foo Foo FOO
1129 Foo on on Foo
1130 \cfoo - - foo Foo FOO
1131 foo\C - - foo
1132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133Technical detail: *NL-used-for-Nul*
1134<Nul> characters in the file are stored as <NL> in memory. In the display
1135they are shown as "^@". The translation is done when reading and writing
1136files. To match a <Nul> with a search pattern you can just enter CTRL-@ or
1137"CTRL-V 000". This is probably just what you expect. Internally the
1138character is replaced with a <NL> in the search pattern. What is unusual is
1139that typing CTRL-V CTRL-J also inserts a <NL>, thus also searches for a <Nul>
1140in the file. {Vi cannot handle <Nul> characters in the file at all}
1141
1142 *CR-used-for-NL*
1143When 'fileformat' is "mac", <NL> characters in the file are stored as <CR>
Bram Moolenaare37d50a2008-08-06 17:06:04 +00001144characters internally. In the text they are shown as "^J". Otherwise this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145works similar to the usage of <NL> for a <Nul>.
1146
1147When working with expression evaluation, a <NL> character in the pattern
1148matches a <NL> in the string. The use of "\n" (backslash n) to match a <NL>
1149doesn't work there, it only works to match text in the buffer.
1150
1151 *pattern-multi-byte*
1152Patterns will also work with multi-byte characters, mostly as you would
1153expect. But invalid bytes may cause trouble, a pattern with an invalid byte
1154will probably never match.
1155
1156==============================================================================
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011578. Composing characters *patterns-composing*
1158
1159 */\Z*
1160When "\Z" appears anywhere in the pattern, composing characters are ignored.
1161Thus only the base characters need to match, the composing characters may be
1162different and the number of composing characters may differ. Only relevant
1163when 'encoding' is "utf-8".
1164
1165When a composing character appears at the start of the pattern of after an
1166item that doesn't include the composing character, a match is found at any
1167character that includes this composing character.
1168
1169When using a dot and a composing character, this works the same as the
1170composing character by itself, except that it doesn't matter what comes before
1171this.
1172
1173The order of composing characters matters, even though changing the order
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00001174doesn't change what a character looks like. This may change in the future.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001175
1176==============================================================================
11779. Compare with Perl patterns *perl-patterns*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
1179Vim's regexes are most similar to Perl's, in terms of what you can do. The
1180difference between them is mostly just notation; here's a summary of where
1181they differ:
1182
1183Capability in Vimspeak in Perlspeak ~
1184----------------------------------------------------------------
1185force case insensitivity \c (?i)
1186force case sensitivity \C (?-i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001187backref-less grouping \%(atom\) (?:atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188conservative quantifiers \{-n,m} *?, +?, ??, {}?
11890-width match atom\@= (?=atom)
11900-width non-match atom\@! (?!atom)
11910-width preceding match atom\@<= (?<=atom)
11920-width preceding non-match atom\@<! (?<!atom)
1193match without retry atom\@> (?>atom)
1194
1195Vim and Perl handle newline characters inside a string a bit differently:
1196
1197In Perl, ^ and $ only match at the very beginning and end of the text,
1198by default, but you can set the 'm' flag, which lets them match at
1199embedded newlines as well. You can also set the 's' flag, which causes
1200a . to match newlines as well. (Both these flags can be changed inside
1201a pattern using the same syntax used for the i flag above, BTW.)
1202
1203On the other hand, Vim's ^ and $ always match at embedded newlines, and
1204you get two separate atoms, \%^ and \%$, which only match at the very
1205start and end of the text, respectively. Vim solves the second problem
1206by giving you the \_ "modifier": put it in front of a . or a character
1207class, and they will match newlines as well.
1208
1209Finally, these constructs are unique to Perl:
1210- execution of arbitrary code in the regex: (?{perl code})
1211- conditional expressions: (?(condition)true-expr|false-expr)
1212
1213...and these are unique to Vim:
1214- changing the magic-ness of a pattern: \v \V \m \M
1215 (very useful for avoiding backslashitis)
1216- sequence of optionally matching atoms: \%[atoms]
1217- \& (which is to \| what "and" is to "or"; it forces several branches
1218 to match at one spot)
1219- matching lines/columns by number: \%5l \%5c \%5v
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001220- setting the start and end of the match: \zs \ze
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
1222==============================================================================
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000122310. Highlighting matches *match-highlight*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
1225 *:mat* *:match*
1226:mat[ch] {group} /{pattern}/
1227 Define a pattern to highlight in the current window. It will
1228 be highlighted with {group}. Example: >
1229 :highlight MyGroup ctermbg=green guibg=green
1230 :match MyGroup /TODO/
1231< Instead of // any character can be used to mark the start and
1232 end of the {pattern}. Watch out for using special characters,
1233 such as '"' and '|'.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {group} must exist at the moment this command is executed.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001236
1237 The {group} highlighting still applies when a character is
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001238 to be highlighted for 'hlsearch', as the highlighting for
1239 matches is given higher priority than that of 'hlsearch'.
1240 Syntax highlighting (see 'syntax') is also overruled by
1241 matches.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 Note that highlighting the last used search pattern with
1244 'hlsearch' is used in all windows, while the pattern defined
1245 with ":match" only exists in the current window. It is kept
1246 when switching to another buffer.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001247
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001248 'ignorecase' does not apply, use |/\c| in the pattern to
1249 ignore case. Otherwise case is not ignored.
1250
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00001251 'redrawtime' defines the maximum time searched for pattern
1252 matches.
1253
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00001254 When matching end-of-line and Vim redraws only part of the
1255 display you may get unexpected results. That is because Vim
1256 looks for a match in the line where redrawing starts.
1257
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00001258 Also see |matcharg()| and |getmatches()|. The former returns
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001259 the highlight group and pattern of a previous |:match|
1260 command. The latter returns a list with highlight groups and
1261 patterns defined by both |matchadd()| and |:match|.
1262
1263 Highlighting matches using |:match| are limited to three
1264 matches (aside from |:match|, |:2match| and |:3match|are
1265 available). |matchadd()| does not have this limitation and in
1266 addition makes it possible to prioritize matches.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001267
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 Another example, which highlights all characters in virtual
1269 column 72 and more: >
1270 :highlight rightMargin term=bold ctermfg=blue guifg=blue
1271 :match rightMargin /.\%>72v/
1272< To highlight all character that are in virtual column 7: >
1273 :highlight col8 ctermbg=grey guibg=grey
1274 :match col8 /\%<8v.\%>7v/
1275< Note the use of two items to also match a character that
1276 occupies more than one virtual column, such as a TAB.
1277
1278:mat[ch]
1279:mat[ch] none
1280 Clear a previously defined match pattern.
1281
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001282
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001283:2mat[ch] {group} /{pattern}/ *:2match*
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001284:2mat[ch]
1285:2mat[ch] none
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001286:3mat[ch] {group} /{pattern}/ *:3match*
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001287:3mat[ch]
1288:3mat[ch] none
1289 Just like |:match| above, but set a separate match. Thus
1290 there can be three matches active at the same time. The match
1291 with the lowest number has priority if several match at the
1292 same position.
1293 The ":3match" command is used by the |matchparen| plugin. You
1294 are suggested to use ":match" for manual matching and
1295 ":2match" for another plugin.
1296
1297
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 vim:tw=78:ts=8:ft=help:norl: