USR_27
*usr_27.txt*    For IdeaVIM version 0.12.0.  Last change: 2006 Nov 12
                     IdeaVIM USER MANUAL - by Rick Maddy
                         Search commands and patterns
In chapter 3 a few simple search patterns were mentioned |03.9|.  Vim can do
much more complex searches.  This chapter explains the most often used ones.
A detailed specification can be found here: |pattern|
|27.1|  Ignoring case
|27.2|  Wrapping around the file end
|27.3|  Offsets
|27.4|  Matching multiple times
|27.5|  Alternatives
|27.6|  Character ranges
|27.7|  Character classes
|27.8|  Matching a line break
|27.9|  Examples
     Next chapter: |usr_28.txt|  Folding
 Previous chapter: |usr_26.txt|  Repeating
Table of contents: |usr_toc.txt|
*27.1*  Ignoring case
By default, Vim's searches are case sensitive.  Therefore, "include",
"INCLUDE", and "Include" are three different words and a search will match
only one of them.
   Now switch on the 'ignorecase' option:
        :set ignorecase
        :set noignorecase
        :set ignorecase smartcase
        pattern                 matches 
        word                    word, Word, WORD, WoRd, etc.
        Word                    Word
        WORD                    WORD
        WoRd                    WoRd
CASE IN ONE PATTERN
If you want to ignore case for one specific pattern, you can do this by
prepending the "\c" string.  Using "\C" will make the pattern to match case.
This overrules the 'ignorecase' and 'smartcase' options, when "\c" or "\C" is
used their value doesn't matter.
        pattern                 matches 
        \Cword                  word
        \CWord                  Word
        \cword                  word, Word, WORD, WoRd, etc.
        \cWord                  word, Word, WORD, WoRd, etc.
A big advantage of using "\c" and "\C" is that it sticks with the pattern.
Thus if you repeat a pattern from the search history, the same will happen, no
matter if 'ignorecase' or 'smartcase' was changed.
*27.2*  Wrapping around the file end
By default, a forward search starts searching for the given string at the
current cursor location.  It then proceeds to the end of the file.  If it has
not found the string by that time, it starts from the beginning and searches
from the start of the file to the cursor location.
   Keep in mind that when repeating the "n" command to search for the next
match, you eventually get back to the first match.  If you don't notice this
you keep searching forever!  To give you a hint, Vim displays this message:
        search hit BOTTOM, continuing at TOP 
If you use the "?" command, to search in the other direction, you get this
message:
        search hit TOP, continuing at BOTTOM 
NOT WRAPPING
To turn off search wrapping, use the following command:
        :set nowrapscan
        E385: search hit BOTTOM without match for: forever 
Thus you can find all matches by going to the start of the file with "gg" and
keep searching until you see this message.
   If you search in the other direction, using "?", you get:
        E384: search hit TOP without match for: forever 
*27.3*  Offsets
By default, the search command leaves the cursor positioned on the beginning
of the pattern.  You can tell Vim to leave it some other place by specifying
an offset.  For the forward search command "/", the offset is specified by
appending a slash (/) and the offset:
        /default/2
        /const/e
        /const/e+1
        /const/e-1
        /const/b+2
        /that
        //e
        /that/e
        /
        //
        ?const?e-2
        /const/-2
*27.4*  Matching multiple times
The "*" item specifies that the item before it can match any number of times.
Thus:
        /a*
        /\(ab\)*
        /ab\+
        /folders\=
{n,m}".  "n" and "m" are
numbers.  The item before it will be matched "n" to "m" times (inclusive).
Example:
        /ab\{3,5}
        pattern         match count 
        \{,4}           0, 1, 2, 3 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}
{-}".  This matches the item before it zero or
more times, as few as possible.  The item by itself always match zero times.
It is useful when combined with something else.  Example:
        /a.\{-}b
        /a.*b
*27.5*  Alternatives
The "or" operator in a pattern is "\|".  Example:
        /foo\|bar
        /one\|two\|three
        /\(foo\|bar\)\+
        /end\(if\|while\|for\)
        /forever\&...
*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]
        /[0123456789abcdef]
        /[0-9a-f]
<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
        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.  Use "\(\d\|\l\)" instead.
See |/\s| for the whole list of these ranges.
*27.7*  Character classes
The character range matches a fixed set of characters.  A character class is
similar, but with an essential difference: The set of characters can be
redefined without changing the search pattern.
   For example, search for this pattern:
        /\f\+
        item    matches                         option 
        \i      identifier characters           'isident'
        \I      like \i, excluding digits
        \k      keyword characters              'iskeyword'
        \K      like \k, excluding digits
        \p      printable characters            'isprint'
        \P      like \p, excluding digits
        \f      file name characters            'isfname'
        \F      like \f, excluding digits
*27.8*  Matching a line break
Vim can find a pattern that includes a line break.  You need to specify where
the line break happens, because all items mentioned so far don't match a line
break.
   To check for a line break in a specific place, use the "\n" item:
        /the\nword
        /the\_sword
        /the\_s\+word
Note:
        "\_.*" matches everything until the end of the file.  Be careful with
        this, it can make a search command very slow.
Another example is "\_[]", a character range that includes a line break:
        /"\_[^"]*"
*27.9*  Examples
Here are a few search patterns you might find useful.  This shows how the
items mentioned above can be combined.
FINDING A CALIFORNIA LICENSE PLATE
A sample license place number is "1MGU103".  It has one digit, three uppercase
letters and three digits.  Directly putting this into a search pattern:
        /\d\u\u\u\d\d\d
        /\d\u\{3}\d\{3}
        /[0-9][A-Z]\{3}[0-9]\{3}
        /\<\h\w*\>
<" and "\>" are used to find only whole words.  "\h" stands for "[A-Za-z_]"
and "\w" for "[0-9A-Za-z_]".
        Note:
        "\<" and "\>" depend on the 'iskeyword' option.  If it includes "-",
        for example, then "ident-" is not matched.  In this situation use:
                /\w\@<!\h\w*\w\@!
        This checks if "\w" does not match before or after the identifier.
        See |/\@<!| and |/\@!|.
Next chapter: |usr_28.txt|  Folding
Copyright: see |manual-copyright|