or 4
\{3,} 3, 4, 5, etc.
\{0,1} 0 or 1, same as \=
\{0,} 0 or more, same as *
\{1,} 1 or more, same as \+
\{3} 3
MATCHING AS LITTLE AS POSSIBLE
The items so far match as many characters as they can find. To match as few
as possible, use "\{-n,m}". It works the same as "\{n,m}", except that the
minimal amount possible is used.
For example, use: >
/ab\{-1,3}
Will match "ab" in "abbb". Actually, it will never match more than one b,
because there is no reason to match more. It requires something else to force
it to match more than the lower limit.
The same rules apply to removing "n" and "m". It's even possible to remove
both of the numbers, resulting in "\{-}". This matches the item before it
zero or more times, as few as possible. The item by itself always matches
zero times. It is useful when combined with something else. Example: >
/a.\{-}b
This matches "axb" in "axbxb". If this pattern would be used: >
/a.*b
It would try to match as many characters as possible with ".*", thus it
matches "axbxb" as a whole.
==============================================================================
*27.5* Alternatives
The "or" operator in a pattern is "\|". Example: >
/foo\|bar
This matches "foo" or "bar". More alternatives can be concatenated: >
/one\|two\|three
Matches "one", "two" and "three".
To match multiple times, the whole thing must be placed in "\(" and "\)": >
/\(foo\|bar\)\+
This matches "foo", "foobar", "foofoo", "barfoobar", etc.
Another example: >
/end\(if\|while\|for\)
This matches "endif", "endwhile" and "endfor".
A related item is "\&". This requires that both alternatives match in the
same place. The resulting match uses the last alternative. Example: >
/forever\&...
This matches "for" in "forever". It will not match "fortuin", for example.
==============================================================================
*27.6* Character ranges
To match "a", "b" or "c" you could use "/a\|b\|c". When you want to match all
letters from "a" to "z" this gets very long. There is a shorter method: >
/[a-z]
The [] construct matches a single character. Inside you specify which
characters to match. You can include a list of characters, like this: >
/[0123456789abcdef]
This will match any of the characters included. For consecutive characters
you can specify the range. "0-3" stands for "0123". "w-z" stands for "wxyz".
Thus the same command as above can be shortened to: >
/[0-9a-f]
To match the "-" character itself make it the first or last one in the range.
These special characters are accepted to make it easier to use them inside a
[] range (they can actually be used anywhere in the search pattern):
\e <Esc>
\t <Tab>
\r <CR>
\b <BS>
There are a few more special cases for [] ranges, see |/[]| for the whole
story.
COMPLEMENTED RANGE
To avoid matching a specific character, use "^" at the start of the range.
The [] item then matches everything but the characters included. Example: >
/"[^"]*"
<
" a double quote
[^"] any character that is not a double quote
* as many as possible
" a double quote again
This matches "foo" and "3!x", including the double quotes.
PREDEFINED RANGES
A number of ranges are used very often. Vim provides a shortcut for these.
For example: >
/\a
Finds alphabetic characters. This is equal to using "/[a-zA-Z]". Here are a
few more of these:
item matches equivalent ~
\d digit [0-9]
\D non-digit [^0-9]
\x hex digit [0-9a-fA-F]
\X non-hex digit [^0-9a-fA-F]
\s white space [ ] (<Tab> and <Space>)
\S non-white characters [^ ] (not <Tab> and <Space>)
\l lowercase alpha [a-z]
\L non-lowercase alpha [^a-z]
\u uppercase alpha [A-Z]
\U non-uppercase alpha [^A-Z]
Note:
Using these predefined ranges works a lot faster than the character
range it stands for.
These items can not be used inside []. Thus "[\d\l]" does NOT work to
match a digit or lowercase alpha.