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