blob: 205d2d8bf7b0673b827b5dff57968ff16b4bbbb9 [file] [log] [blame]
Bram Moolenaar34401cc2014-08-29 15:12:19 +02001*pattern.txt* For Vim version 7.4. Last change: 2014 Aug 29
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==============================================================================
Bram Moolenaar15146672011-10-20 22:22:38 +0200241. Search commands *search-commands*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
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
Bram Moolenaar15146672011-10-20 22:22:38 +0200153When no match is found you get the error: *E486* Pattern not found
154Note that for the |:global| command this behaves like a normal message, for Vi
155compatibility. For the |:s| command the "e" flag can be used to avoid the
156error message |:s_flags|.
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 *search-offset* *{offset}*
159These commands search for the specified pattern. With "/" and "?" an
160additional offset may be given. There are two types of offsets: line offsets
161and character offsets. {the character offsets are not in Vi}
162
163The offset gives the cursor position relative to the found match:
164 [num] [num] lines downwards, in column 1
165 +[num] [num] lines downwards, in column 1
166 -[num] [num] lines upwards, in column 1
167 e[+num] [num] characters to the right of the end of the match
168 e[-num] [num] characters to the left of the end of the match
169 s[+num] [num] characters to the right of the start of the match
170 s[-num] [num] characters to the left of the start of the match
171 b[+num] [num] identical to s[+num] above (mnemonic: begin)
172 b[-num] [num] identical to s[-num] above (mnemonic: begin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000173 ;{pattern} perform another search, see |//;|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174
175If a '-' or '+' is given but [num] is omitted, a count of one will be used.
176When including an offset with 'e', the search becomes inclusive (the
177character the cursor lands on is included in operations).
178
179Examples:
180
181pattern cursor position ~
182/test/+1 one line below "test", in column 1
183/test/e on the last t of "test"
184/test/s+2 on the 's' of "test"
185/test/b-3 three characters before "test"
186
187If one of these commands is used after an operator, the characters between
188the cursor position before and after the search is affected. However, if a
189line offset is given, the whole lines between the two cursor positions are
190affected.
191
192An example of how to search for matches with a pattern and change the match
193with another word: >
194 /foo<CR> find "foo"
Bram Moolenaar92dff182014-02-11 19:15:50 +0100195 c//e<CR> change until end of match
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 bar<Esc> type replacement
197 //<CR> go to start of next match
Bram Moolenaar92dff182014-02-11 19:15:50 +0100198 c//e<CR> change until end of match
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 beep<Esc> type another replacement
200 etc.
201<
202 *//;* *E386*
203A very special offset is ';' followed by another search command. For example: >
204
205 /test 1/;/test
206 /test.*/+1;?ing?
207
208The first one first finds the next occurrence of "test 1", and then the first
209occurrence of "test" after that.
210
211This is like executing two search commands after each other, except that:
212- It can be used as a single motion command after an operator.
213- The direction for a following "n" or "N" command comes from the first
214 search command.
215- When an error occurs the cursor is not moved at all.
216
217 *last-pattern*
218The last used pattern and offset are remembered. They can be used to repeat
219the search, possibly in another direction or with another count. Note that
220two patterns are remembered: One for 'normal' search commands and one for the
221substitute command ":s". Each time an empty pattern is given, the previously
Bram Moolenaar662db672011-03-22 14:05:35 +0100222used pattern is used. However, if there is no previous search command, a
223previous substitute pattern is used, if possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
225The 'magic' option sticks with the last used pattern. If you change 'magic',
226this will not change how the last used pattern will be interpreted.
227The 'ignorecase' option does not do this. When 'ignorecase' is changed, it
228will result in the pattern to match other text.
229
230All matches for the last used search pattern will be highlighted if you set
231the 'hlsearch' option.
232
233To clear the last used search pattern: >
234 :let @/ = ""
235This will not set the pattern to an empty string, because that would match
236everywhere. The pattern is really cleared, like when starting Vim.
237
Bram Moolenaar8f999f12005-01-25 22:12:55 +0000238The search usually skips matches that don't move the cursor. Whether the next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239match is found at the next character or after the skipped match depends on the
240'c' flag in 'cpoptions'. See |cpo-c|.
241 with 'c' flag: "/..." advances 1 to 3 characters
242 without 'c' flag: "/..." advances 1 character
243The unpredictability with the 'c' flag is caused by starting the search in the
244first column, skipping matches until one is found past the cursor position.
245
Bram Moolenaar8f999f12005-01-25 22:12:55 +0000246When searching backwards, searching starts at the start of the line, using the
247'c' flag in 'cpoptions' as described above. Then the last match before the
248cursor position is used.
249
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250In Vi the ":tag" command sets the last search pattern when the tag is searched
251for. In Vim this is not done, the previous search pattern is still remembered,
252unless the 't' flag is present in 'cpoptions'. The search pattern is always
253put in the search history.
254
255If the 'wrapscan' option is on (which is the default), searches wrap around
256the end of the buffer. If 'wrapscan' is not set, the backward search stops
257at the beginning and the forward search stops at the end of the buffer. If
258'wrapscan' is set and the pattern was not found the error message "pattern
259not found" is given, and the cursor will not be moved. If 'wrapscan' is not
260set the message becomes "search hit BOTTOM without match" when searching
261forward, or "search hit TOP without match" when searching backward. If
262wrapscan is set and the search wraps around the end of the file the message
263"search hit TOP, continuing at BOTTOM" or "search hit BOTTOM, continuing at
264TOP" is given when searching backwards or forwards respectively. This can be
265switched off by setting the 's' flag in the 'shortmess' option. The highlight
266method 'w' is used for this message (default: standout).
267
268 *search-range*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000269You can limit the search command "/" to a certain range of lines by including
270\%>l items. For example, to match the word "limit" below line 199 and above
271line 300: >
272 /\%>199l\%<300llimit
273Also see |/\%>l|.
274
275Another way is to use the ":substitute" command with the 'c' flag. Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 :.,300s/Pattern//gc
277This command will search from the cursor position until line 300 for
278"Pattern". At the match, you will be asked to type a character. Type 'q' to
279stop at this match, type 'n' to find the next match.
280
281The "*", "#", "g*" and "g#" commands look for a word near the cursor in this
282order, the first one that is found is used:
283- The keyword currently under the cursor.
284- The first keyword to the right of the cursor, in the same line.
285- The WORD currently under the cursor.
286- The first WORD to the right of the cursor, in the same line.
287The keyword may only contain letters and characters in 'iskeyword'.
288The WORD may contain any non-blanks (<Tab>s and/or <Space>s).
289Note that if you type with ten fingers, the characters are easy to remember:
290the "#" is under your left hand middle finger (search to the left and up) and
291the "*" is under your right hand middle finger (search to the right and down).
292(this depends on your keyboard layout though).
293
294==============================================================================
2952. The definition of a pattern *search-pattern* *pattern* *[pattern]*
296 *regular-expression* *regexp* *Pattern*
Bram Moolenaarf1f8bc52005-03-07 23:20:08 +0000297 *E76* *E383* *E476*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298
299For starters, read chapter 27 of the user manual |usr_27.txt|.
300
301 */bar* */\bar* */pattern*
3021. A pattern is one or more branches, separated by "\|". It matches anything
303 that matches one of the branches. Example: "foo\|beep" matches "foo" and
304 matches "beep". If more than one branch matches, the first one is used.
305
306 pattern ::= branch
307 or branch \| branch
308 or branch \| branch \| branch
309 etc.
310
311 */branch* */\&*
3122. A branch is one or more concats, separated by "\&". It matches the last
313 concat, but only if all the preceding concats also match at the same
314 position. Examples:
315 "foobeep\&..." matches "foo" in "foobeep".
316 ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
317
318 branch ::= concat
319 or concat \& concat
320 or concat \& concat \& concat
321 etc.
322
323 */concat*
3243. A concat is one or more pieces, concatenated. It matches a match for the
325 first piece, followed by a match for the second piece, etc. Example:
326 "f[0-9]b", first matches "f", then a digit and then "b".
327
328 concat ::= piece
329 or piece piece
330 or piece piece piece
331 etc.
332
333 */piece*
3344. A piece is an atom, possibly followed by a multi, an indication of how many
335 times the atom can be matched. Example: "a*" matches any sequence of "a"
336 characters: "", "a", "aa", etc. See |/multi|.
337
338 piece ::= atom
339 or atom multi
340
341 */atom*
3425. An atom can be one of a long list of items. Many atoms match one character
343 in the text. It is often an ordinary character or a character class.
344 Braces can be used to make a pattern into an atom. The "\z(\)" construct
345 is only for syntax highlighting.
346
347 atom ::= ordinary-atom |/ordinary-atom|
348 or \( pattern \) |/\(|
349 or \%( pattern \) |/\%(|
350 or \z( pattern \) |/\z(|
351
352
Bram Moolenaar913df812013-07-06 15:44:11 +0200353 */\%#=* *two-engines* *NFA*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200354Vim includes two regexp engines:
3551. An old, backtracking engine that supports everything.
3562. A new, NFA engine that works much faster on some patterns, but does not
357 support everything.
358
359Vim will automatically select the right engine for you. However, if you run
360into a problem or want to specifically select one engine or the other, you can
361prepend one of the following to the pattern:
362
363 \%#=0 Force automatic selection. Only has an effect when
364 'regexpengine' has been set to a non-zero value.
365 \%#=1 Force using the old engine.
366 \%#=2 Force using the NFA engine.
367
368You can also use the 'regexpengine' option to change the default.
369
370 *E864* *E868* *E874* *E875* *E876* *E877* *E878*
371If selecting the NFA engine and it runs into something that is not implemented
372the pattern will not match. This is only useful when debugging Vim.
373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374==============================================================================
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003753. Magic */magic*
376
377Some characters in the pattern are taken literally. They match with the same
378character in the text. When preceded with a backslash however, these
379characters get a special meaning.
380
381Other characters have a special meaning without a backslash. They need to be
382preceded with a backslash to match literally.
383
384If a character is taken literally or not depends on the 'magic' option and the
385items mentioned next.
386 */\m* */\M*
387Use of "\m" makes the pattern after it be interpreted as if 'magic' is set,
388ignoring the actual value of the 'magic' option.
389Use of "\M" makes the pattern after it be interpreted as if 'nomagic' is used.
390 */\v* */\V*
391Use of "\v" means that in the pattern after it all ASCII characters except
392'0'-'9', 'a'-'z', 'A'-'Z' and '_' have a special meaning. "very magic"
393
394Use of "\V" means that in the pattern after it only the backslash has a
395special meaning. "very nomagic"
396
397Examples:
398after: \v \m \M \V matches ~
399 'magic' 'nomagic'
400 $ $ $ \$ matches end-of-line
401 . . \. \. matches any character
402 * * \* \* any number of the previous atom
403 () \(\) \(\) \(\) grouping into an atom
404 | \| \| \| separating alternatives
405 \a \a \a \a alphabetic character
406 \\ \\ \\ \\ literal backslash
407 \. \. . . literal dot
408 \{ { { { literal '{'
409 a a a a literal 'a'
410
411{only Vim supports \m, \M, \v and \V}
412
413It is recommended to always keep the 'magic' option at the default setting,
414which is 'magic'. This avoids portability problems. To make a pattern immune
415to the 'magic' option being set or not, put "\m" or "\M" at the start of the
416pattern.
417
418==============================================================================
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194. Overview of pattern items *pattern-overview*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200420 *E865* *E866* *E867* *E869*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421
422Overview of multi items. */multi* *E61* *E62*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200423More explanation and examples below, follow the links. *E64* *E871*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424
425 multi ~
426 'magic' 'nomagic' matches of the preceding atom ~
427|/star| * \* 0 or more as many as possible
428|/\+| \+ \+ 1 or more as many as possible (*)
429|/\=| \= \= 0 or 1 as many as possible (*)
430|/\?| \? \? 0 or 1 as many as possible (*)
431
432|/\{| \{n,m} \{n,m} n to m as many as possible (*)
433 \{n} \{n} n exactly (*)
434 \{n,} \{n,} at least n as many as possible (*)
435 \{,m} \{,m} 0 to m as many as possible (*)
436 \{} \{} 0 or more as many as possible (same as *) (*)
437
438|/\{-| \{-n,m} \{-n,m} n to m as few as possible (*)
439 \{-n} \{-n} n exactly (*)
440 \{-n,} \{-n,} at least n as few as possible (*)
441 \{-,m} \{-,m} 0 to m as few as possible (*)
442 \{-} \{-} 0 or more as few as possible (*)
443
444 *E59*
445|/\@>| \@> \@> 1, like matching a whole pattern (*)
446|/\@=| \@= \@= nothing, requires a match |/zero-width| (*)
447|/\@!| \@! \@! nothing, requires NO match |/zero-width| (*)
448|/\@<=| \@<= \@<= nothing, requires a match behind |/zero-width| (*)
449|/\@<!| \@<! \@<! nothing, requires NO match behind |/zero-width| (*)
450
451(*) {not in Vi}
452
453
454Overview of ordinary atoms. */ordinary-atom*
455More explanation and examples below, follow the links.
456
457 ordinary atom ~
458 magic nomagic matches ~
459|/^| ^ ^ start-of-line (at start of pattern) |/zero-width|
460|/\^| \^ \^ literal '^'
461|/\_^| \_^ \_^ start-of-line (used anywhere) |/zero-width|
462|/$| $ $ end-of-line (at end of pattern) |/zero-width|
463|/\$| \$ \$ literal '$'
464|/\_$| \_$ \_$ end-of-line (used anywhere) |/zero-width|
465|/.| . \. any single character (not an end-of-line)
466|/\_.| \_. \_. any single character or end-of-line
467|/\<| \< \< beginning of a word |/zero-width|
468|/\>| \> \> end of a word |/zero-width|
469|/\zs| \zs \zs anything, sets start of match
470|/\ze| \ze \ze anything, sets end of match
471|/\%^| \%^ \%^ beginning of file |/zero-width| *E71*
472|/\%$| \%$ \%$ end of file |/zero-width|
Bram Moolenaar33aec762006-01-22 23:30:12 +0000473|/\%V| \%V \%V inside Visual area |/zero-width|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474|/\%#| \%# \%# cursor position |/zero-width|
Bram Moolenaar33aec762006-01-22 23:30:12 +0000475|/\%'m| \%'m \%'m mark m position |/zero-width|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476|/\%l| \%23l \%23l in line 23 |/zero-width|
477|/\%c| \%23c \%23c in column 23 |/zero-width|
478|/\%v| \%23v \%23v in virtual column 23 |/zero-width|
479
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000480Character classes {not in Vi}: */character-classes*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481|/\i| \i \i identifier character (see 'isident' option)
482|/\I| \I \I like "\i", but excluding digits
483|/\k| \k \k keyword character (see 'iskeyword' option)
484|/\K| \K \K like "\k", but excluding digits
485|/\f| \f \f file name character (see 'isfname' option)
486|/\F| \F \F like "\f", but excluding digits
487|/\p| \p \p printable character (see 'isprint' option)
488|/\P| \P \P like "\p", but excluding digits
489|/\s| \s \s whitespace character: <Space> and <Tab>
490|/\S| \S \S non-whitespace character; opposite of \s
491|/\d| \d \d digit: [0-9]
492|/\D| \D \D non-digit: [^0-9]
493|/\x| \x \x hex digit: [0-9A-Fa-f]
494|/\X| \X \X non-hex digit: [^0-9A-Fa-f]
495|/\o| \o \o octal digit: [0-7]
496|/\O| \O \O non-octal digit: [^0-7]
497|/\w| \w \w word character: [0-9A-Za-z_]
498|/\W| \W \W non-word character: [^0-9A-Za-z_]
499|/\h| \h \h head of word character: [A-Za-z_]
500|/\H| \H \H non-head of word character: [^A-Za-z_]
501|/\a| \a \a alphabetic character: [A-Za-z]
502|/\A| \A \A non-alphabetic character: [^A-Za-z]
503|/\l| \l \l lowercase character: [a-z]
504|/\L| \L \L non-lowercase character: [^a-z]
505|/\u| \u \u uppercase character: [A-Z]
506|/\U| \U \U non-uppercase character [^A-Z]
507|/\_| \_x \_x where x is any of the characters above: character
508 class with end-of-line included
509(end of character classes)
510
511|/\e| \e \e <Esc>
512|/\t| \t \t <Tab>
513|/\r| \r \r <CR>
514|/\b| \b \b <BS>
515|/\n| \n \n end-of-line
516|/~| ~ \~ last given substitute string
517|/\1| \1 \1 same string as matched by first \(\) {not in Vi}
518|/\2| \2 \2 Like "\1", but uses second \(\)
519 ...
520|/\9| \9 \9 Like "\1", but uses ninth \(\)
521 *E68*
522|/\z1| \z1 \z1 only for syntax highlighting, see |:syn-ext-match|
523 ...
524|/\z1| \z9 \z9 only for syntax highlighting, see |:syn-ext-match|
525
526 x x a character with no special meaning matches itself
527
528|/[]| [] \[] any character specified inside the []
Bram Moolenaar00154502013-02-13 16:15:55 +0100529|/\%[]| \%[] \%[] a sequence of optionally matched atoms
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000531|/\c| \c \c ignore case, do not use the 'ignorecase' option
532|/\C| \C \C match case, do not use the 'ignorecase' option
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200533|/\Z| \Z \Z ignore differences in Unicode "combining characters".
534 Useful when searching voweled Hebrew or Arabic text.
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536|/\m| \m \m 'magic' on for the following chars in the pattern
537|/\M| \M \M 'magic' off for the following chars in the pattern
538|/\v| \v \v the following chars in the pattern are "very magic"
539|/\V| \V \V the following chars in the pattern are "very nomagic"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200540|/\%#=| \%#=1 \%#=1 select regexp engine |/zero-width|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100542|/\%d| \%d \%d match specified decimal character (eg \%d123)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000543|/\%x| \%x \%x match specified hex character (eg \%x2a)
544|/\%o| \%o \%o match specified octal character (eg \%o040)
545|/\%u| \%u \%u match specified multibyte character (eg \%u20ac)
546|/\%U| \%U \%U match specified large multibyte character (eg
547 \%U12345678)
Bram Moolenaar8df5acf2014-05-13 19:37:29 +0200548|/\%C| \%C \%C match any composing characters
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
550Example matches ~
551\<\I\i* or
552\<\h\w*
553\<[a-zA-Z_][a-zA-Z0-9_]*
554 An identifier (e.g., in a C program).
555
556\(\.$\|\. \) A period followed by <EOL> or a space.
557
558[.!?][])"']*\($\|[ ]\) A search pattern that finds the end of a sentence,
559 with almost the same definition as the ")" command.
560
561cat\Z Both "cat" and "càt" ("a" followed by 0x0300)
562 Does not match "càt" (character 0x00e0), even
563 though it may look the same.
564
565
566==============================================================================
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675. Multi items *pattern-multi-items*
568
569An atom can be followed by an indication of how many times the atom can be
570matched and in what way. This is called a multi. See |/multi| for an
571overview.
572
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 */star* */\star* *E56*
574* (use \* when 'magic' is not set)
575 Matches 0 or more of the preceding atom, as many as possible.
576 Example 'nomagic' matches ~
577 a* a\* "", "a", "aa", "aaa", etc.
578 .* \.\* anything, also an empty string, no end-of-line
579 \_.* \_.\* everything up to the end of the buffer
580 \_.*END \_.\*END everything up to and including the last "END"
581 in the buffer
582
583 Exception: When "*" is used at the start of the pattern or just after
584 "^" it matches the star character.
585
586 Be aware that repeating "\_." can match a lot of text and take a long
587 time. For example, "\_.*END" matches all text from the current
588 position to the last occurrence of "END" in the file. Since the "*"
589 will match as many as possible, this first skips over all lines until
590 the end of the file and then tries matching "END", backing up one
591 character at a time.
592
593 */\+* *E57*
594\+ Matches 1 or more of the preceding atom, as many as possible. {not in
595 Vi}
596 Example matches ~
597 ^.\+$ any non-empty line
598 \s\+ white space of at least one character
599
600 */\=*
601\= Matches 0 or 1 of the preceding atom, as many as possible. {not in Vi}
602 Example matches ~
603 foo\= "fo" and "foo"
604
605 */\?*
606\? Just like \=. Cannot be used when searching backwards with the "?"
607 command. {not in Vi}
608
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200609 */\{* *E58* *E60* *E554* *E870*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610\{n,m} Matches n to m of the preceding atom, as many as possible
611\{n} Matches n of the preceding atom
612\{n,} Matches at least n of the preceding atom, as many as possible
613\{,m} Matches 0 to m of the preceding atom, as many as possible
614\{} Matches 0 or more of the preceding atom, as many as possible (like *)
615 */\{-*
616\{-n,m} matches n to m of the preceding atom, as few as possible
617\{-n} matches n of the preceding atom
618\{-n,} matches at least n of the preceding atom, as few as possible
619\{-,m} matches 0 to m of the preceding atom, as few as possible
620\{-} matches 0 or more of the preceding atom, as few as possible
621 {Vi does not have any of these}
622
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000623 n and m are positive decimal numbers or zero
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000624 *non-greedy*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 If a "-" appears immediately after the "{", then a shortest match
626 first algorithm is used (see example below). In particular, "\{-}" is
627 the same as "*" but uses the shortest match first algorithm. BUT: A
628 match that starts earlier is preferred over a shorter match: "a\{-}b"
629 matches "aaab" in "xaaab".
630
631 Example matches ~
632 ab\{2,3}c "abbc" or "abbbc"
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000633 a\{5} "aaaaa"
634 ab\{2,}c "abbc", "abbbc", "abbbbc", etc.
635 ab\{,3}c "ac", "abc", "abbc" or "abbbc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 a[bc]\{3}d "abbbd", "abbcd", "acbcd", "acccd", etc.
637 a\(bc\)\{1,2}d "abcd" or "abcbcd"
638 a[bc]\{-}[cd] "abc" in "abcd"
639 a[bc]*[cd] "abcd" in "abcd"
640
641 The } may optionally be preceded with a backslash: \{n,m\}.
642
643 */\@=*
644\@= Matches the preceding atom with zero width. {not in Vi}
645 Like "(?=pattern)" in Perl.
646 Example matches ~
647 foo\(bar\)\@= "foo" in "foobar"
648 foo\(bar\)\@=foo nothing
649 */zero-width*
650 When using "\@=" (or "^", "$", "\<", "\>") no characters are included
651 in the match. These items are only used to check if a match can be
652 made. This can be tricky, because a match with following items will
653 be done in the same position. The last example above will not match
654 "foobarfoo", because it tries match "foo" in the same position where
655 "bar" matched.
656
657 Note that using "\&" works the same as using "\@=": "foo\&.." is the
658 same as "\(foo\)\@=..". But using "\&" is easier, you don't need the
659 braces.
660
661
662 */\@!*
663\@! Matches with zero width if the preceding atom does NOT match at the
664 current position. |/zero-width| {not in Vi}
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200665 Like "(?!pattern)" in Perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 Example matches ~
667 foo\(bar\)\@! any "foo" not followed by "bar"
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200668 a.\{-}p\@! "a", "ap", "app", "appp", etc. not immediately
Bram Moolenaar251e1912011-06-19 05:09:16 +0200669 followed by a "p"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 if \(\(then\)\@!.\)*$ "if " not followed by "then"
671
672 Using "\@!" is tricky, because there are many places where a pattern
673 does not match. "a.*p\@!" will match from an "a" to the end of the
674 line, because ".*" can match all characters in the line and the "p"
675 doesn't match at the end of the line. "a.\{-}p\@!" will match any
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200676 "a", "ap", "app", etc. that isn't followed by a "p", because the "."
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 can match a "p" and "p\@!" doesn't match after that.
678
679 You can't use "\@!" to look for a non-match before the matching
680 position: "\(foo\)\@!bar" will match "bar" in "foobar", because at the
681 position where "bar" matches, "foo" does not match. To avoid matching
682 "foobar" you could use "\(foo\)\@!...bar", but that doesn't match a
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000683 bar at the start of a line. Use "\(foo\)\@<!bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
Bram Moolenaar8e5af3e2011-04-28 19:02:44 +0200685 Useful example: to find "foo" in a line that does not contain "bar": >
686 /^\%(.*bar\)\@!.*\zsfoo
687< This pattern first checks that there is not a single position in the
688 line where "bar" matches. If ".*bar" matches somewhere the \@! will
689 reject the pattern. When there is no match any "foo" will be found.
690 The "\zs" is to have the match start just before "foo".
691
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 */\@<=*
693\@<= Matches with zero width if the preceding atom matches just before what
694 follows. |/zero-width| {not in Vi}
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200695 Like "(?<=pattern)" in Perl, but Vim allows non-fixed-width patterns.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 Example matches ~
697 \(an\_s\+\)\@<=file "file" after "an" and white space or an
698 end-of-line
699 For speed it's often much better to avoid this multi. Try using "\zs"
700 instead |/\zs|. To match the same as the above example:
701 an\_s\+\zsfile
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200702 At least set a limit for the look-behind, see below.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
704 "\@<=" and "\@<!" check for matches just before what follows.
705 Theoretically these matches could start anywhere before this position.
706 But to limit the time needed, only the line where what follows matches
707 is searched, and one line before that (if there is one). This should
708 be sufficient to match most things and not be too slow.
Bram Moolenaarfb539272014-08-22 19:21:47 +0200709
710 In the old regexp engine the part of the pattern after "\@<=" and
711 "\@<!" are checked for a match first, thus things like "\1" don't work
712 to reference \(\) inside the preceding atom. It does work the other
713 way around:
714 Bad example matches ~
715 \%#=1\1\@<=,\([a-z]\+\) ",abc" in "abc,abc"
716
717 However, the new regexp engine works differently, it is better to not
718 rely on this behavior, do not use \@<= if it can be avoided:
719 Example matches ~
720 \([a-z]\+\)\zs,\1 ",abc" in "abc,abc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200722\@123<=
723 Like "\@<=" but only look back 123 bytes. This avoids trying lots
724 of matches that are known to fail and make executing the pattern very
725 slow. Example, check if there is a "<" just before "span":
726 /<\@1<=span
727 This will try matching "<" only one byte before "span", which is the
728 only place that works anyway.
729 After crossing a line boundary, the limit is relative to the end of
730 the line. Thus the characters at the start of the line with the match
731 are not counted (this is just to keep it simple).
732 The number zero is the same as no limit.
733
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 */\@<!*
735\@<! Matches with zero width if the preceding atom does NOT match just
736 before what follows. Thus this matches if there is no position in the
737 current or previous line where the atom matches such that it ends just
738 before what follows. |/zero-width| {not in Vi}
Bram Moolenaar1aeaf8c2012-05-18 13:46:39 +0200739 Like "(?<!pattern)" in Perl, but Vim allows non-fixed-width patterns.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 The match with the preceding atom is made to end just before the match
741 with what follows, thus an atom that ends in ".*" will work.
742 Warning: This can be slow (because many positions need to be checked
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200743 for a match). Use a limit if you can, see below.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 Example matches ~
745 \(foo\)\@<!bar any "bar" that's not in "foobar"
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000746 \(\/\/.*\)\@<!in "in" which is not after "//"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
Bram Moolenaar543b7ef2013-06-01 14:50:56 +0200748\@123<!
749 Like "\@<!" but only look back 123 bytes. This avoids trying lots of
750 matches that are known to fail and make executing the pattern very
751 slow.
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 */\@>*
754\@> Matches the preceding atom like matching a whole pattern. {not in Vi}
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000755 Like "(?>pattern)" in Perl.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 Example matches ~
757 \(a*\)\@>a nothing (the "a*" takes all the "a"'s, there can't be
758 another one following)
759
760 This matches the preceding atom as if it was a pattern by itself. If
761 it doesn't match, there is no retry with shorter sub-matches or
762 anything. Observe this difference: "a*b" and "a*ab" both match
763 "aaab", but in the second case the "a*" matches only the first two
764 "a"s. "\(a*\)\@>ab" will not match "aaab", because the "a*" matches
765 the "aaa" (as many "a"s as possible), thus the "ab" can't match.
766
767
768==============================================================================
7696. Ordinary atoms *pattern-atoms*
770
771An ordinary atom can be:
772
773 */^*
774^ At beginning of pattern or after "\|", "\(", "\%(" or "\n": matches
775 start-of-line; at other positions, matches literal '^'. |/zero-width|
776 Example matches ~
777 ^beep( the start of the C function "beep" (probably).
778
779 */\^*
780\^ Matches literal '^'. Can be used at any position in the pattern.
781
782 */\_^*
783\_^ Matches start-of-line. |/zero-width| Can be used at any position in
784 the pattern.
785 Example matches ~
786 \_s*\_^foo white space and blank lines and then "foo" at
787 start-of-line
788
789 */$*
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000790$ At end of pattern or in front of "\|", "\)" or "\n" ('magic' on):
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 matches end-of-line <EOL>; at other positions, matches literal '$'.
792 |/zero-width|
793
794 */\$*
795\$ Matches literal '$'. Can be used at any position in the pattern.
796
797 */\_$*
798\_$ Matches end-of-line. |/zero-width| Can be used at any position in the
799 pattern. Note that "a\_$b" never matches, since "b" cannot match an
800 end-of-line. Use "a\nb" instead |/\n|.
801 Example matches ~
802 foo\_$\_s* "foo" at end-of-line and following white space and
803 blank lines
804
805. (with 'nomagic': \.) */.* */\.*
806 Matches any single character, but not an end-of-line.
807
808 */\_.*
809\_. Matches any single character or end-of-line.
810 Careful: "\_.*" matches all text to the end of the buffer!
811
812 */\<*
813\< Matches the beginning of a word: The next char is the first char of a
814 word. The 'iskeyword' option specifies what is a word character.
815 |/zero-width|
816
817 */\>*
818\> Matches the end of a word: The previous char is the last char of a
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000819 word. The 'iskeyword' option specifies what is a word character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 |/zero-width|
821
822 */\zs*
823\zs Matches at any position, and sets the start of the match there: The
824 next char is the first char of the whole match. |/zero-width|
825 Example: >
826 /^\s*\zsif
827< matches an "if" at the start of a line, ignoring white space.
828 Can be used multiple times, the last one encountered in a matching
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000829 branch is used. Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 /\(.\{-}\zsFab\)\{3}
831< Finds the third occurrence of "Fab".
Bram Moolenaar34401cc2014-08-29 15:12:19 +0200832 This cannot be followed by a multi. *E888*
Bram Moolenaardb84e452010-08-15 13:50:43 +0200833 {not in Vi} {not available when compiled without the |+syntax| feature}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 */\ze*
835\ze Matches at any position, and sets the end of the match there: The
836 previous char is the last char of the whole match. |/zero-width|
837 Can be used multiple times, the last one encountered in a matching
838 branch is used.
839 Example: "end\ze\(if\|for\)" matches the "end" in "endif" and
840 "endfor".
Bram Moolenaar34401cc2014-08-29 15:12:19 +0200841 This cannot be followed by a multi. *E888*
Bram Moolenaardb84e452010-08-15 13:50:43 +0200842 {not in Vi} {not available when compiled without the |+syntax| feature}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
844 */\%^* *start-of-file*
845\%^ Matches start of the file. When matching with a string, matches the
846 start of the string. {not in Vi}
847 For example, to find the first "VIM" in a file: >
848 /\%^\_.\{-}\zsVIM
849<
850 */\%$* *end-of-file*
851\%$ Matches end of the file. When matching with a string, matches the
852 end of the string. {not in Vi}
853 Note that this does NOT find the last "VIM" in a file: >
854 /VIM\_.\{-}\%$
855< It will find the next VIM, because the part after it will always
856 match. This one will find the last "VIM" in the file: >
857 /VIM\ze\(\(VIM\)\@!\_.\)*\%$
858< This uses |/\@!| to ascertain that "VIM" does NOT match in any
859 position after the first "VIM".
860 Searching from the end of the file backwards is easier!
861
Bram Moolenaar33aec762006-01-22 23:30:12 +0000862 */\%V*
863\%V Match inside the Visual area. When Visual mode has already been
864 stopped match in the area that |gv| would reselect.
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100865 This is a |/zero-width| match. To make sure the whole pattern is
866 inside the Visual area put it at the start and end of the pattern,
867 e.g.: >
868 /\%Vfoo.*bar\%V
869< Only works for the current buffer.
Bram Moolenaar33aec762006-01-22 23:30:12 +0000870
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 */\%#* *cursor-position*
872\%# Matches with the cursor position. Only works when matching in a
873 buffer displayed in a window. {not in Vi}
874 WARNING: When the cursor is moved after the pattern was used, the
875 result becomes invalid. Vim doesn't automatically update the matches.
876 This is especially relevant for syntax highlighting and 'hlsearch'.
877 In other words: When the cursor moves the display isn't updated for
878 this change. An update is done for lines which are changed (the whole
879 line is updated) or when using the |CTRL-L| command (the whole screen
880 is updated). Example, to highlight the word under the cursor: >
881 /\k*\%#\k*
882< When 'hlsearch' is set and you move the cursor around and make changes
883 this will clearly show when the match is updated or not.
884
Bram Moolenaar33aec762006-01-22 23:30:12 +0000885 */\%'m* */\%<'m* */\%>'m*
886\%'m Matches with the position of mark m.
887\%<'m Matches before the position of mark m.
888\%>'m Matches after the position of mark m.
889 Example, to highlight the text from mark 's to 'e: >
890 /.\%>'s.*\%<'e..
891< Note that two dots are required to include mark 'e in the match. That
892 is because "\%<'e" matches at the character before the 'e mark, and
893 since it's a |/zero-width| match it doesn't include that character.
894 {not in Vi}
895 WARNING: When the mark is moved after the pattern was used, the result
896 becomes invalid. Vim doesn't automatically update the matches.
Bram Moolenaar1ef15e32006-02-01 21:56:25 +0000897 Similar to moving the cursor for "\%#" |/\%#|.
Bram Moolenaar33aec762006-01-22 23:30:12 +0000898
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 */\%l* */\%>l* */\%<l*
900\%23l Matches in a specific line.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000901\%<23l Matches above a specific line (lower line number).
902\%>23l Matches below a specific line (higher line number).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 These three can be used to match specific lines in a buffer. The "23"
904 can be any line number. The first line is 1. {not in Vi}
905 WARNING: When inserting or deleting lines Vim does not automatically
906 update the matches. This means Syntax highlighting quickly becomes
907 wrong.
908 Example, to highlight the line where the cursor currently is: >
909 :exe '/\%' . line(".") . 'l.*'
910< When 'hlsearch' is set and you move the cursor around and make changes
911 this will clearly show when the match is updated or not.
912
913 */\%c* */\%>c* */\%<c*
914\%23c Matches in a specific column.
915\%<23c Matches before a specific column.
916\%>23c Matches after a specific column.
917 These three can be used to match specific columns in a buffer or
918 string. The "23" can be any column number. The first column is 1.
919 Actually, the column is the byte number (thus it's not exactly right
920 for multi-byte characters). {not in Vi}
921 WARNING: When inserting or deleting text Vim does not automatically
922 update the matches. This means Syntax highlighting quickly becomes
923 wrong.
924 Example, to highlight the column where the cursor currently is: >
925 :exe '/\%' . col(".") . 'c'
926< When 'hlsearch' is set and you move the cursor around and make changes
927 this will clearly show when the match is updated or not.
928 Example for matching a single byte in column 44: >
929 /\%>43c.\%<46c
930< Note that "\%<46c" matches in column 45 when the "." matches a byte in
931 column 44.
932 */\%v* */\%>v* */\%<v*
933\%23v Matches in a specific virtual column.
934\%<23v Matches before a specific virtual column.
935\%>23v Matches after a specific virtual column.
936 These three can be used to match specific virtual columns in a buffer
937 or string. When not matching with a buffer in a window, the option
938 values of the current window are used (e.g., 'tabstop').
939 The "23" can be any column number. The first column is 1.
940 Note that some virtual column positions will never match, because they
Bram Moolenaar69c2f172007-05-12 14:57:31 +0000941 are halfway through a tab or other character that occupies more than
942 one screen character. {not in Vi}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 WARNING: When inserting or deleting text Vim does not automatically
Bram Moolenaarde934d72005-05-22 22:09:40 +0000944 update highlighted matches. This means Syntax highlighting quickly
945 becomes wrong.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +0000946 Example, to highlight all the characters after virtual column 72: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 /\%>72v.*
948< When 'hlsearch' is set and you move the cursor around and make changes
949 this will clearly show when the match is updated or not.
950 To match the text up to column 17: >
951 /.*\%17v
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100952< Column 17 is included, because that's where the "\%17v" matches,
953 even though this is a |/zero-width| match. Adding a dot to match the
954 next character has the same result: >
955 /.*\%17v.
956< This command does the same thing, but also matches when there is no
957 character in column 17: >
958 /.*\%<18v.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959<
960
961Character classes: {not in Vi}
962\i identifier character (see 'isident' option) */\i*
963\I like "\i", but excluding digits */\I*
964\k keyword character (see 'iskeyword' option) */\k*
965\K like "\k", but excluding digits */\K*
966\f file name character (see 'isfname' option) */\f*
967\F like "\f", but excluding digits */\F*
968\p printable character (see 'isprint' option) */\p*
969\P like "\p", but excluding digits */\P*
970
971NOTE: the above also work for multi-byte characters. The ones below only
972match ASCII characters, as indicated by the range.
973
974 *whitespace* *white-space*
975\s whitespace character: <Space> and <Tab> */\s*
976\S non-whitespace character; opposite of \s */\S*
977\d digit: [0-9] */\d*
978\D non-digit: [^0-9] */\D*
979\x hex digit: [0-9A-Fa-f] */\x*
980\X non-hex digit: [^0-9A-Fa-f] */\X*
981\o octal digit: [0-7] */\o*
982\O non-octal digit: [^0-7] */\O*
983\w word character: [0-9A-Za-z_] */\w*
984\W non-word character: [^0-9A-Za-z_] */\W*
985\h head of word character: [A-Za-z_] */\h*
986\H non-head of word character: [^A-Za-z_] */\H*
987\a alphabetic character: [A-Za-z] */\a*
988\A non-alphabetic character: [^A-Za-z] */\A*
989\l lowercase character: [a-z] */\l*
990\L non-lowercase character: [^a-z] */\L*
991\u uppercase character: [A-Z] */\u*
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100992\U non-uppercase character: [^A-Z] */\U*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 NOTE: Using the atom is faster than the [] form.
995
996 NOTE: 'ignorecase', "\c" and "\C" are not used by character classes.
997
998 */\_* *E63* */\_i* */\_I* */\_k* */\_K* */\_f* */\_F*
999 */\_p* */\_P* */\_s* */\_S* */\_d* */\_D* */\_x* */\_X*
1000 */\_o* */\_O* */\_w* */\_W* */\_h* */\_H* */\_a* */\_A*
1001 */\_l* */\_L* */\_u* */\_U*
1002\_x Where "x" is any of the characters above: The character class with
1003 end-of-line added
1004(end of character classes)
1005
1006\e matches <Esc> */\e*
1007\t matches <Tab> */\t*
1008\r matches <CR> */\r*
1009\b matches <BS> */\b*
1010\n matches an end-of-line */\n*
1011 When matching in a string instead of buffer text a literal newline
1012 character is matched.
1013
1014~ matches the last given substitute string */~* */\~*
1015
1016\(\) A pattern enclosed by escaped parentheses. */\(* */\(\)* */\)*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001017 E.g., "\(^a\)" matches 'a' at the start of a line.
1018 *E51* *E54* *E55* *E872* *E873*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
1020\1 Matches the same string that was matched by */\1* *E65*
1021 the first sub-expression in \( and \). {not in Vi}
1022 Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc.
1023\2 Like "\1", but uses second sub-expression, */\2*
1024 ... */\3*
1025\9 Like "\1", but uses ninth sub-expression. */\9*
1026 Note: The numbering of groups is done based on which "\(" comes first
1027 in the pattern (going left to right), NOT based on what is matched
1028 first.
1029
1030\%(\) A pattern enclosed by escaped parentheses. */\%(\)* */\%(* *E53*
1031 Just like \(\), but without counting it as a sub-expression. This
1032 allows using more groups and it's a little bit faster.
1033 {not in Vi}
1034
1035x A single character, with no special meaning, matches itself
1036
1037 */\* */\\*
1038\x A backslash followed by a single character, with no special meaning,
1039 is reserved for future expansions
1040
1041[] (with 'nomagic': \[]) */[]* */\[]* */\_[]* */collection*
1042\_[]
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00001043 A collection. This is a sequence of characters enclosed in brackets.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 It matches any single character in the collection.
1045 Example matches ~
1046 [xyz] any 'x', 'y' or 'z'
1047 [a-zA-Z]$ any alphabetic character at the end of a line
1048 \c[a-z]$ same
Bram Moolenaara3e6bc92013-01-30 14:18:00 +01001049 [А-яЁё] Russian alphabet (with utf-8 and cp1251)
1050
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00001051 */[\n]*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 With "\_" prepended the collection also includes the end-of-line.
1053 The same can be done by including "\n" in the collection. The
1054 end-of-line is also matched when the collection starts with "^"! Thus
1055 "\_[^ab]" matches the end-of-line and any character but "a" and "b".
1056 This makes it Vi compatible: Without the "\_" or "\n" the collection
1057 does not match an end-of-line.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00001058 *E769*
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001059 When the ']' is not there Vim will not give an error message but
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00001060 assume no collection is used. Useful to search for '['. However, you
1061 do get E769 for internal searching.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 If the sequence begins with "^", it matches any single character NOT
1064 in the collection: "[^xyz]" matches anything but 'x', 'y' and 'z'.
1065 - If two characters in the sequence are separated by '-', this is
1066 shorthand for the full list of ASCII characters between them. E.g.,
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02001067 "[0-9]" matches any decimal digit. Non-ASCII characters can be
1068 used, but the character values must not be more than 256 apart.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 - A character class expression is evaluated to the set of characters
1070 belonging to that character class. The following character classes
1071 are supported:
1072 Name Contents ~
1073*[:alnum:]* [:alnum:] letters and digits
1074*[:alpha:]* [:alpha:] letters
1075*[:blank:]* [:blank:] space and tab characters
1076*[:cntrl:]* [:cntrl:] control characters
1077*[:digit:]* [:digit:] decimal digits
1078*[:graph:]* [:graph:] printable characters excluding space
1079*[:lower:]* [:lower:] lowercase letters (all letters when
1080 'ignorecase' is used)
1081*[:print:]* [:print:] printable characters including space
1082*[:punct:]* [:punct:] punctuation characters
1083*[:space:]* [:space:] whitespace characters
1084*[:upper:]* [:upper:] uppercase letters (all letters when
1085 'ignorecase' is used)
1086*[:xdigit:]* [:xdigit:] hexadecimal digits
1087*[:return:]* [:return:] the <CR> character
1088*[:tab:]* [:tab:] the <Tab> character
1089*[:escape:]* [:escape:] the <Esc> character
1090*[:backspace:]* [:backspace:] the <BS> character
1091 The brackets in character class expressions are additional to the
1092 brackets delimiting a collection. For example, the following is a
1093 plausible pattern for a UNIX filename: "[-./[:alnum:]_~]\+" That is,
1094 a list of at least one character, each of which is either '-', '.',
1095 '/', alphabetic, numeric, '_' or '~'.
1096 These items only work for 8-bit characters.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001097 */[[=* *[==]*
1098 - An equivalence class. This means that characters are matched that
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001099 have almost the same meaning, e.g., when ignoring accents. This
1100 only works for Unicode, latin1 and latin9. The form is:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001101 [=a=]
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001102 */[[.* *[..]*
1103 - A collation element. This currently simply accepts a single
1104 character in the form:
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001105 [.a.]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 */\]*
1107 - To include a literal ']', '^', '-' or '\' in the collection, put a
1108 backslash before it: "[xyz\]]", "[\^xyz]", "[xy\-z]" and "[xyz\\]".
1109 (Note: POSIX does not support the use of a backslash this way). For
1110 ']' you can also make it the first character (following a possible
1111 "^"): "[]xyz]" or "[^]xyz]" {not in Vi}.
1112 For '-' you can also make it the first or last character: "[-xyz]",
1113 "[^-xyz]" or "[xyz-]". For '\' you can also let it be followed by
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02001114 any character that's not in "^]-\bdertnoUux". "[\xyz]" matches '\',
1115 'x', 'y' and 'z'. It's better to use "\\" though, future expansions
1116 may use other characters after '\'.
Bram Moolenaarff034192013-04-24 18:51:19 +02001117 - Omitting the trailing ] is not considered an error. "[]" works like
1118 "[]]", it matches the ']' character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 - The following translations are accepted when the 'l' flag is not
1120 included in 'cpoptions' {not in Vi}:
1121 \e <Esc>
1122 \t <Tab>
1123 \r <CR> (NOT end-of-line!)
1124 \b <BS>
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00001125 \n line break, see above |/[\n]|
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001126 \d123 decimal number of character
1127 \o40 octal number of character up to 0377
1128 \x20 hexadecimal number of character up to 0xff
1129 \u20AC hex. number of multibyte character up to 0xffff
1130 \U1234 hex. number of multibyte character up to 0xffffffff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 NOTE: The other backslash codes mentioned above do not work inside
1132 []!
1133 - Matching with a collection can be slow, because each character in
1134 the text has to be compared with each character in the collection.
1135 Use one of the other atoms above when possible. Example: "\d" is
1136 much faster than "[0-9]" and matches the same characters.
1137
1138 */\%[]* *E69* *E70* *E369*
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001139\%[] A sequence of optionally matched atoms. This always matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 It matches as much of the list of atoms it contains as possible. Thus
1141 it stops at the first atom that doesn't match. For example: >
1142 /r\%[ead]
1143< matches "r", "re", "rea" or "read". The longest that matches is used.
1144 To match the Ex command "function", where "fu" is required and
1145 "nction" is optional, this would work: >
1146 /\<fu\%[nction]\>
1147< The end-of-word atom "\>" is used to avoid matching "fu" in "full".
1148 It gets more complicated when the atoms are not ordinary characters.
1149 You don't often have to use it, but it is possible. Example: >
1150 /\<r\%[[eo]ad]\>
1151< Matches the words "r", "re", "ro", "rea", "roa", "read" and "road".
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00001152 There can be no \(\), \%(\) or \z(\) items inside the [] and \%[] does
1153 not nest.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00001154 To include a "[" use "[[]" and for "]" use []]", e.g.,: >
1155 /index\%[[[]0[]]]
1156< matches "index" "index[", "index[0" and "index[0]".
Bram Moolenaardb84e452010-08-15 13:50:43 +02001157 {not available when compiled without the |+syntax| feature}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaar677ee682005-01-27 14:41:15 +00001159 */\%d* */\%x* */\%o* */\%u* */\%U* *E678*
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001160
1161\%d123 Matches the character specified with a decimal number. Must be
1162 followed by a non-digit.
1163\%o40 Matches the character specified with an octal number up to 0377.
1164 Numbers below 040 must be followed by a non-octal digit or a non-digit.
1165\%x2a Matches the character specified with up to two hexadecimal characters.
1166\%u20AC Matches the character specified with up to four hexadecimal
1167 characters.
1168\%U1234abcd Matches the character specified with up to eight hexadecimal
1169 characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
1171==============================================================================
11727. Ignoring case in a pattern */ignorecase*
1173
1174If the 'ignorecase' option is on, the case of normal letters is ignored.
1175'smartcase' can be set to ignore case when the pattern contains lowercase
1176letters only.
1177 */\c* */\C*
1178When "\c" appears anywhere in the pattern, the whole pattern is handled like
1179'ignorecase' is on. The actual value of 'ignorecase' and 'smartcase' is
1180ignored. "\C" does the opposite: Force matching case for the whole pattern.
1181{only Vim supports \c and \C}
1182Note that 'ignorecase', "\c" and "\C" are not used for the character classes.
1183
1184Examples:
1185 pattern 'ignorecase' 'smartcase' matches ~
1186 foo off - foo
1187 foo on - foo Foo FOO
1188 Foo on off foo Foo FOO
1189 Foo on on Foo
1190 \cfoo - - foo Foo FOO
1191 foo\C - - foo
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193Technical detail: *NL-used-for-Nul*
1194<Nul> characters in the file are stored as <NL> in memory. In the display
1195they are shown as "^@". The translation is done when reading and writing
1196files. To match a <Nul> with a search pattern you can just enter CTRL-@ or
1197"CTRL-V 000". This is probably just what you expect. Internally the
1198character is replaced with a <NL> in the search pattern. What is unusual is
1199that typing CTRL-V CTRL-J also inserts a <NL>, thus also searches for a <Nul>
1200in the file. {Vi cannot handle <Nul> characters in the file at all}
1201
1202 *CR-used-for-NL*
1203When 'fileformat' is "mac", <NL> characters in the file are stored as <CR>
Bram Moolenaare37d50a2008-08-06 17:06:04 +00001204characters internally. In the text they are shown as "^J". Otherwise this
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205works similar to the usage of <NL> for a <Nul>.
1206
1207When working with expression evaluation, a <NL> character in the pattern
1208matches a <NL> in the string. The use of "\n" (backslash n) to match a <NL>
1209doesn't work there, it only works to match text in the buffer.
1210
1211 *pattern-multi-byte*
1212Patterns will also work with multi-byte characters, mostly as you would
1213expect. But invalid bytes may cause trouble, a pattern with an invalid byte
1214will probably never match.
1215
1216==============================================================================
Bram Moolenaar362e1a32006-03-06 23:29:24 +000012178. Composing characters *patterns-composing*
1218
1219 */\Z*
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001220When "\Z" appears anywhere in the pattern, all composing characters are
1221ignored. Thus only the base characters need to match, the composing
1222characters may be different and the number of composing characters may differ.
1223Only relevant when 'encoding' is "utf-8".
Bram Moolenaar543b7ef2013-06-01 14:50:56 +02001224Exception: If the pattern starts with one or more composing characters, these
1225must match.
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001226 */\%C*
1227Use "\%C" to skip any composing characters. For example, the pattern "a" does
1228not match in "càt" (where the a has the composing character 0x0300), but
1229"a\%C" does. Note that this does not match "cát" (where the á is character
12300xe1, it does not have a compositing character). It does match "cat" (where
1231the a is just an a).
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001232
1233When a composing character appears at the start of the pattern of after an
1234item that doesn't include the composing character, a match is found at any
1235character that includes this composing character.
1236
1237When using a dot and a composing character, this works the same as the
1238composing character by itself, except that it doesn't matter what comes before
1239this.
1240
Bram Moolenaar543b7ef2013-06-01 14:50:56 +02001241The order of composing characters does not matter. Also, the text may have
1242more composing characters than the pattern, it still matches. But all
1243composing characters in the pattern must be found in the text.
1244
1245Suppose B is a base character and x and y are composing characters:
1246 pattern text match ~
1247 Bxy Bxy yes (perfect match)
1248 Bxy Byx yes (order ignored)
1249 Bxy By no (x missing)
1250 Bxy Bx no (y missing)
Bram Moolenaar203d04d2013-06-06 21:36:40 +02001251 Bx Bx yes (perfect match)
Bram Moolenaar543b7ef2013-06-01 14:50:56 +02001252 Bx By no (x missing)
1253 Bx Bxy yes (extra y ignored)
1254 Bx Byx yes (extra y ignored)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001255
1256==============================================================================
12579. Compare with Perl patterns *perl-patterns*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
1259Vim's regexes are most similar to Perl's, in terms of what you can do. The
1260difference between them is mostly just notation; here's a summary of where
1261they differ:
1262
1263Capability in Vimspeak in Perlspeak ~
1264----------------------------------------------------------------
1265force case insensitivity \c (?i)
1266force case sensitivity \C (?-i)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001267backref-less grouping \%(atom\) (?:atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268conservative quantifiers \{-n,m} *?, +?, ??, {}?
12690-width match atom\@= (?=atom)
12700-width non-match atom\@! (?!atom)
12710-width preceding match atom\@<= (?<=atom)
12720-width preceding non-match atom\@<! (?<!atom)
1273match without retry atom\@> (?>atom)
1274
1275Vim and Perl handle newline characters inside a string a bit differently:
1276
1277In Perl, ^ and $ only match at the very beginning and end of the text,
1278by default, but you can set the 'm' flag, which lets them match at
1279embedded newlines as well. You can also set the 's' flag, which causes
1280a . to match newlines as well. (Both these flags can be changed inside
1281a pattern using the same syntax used for the i flag above, BTW.)
1282
1283On the other hand, Vim's ^ and $ always match at embedded newlines, and
1284you get two separate atoms, \%^ and \%$, which only match at the very
1285start and end of the text, respectively. Vim solves the second problem
1286by giving you the \_ "modifier": put it in front of a . or a character
1287class, and they will match newlines as well.
1288
1289Finally, these constructs are unique to Perl:
1290- execution of arbitrary code in the regex: (?{perl code})
1291- conditional expressions: (?(condition)true-expr|false-expr)
1292
1293...and these are unique to Vim:
1294- changing the magic-ness of a pattern: \v \V \m \M
1295 (very useful for avoiding backslashitis)
1296- sequence of optionally matching atoms: \%[atoms]
1297- \& (which is to \| what "and" is to "or"; it forces several branches
1298 to match at one spot)
1299- matching lines/columns by number: \%5l \%5c \%5v
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001300- setting the start and end of the match: \zs \ze
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301
1302==============================================================================
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000130310. Highlighting matches *match-highlight*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
1305 *:mat* *:match*
1306:mat[ch] {group} /{pattern}/
1307 Define a pattern to highlight in the current window. It will
1308 be highlighted with {group}. Example: >
1309 :highlight MyGroup ctermbg=green guibg=green
1310 :match MyGroup /TODO/
1311< Instead of // any character can be used to mark the start and
1312 end of the {pattern}. Watch out for using special characters,
1313 such as '"' and '|'.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001314
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 {group} must exist at the moment this command is executed.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001316
1317 The {group} highlighting still applies when a character is
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001318 to be highlighted for 'hlsearch', as the highlighting for
1319 matches is given higher priority than that of 'hlsearch'.
1320 Syntax highlighting (see 'syntax') is also overruled by
1321 matches.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001322
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 Note that highlighting the last used search pattern with
1324 'hlsearch' is used in all windows, while the pattern defined
1325 with ":match" only exists in the current window. It is kept
1326 when switching to another buffer.
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001327
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001328 'ignorecase' does not apply, use |/\c| in the pattern to
1329 ignore case. Otherwise case is not ignored.
1330
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00001331 'redrawtime' defines the maximum time searched for pattern
1332 matches.
1333
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00001334 When matching end-of-line and Vim redraws only part of the
1335 display you may get unexpected results. That is because Vim
1336 looks for a match in the line where redrawing starts.
1337
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00001338 Also see |matcharg()| and |getmatches()|. The former returns
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001339 the highlight group and pattern of a previous |:match|
1340 command. The latter returns a list with highlight groups and
1341 patterns defined by both |matchadd()| and |:match|.
1342
1343 Highlighting matches using |:match| are limited to three
Bram Moolenaar822ff862014-06-12 21:46:14 +02001344 matches (aside from |:match|, |:2match| and |:3match| are
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001345 available). |matchadd()| does not have this limitation and in
1346 addition makes it possible to prioritize matches.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 Another example, which highlights all characters in virtual
1349 column 72 and more: >
1350 :highlight rightMargin term=bold ctermfg=blue guifg=blue
1351 :match rightMargin /.\%>72v/
1352< To highlight all character that are in virtual column 7: >
1353 :highlight col8 ctermbg=grey guibg=grey
1354 :match col8 /\%<8v.\%>7v/
1355< Note the use of two items to also match a character that
1356 occupies more than one virtual column, such as a TAB.
1357
1358:mat[ch]
1359:mat[ch] none
1360 Clear a previously defined match pattern.
1361
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001362
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001363:2mat[ch] {group} /{pattern}/ *:2match*
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001364:2mat[ch]
1365:2mat[ch] none
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001366:3mat[ch] {group} /{pattern}/ *:3match*
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001367:3mat[ch]
1368:3mat[ch] none
1369 Just like |:match| above, but set a separate match. Thus
1370 there can be three matches active at the same time. The match
1371 with the lowest number has priority if several match at the
1372 same position.
1373 The ":3match" command is used by the |matchparen| plugin. You
1374 are suggested to use ":match" for manual matching and
1375 ":2match" for another plugin.
1376
1377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 vim:tw=78:ts=8:ft=help:norl: