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