home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vim53os2.zip / vim-5.3 / doc / pattern.txt < prev    next >
Text File  |  1998-08-30  |  19KB  |  443 lines

  1. *pattern.txt*   For Vim version 5.3.  Last modification: 1998 Aug 17
  2.  
  3.  
  4.           VIM REFERENCE MANUAL    by Bram Moolenaar
  5.  
  6.  
  7. Patterns and search commands                *pattern-searches*
  8.  
  9. 1. Search commands        |search-commands|
  10. 2. The definition of a pattern    |search-pattern|
  11.  
  12. ==============================================================================
  13. 1. Search commands                    *search-commands*
  14.  
  15.                             */*
  16. /{pattern}[/]<CR>    Search forward for the [count]'th occurrence of
  17.             {pattern} (exclusive).
  18.  
  19. /{pattern}/{offset}<CR>    Search forward for the [count]'th occurrence of
  20.             {pattern} and go |{offset}| lines up or down.
  21.             (linewise).
  22.  
  23.                             */<CR>*
  24. /<CR>            Search forward for the [count]'th latest used
  25.             pattern with latest used |{offset}|.
  26.  
  27. //{offset}<CR>        Search forward for the [count]'th latest used
  28.             pattern with new |{offset}|.  If {offset} is empty no
  29.             offset is used.
  30.  
  31.                             *?*
  32. ?{pattern}[?]<CR>    Search backward for the [count]'th previous
  33.             occurrence of {pattern} (exclusive).
  34.  
  35. ?{pattern}?{offset}<CR>    Search backward for the [count]'th previous
  36.             occurrence of {pattern} and go |{offset}| lines up or
  37.             down (linewise).
  38.  
  39.                             *?<CR>*
  40. ?<CR>            Search backward for the [count]'th latest used
  41.             pattern with latest used |{offset}|.
  42.  
  43. ??{offset}<CR>        Search backward for the [count]'th latest used
  44.             pattern with new |{offset}|.  If {offset} is empty no
  45.             offset is used.
  46.  
  47.                             *n*
  48. n            Repeat the latest "/" or "?" [count] times.  {Vi: no
  49.             count}
  50.  
  51.                             *N*
  52. N            Repeat the latest "/" or "?" [count] times in
  53.             opposite direction.  {Vi: no count}
  54.  
  55.                             *star*
  56. *            Search forward for the [count]'th occurrence of the
  57.             word nearest to the cursor.  The word used for the
  58.             search is the first of:
  59.                 1. the keyword under the cursor |'iskeyword'|
  60.                 2. the first keyword after the cursor, in the
  61.                    current line
  62.                 3. the non-blank word under the cursor
  63.                 4. the first non-blank word after the cursor,
  64.                    in the current line
  65.             Only whole keywords are searched for, like with the
  66.             command "/\<keyword\>".  (exclusive)  {not in Vi}
  67.  
  68.                             *#*
  69. #            Same as "*", but search backward.  The pound sign
  70.             (character 163) also works.  If the "#" key works as
  71.             backspace, try using "stty erase <BS>" before starting
  72.             Vim (<BS> is CTRL-H or a real backspace).  {not in Vi}
  73.  
  74.                             *gstar*
  75. g*            Like "*", but don't put "\<" and "\>" around the word.
  76.             This makes the search also find matches that are not a
  77.             whole word.  {not in Vi}
  78.  
  79.                             *g#*
  80. g#            Like "#", but don't put "\<" and "\>" around the word.
  81.             This makes the search also find matches that are not a
  82.             whole word.  {not in Vi}
  83.  
  84.                             *gd*
  85. gd            Goto local Declaration.  When the cursor is on a local
  86.             variable, this command will jump to its declaration.
  87.             First a search is made for the end of the previous
  88.             function, just like "[]".  If it is not found the
  89.             search stops in line 1.  From this position a search is
  90.             made for the keyword under the cursor, like with "*",
  91.             but lines that look like a comment are ignored (see
  92.             'comments' option).  Note that this is not guaranteed
  93.             to work, Vim does not really check the syntax, it only
  94.             searches for a match with the keyword.  If included
  95.             files also need to be searched use the commands listed
  96.             in |include-search|.  {not in Vi}
  97.  
  98.                             *gD*
  99. gD            Goto global Declaration.  When the cursor is on a
  100.             global variable that is defined in the file, this
  101.             command will jump to its declaration.  This works just
  102.             like "gd", except that the search for the keyword
  103.             always starts in line 1.  {not in Vi}
  104.  
  105.                             *CTRL-C*
  106. CTRL-C            Interrupt current (search) command.  Use CTRL-Break on
  107.             MS-DOS |dos-CTRL-Break|.
  108.             In Normal mode, any pending command is aborted.
  109.  
  110.                             *:noh* *:nohlsearch*
  111. :noh[lsearch]        Stop the highlighting for the 'hlsearch' option.  It
  112.             is automatically turned back on when using a search
  113.             command, or setting the 'hlsearch' option.
  114.  
  115. While typing the search pattern the current match will be shown if the
  116. 'incsearch' option is on.  Remember that you still have to finish the search
  117. command with <CR> to actually position the cursor at the displayed match.  Or
  118. use <Esc> to abandon the search.
  119.  
  120. All matches for the last used search pattern will be highlighted if you set
  121. the 'hlsearch' option.  This can be suspended with the |:nohlsearch| command.
  122.  
  123.                     *search-offset* *{offset}*
  124. These commands search for the specified pattern.  With "/" and "?" an
  125. additional offset may be given.  There are two types of offsets: line offsets
  126. and character offsets.  {the character offsets are not in Vi}
  127.  
  128. The offset gives the cursor position relative to the found match:
  129.     [num]    [num] lines downwards, in column 1
  130.     +[num]    [num] lines downwards, in column 1
  131.     -[num]    [num] lines upwards, in column 1
  132.     e[+num]    [num] characters to the right of the end of the match
  133.     e[-num]    [num] characters to the left of the end of the match
  134.     s[+num]    [num] characters to the right of the start of the match
  135.     s[-num]    [num] characters to the left of the start of the match
  136.     b[+num]    [num] characters to the right of the start (begin) of the match
  137.     b[-num]    [num] characters to the left of the start (begin) of the match
  138.  
  139. If a '-' or '+' is given but [num] is omitted, a count of one will be used.
  140. When including an offset with 'e', the search becomes inclusive (the
  141. character the cursor lands on is included in operations).
  142.  
  143. Examples:
  144.  
  145. pattern            cursor position    ~
  146. /test/+1        one line below "test", in column 1
  147. /test/e            on the last t of "test"
  148. /test/s+2        on the 's' of "test"
  149. /test/b-3        three characters before "test"
  150.  
  151. If one of these commands is used after an operator, the characters between
  152. the cursor position before and after the search is affected.  However, if a
  153. line offset is given, the whole lines between the two cursor positions are
  154. affected.
  155.  
  156.                             *//;*
  157. A very special offset is ';' followed by another search command.  For example:
  158.  
  159. >  /test 1/;/test
  160. >  /test.*/+1;?ing?
  161.  
  162. The first one first finds the next occurence of "test 1", and then the first
  163. occurence of "test" after that.
  164.  
  165. This is like executing two search commands after each other, except that:
  166. - It can be used as a single motion command after an operator.
  167. - The direction for a following "n" or "N" command comes from the first
  168.   search command.
  169. - When an error occurs the cursor is not moved at all.
  170.  
  171. The last used pattern and offset are remembered.  They can be used to repeat
  172. the search, possibly in another direction or with another count.  Note that
  173. two patterns are remembered: one for 'normal' search commands and one for the
  174. substitute command ":s".  Each time an empty pattern is given, the previously
  175. used pattern is used.
  176.  
  177. All matches for the last used search pattern will be highlighted if you set
  178. the 'hlsearch' option.
  179.  
  180. In Vi the ":tag" command sets the last search pattern when the tag is searched
  181. for.  In Vim this is not done, the previous search pattern is still remembered,
  182. unless the 't' flag is present in 'cpoptions'.  The search pattern is always
  183. put in the search history.
  184.  
  185. If the 'wrapscan' option is on (which is the default), searches wrap around
  186. the end of the buffer.  If 'wrapscan' is not set, the backward search stops
  187. at the beginning and the forward search stops at the end of the buffer.  If
  188. 'wrapscan' is set and the pattern was not found the error message "pattern
  189. not found" is given, and the cursor will not be moved.  If 'wrapscan' is not
  190. set the message becomes "search hit BOTTOM without match" when searching
  191. forward, or "search hit TOP without match" when searching backward.  If
  192. wrapscan is set and the search wraps around the end of the file the message
  193. "search hit TOP, continuing at BOTTOM" or "search hit BOTTOM, continuing at
  194. TOP" is given when searching backwards or forwards respectively.  This can be
  195. switched off by setting the 's' flag in the 'shortmess' option.  The highlight
  196. method 'w' is used for this message (default: standout).
  197.  
  198.                             *search-range*
  199. You cannot limit the search command "/" to a certain range of lines.  A trick
  200. to do this anyway is to use the ":substitute" command with the 'c' flag.
  201. Example:
  202. >  :.,300s/Pattern//gc
  203. This command will search from the cursor position until line 300 for
  204. "Pattern".  At the match, you will be asked to type a character.  Type 'q' to
  205. stop at this match, type 'n' to find the next match.
  206.  
  207. The "*", "#", "g*" and "g#" commands look for a word near the cursor in this
  208. order, the first one that is found is used:
  209. - The keyword currently under the cursor.
  210. - The first keyword to the right of the cursor, in the same line.
  211. - The WORD currently under the cursor.
  212. - The first WORD to the right of the cursor, in the same line.
  213. The keyword may only contain letters and characters in 'iskeyword'.
  214. The WORD may contain any non-blanks (<Tab>s and/or <Space>s).
  215. Note that if you type with ten fingers, the characters are easy to remember:
  216. the "#" is under your left hand middle finger (search to the left and up) and
  217. the "*" is under your right hand middle finger (search to the right and down).
  218.  
  219. ==============================================================================
  220. 2. The definition of a pattern        *search-pattern* *pattern* *[pattern]*
  221.                     *regular-expression* *regexp*
  222.  
  223. Patterns may contain special characters, depending on the setting of the
  224. 'magic' option.
  225.  
  226.                             */bar* */\bar*
  227. 1. A pattern is one or more branches, separated by "\|".  It matches anything
  228.    that matches one of the branches.  Example: "foo\|beep" matches "foo" and
  229.    "beep".  If more than one branch matches, the first one is used.
  230.  
  231. 2. A branch is one or more pieces, concatenated.  It matches a match for the
  232.    first, followed by a match for the second, etc.  Example: "foo[0-9]beep",
  233.    first match "foo", then a digit and then "beep".
  234.  
  235. 3. A piece is an atom, possibly followed by:
  236.      'magic' 'nomagic'    ~
  237.                             */star* */\star*
  238.     *    \*    matches 0 or more of the preceding atom, as much as
  239.             possible
  240.                             */\+*
  241.     \+    \+    matches 1 or more of the preceding atom, as much as
  242.             possible {not in Vi}
  243.                             */\=*
  244.     \=    \=    matches 0 or 1 of the preceding atom, as much as
  245.             possible {not in Vi}
  246.                             */\{*
  247.     \{n,m}  \{n,m}    matches n to m of the preceding atom, as much as
  248.             possible {not in Vi}
  249.     \{n}    \{n}    matches n of the preceding atom {not in Vi}
  250.     \{n,}   \{n,}    matches at least n of the preceding atom, as much as
  251.             possible {not in Vi}
  252.     \{,m}   \{,m}    matches 0 to m of the preceding atom, as much as
  253.             possible {not in Vi}
  254.     \{}     \{}    matches 0 or more of the preceding atom, as much as
  255.             possible (same as *) {not in Vi}
  256.                             */\{-*
  257.     \{-n,m}  \{-n,m} matches n to m of the preceding atom, as few as
  258.             possible {not in Vi}
  259.     \{-n}    \{-n}    matches n of the preceding atom {not in Vi}
  260.     \{-n,}   \{-n,}    matches at least n of the preceding atom, as few as
  261.             possible {not in Vi}
  262.     \{-,m}   \{-,m}    matches 0 to m of the preceding atom, as few as
  263.             possible {not in Vi}
  264.     \{-}     \{-}    matches 0 or more of the preceding atom, as few as
  265.             possible {not in Vi}
  266.  
  267.         (n and m are decimal numbers between 0 and 32000)
  268.  
  269.         If a "-" appears immediately after the "{", then a shortest
  270.         match first algorithm is used (see example below).  In
  271.         particular, "\{-}" is the same as "*" but uses the shortest
  272.         match first algorithm.  BUT: A match that starts earlier is
  273.         preferred over a shorter match: "a\{-}b" matches "aaab" in
  274.         "xaaab".
  275.  
  276.     Examples:
  277.        .*    .\*    matches anything, also empty string
  278.        ^.\+$    ^.\+$    matches any non-empty line
  279.        foo\=    foo\=    matches "fo" and "foo"
  280.        ab\{2,3}c    matches "abbc" or "abbbc"
  281.        a\{5}        matches "aaaaa".
  282.        ab\{2,}c        matches "abbc", "abbbc", "abbbbc", etc
  283.        ab\{,3}c        matches "ac", "abc", "abbc" or "abbbc".
  284.        a[bc]\{3}d    matches "abbbd", "abbcd", "acbcd", "acccd", etc.
  285.        a\(bc\)\{1,2}d    matches "abcd" or "abcbcd"
  286.        a[bc]\{-}[cd]    matches "abc" in "abcd"
  287.        a[bc]*[cd]    matches "abcd" in "abcd"
  288.  
  289.  
  290. 4. An atom can be:
  291.       magic   nomagic    ~
  292.     ^    ^    at beginning of pattern, matches start of line    */^*
  293.     $    $    at end of pattern or in front of "\|",        */$*
  294.             matches <EOL>
  295.     .    \.    matches any single character          */.* */\.*
  296.     \<    \<    matches the beginning of a word            */\<*
  297.     \>    \>    matches the end of a word            */\>*
  298.  
  299.     Character classes {not in Vi}:
  300.     \i    \i    identifier character (see 'isident' option)    */\i*
  301.     \I    \I    like "\i", but excluding digits            */\I*
  302.     \k    \k    keyword character (see 'iskeyword' option)    */\k*
  303.     \K    \K    like "\k", but excluding digits            */\K*
  304.     \f    \f    file name character (see 'isfname' option)    */\f*
  305.     \F    \F    like "\f", but excluding digits            */\F*
  306.     \p    \p    printable character (see 'isprint' option)    */\p*
  307.     \P    \P    like "\p", but excluding digits            */\P*
  308.  
  309.                         *whitespace* *white-space*
  310.     \s    \s    whitespace character: <Space> and <Tab>        */\s*
  311.     \S    \S    non-whitespace character; opposite of \s    */\S*
  312.     \d    \d    digit:                [0-9]        */\d*
  313.     \D    \D    non-digit:            [^0-9]        */\D*
  314.     \x    \x    hex digit:            [0-9A-Fa-f]    */\x*
  315.     \X    \X    non-hex digit:            [^0-9A-Fa-f]    */\X*
  316.     \o    \o    octal digit:            [0-7]        */\o*
  317.     \O    \O    non-octal digit:        [^0-7]        */\O*
  318.     \w    \w    word character:            [0-9A-Za-z_]    */\w*
  319.     \W    \W    non-word character:        [^0-9A-Za-z_]    */\W*
  320.     \h    \h    head of word character:        [A-Za-z_]    */\h*
  321.     \H    \H    non-head of word character:    [^A-Za-z_]    */\H*
  322.     \a    \a    alphabetic character:        [A-Za-z]    */\a*
  323.     \A    \A    non-alphabetic character:    [^A-Za-z]    */\A*
  324.     \l    \l    lowercase character:        [a-z]        */\l*
  325.     \L    \L    non-lowercase character:    [^a-z]        */\L*
  326.     \u    \u    uppercase character:        [A-Z]        */\u*
  327.     \U    \U    non-uppercase character        [^A-Z]        */\U*
  328.             NOTE: using the atom is faster than the [] form
  329.     (end of character classes)
  330.  
  331.     \e    \e    matches <Esc>                    */\e*
  332.     \t    \t    matches <Tab>                    */\t*
  333.     \r    \r    matches <CR>                    */\r*
  334.     \b    \b    matches <BS>                    */\b*
  335.     \n    \n    matches <NL> Not available yet!  Will be used    */\n*
  336.             for multi-line patterns
  337.     ~    \~    matches the last given substitute string    */~* */\~*
  338.     \(\)    \(\)    A pattern enclosed by escaped parentheses      */\(\)*
  339.             (e.g., "\(^a\)") matches that pattern
  340.     \1      \1    Matches the same string that was matched by    */\1*
  341.             the first sub-expression in \( and \). {not in Vi}
  342.             Example: "\([a-z]\).\1" matches "ata", "ehe", "tot",
  343.             etc.
  344.     \2      \2    Like "\1", but uses second sub-expression,    */\2*
  345.        ...
  346.     \9      \9    Like "\1", but uses ninth sub-expression.    */\9*
  347.  
  348.     x    x    A single character, with no special meaning,
  349.             matches itself
  350.     \x    \x    A backslash followed by a single character,    */\*
  351.             with no special meaning, is reserved for future
  352.             expansions
  353.  
  354.     []    \[]    A range. This is a sequence of characters    */[]*
  355.             enclosed in "[]" or "\[]".  It matches any    */\[]*
  356.             single character from the sequence.  E.g., "[xyz]"
  357.             matches any 'x', 'y' or 'z'.
  358.             - If the sequence begins with "^", it matches any
  359.               single character NOT in the sequence: "[^xyz]"
  360.               matches anything but 'x', 'y' and 'z'.
  361.             - If two characters in the sequence are separated by
  362.               '-', this is shorthand for the full list of ASCII
  363.               characters between them.  E.g., "[0-9]" matches any
  364.               decimal digit.
  365.             - A character class expression is evaluated to the set
  366.               of characters belonging to that character class.  The
  367.               following character classes are supported:
  368.               Name        Contents ~
  369.               [:alnum:]     letters and digits
  370.               [:alpha:]     letters
  371.               [:ascii:]     ASCII characters
  372.               [:blank:]     space and tab characters
  373.               [:cntrl:]     control characters
  374.               [:digit:]     decimal digits
  375.               [:graph:]     printable characters excluding space
  376.               [:lower:]     lowercase letters
  377.               [:print:]     printable characters including space
  378.               [:punct:]     punctuation characters
  379.               [:space:]     whitespace characters
  380.               [:upper:]     uppercase letters
  381.               [:xdigit:]    hexadecimal digits
  382.               The brackets in character class expressions are
  383.               additional to the brackets delimiting a range.
  384.               For example, the following is plausible for a UNIX
  385.               filename: [-./[:alnum:]_~]\+
  386.               That is, a list of at least one character, each of
  387.               which is either '-', '.', '/', alphabetic, numeric,
  388.               '_' or '~'.
  389.             - To include a literal ']', '^', '-' or '\' in the
  390.               sequence, put a backslash before it: "[xyz\]]",
  391.               "[\^xyz]", "[xy\-z]" and "[xyz\\]".
  392.               For ']' you can also make it the first character
  393.               (following a possible "^"):  "[]xyz]" or "[^]xyz]"
  394.               {not in Vi}.
  395.               For '-' you can also make it the first or last
  396.               character: "[-xyz]", "[^-xyz]" or "[xyz-]".
  397.               For '\' you can also let it be followed by any
  398.               character that's not in "^]-\etrb".  "[\xyz]"
  399.               matches '\', 'x', 'y' and 'z'.  It's better to use
  400.               "\\" though, future expansions may use other
  401.               characters after '\'.
  402.             - The following translations are accepted when the 'l'
  403.               flag is not included in 'cpoptions' {not in Vi}:
  404.                 \e    <Esc>
  405.                 \t    <Tab>
  406.                 \r    <CR>
  407.                 \b    <BS>
  408.             - Machting ranges can be slow, use one of the other
  409.               items above when possible.
  410.  
  411. If the 'ignorecase' option is on, the case of letters is ignored.
  412.  
  413. It is impossible to have a pattern that contains a line break (Sorry!).
  414.  
  415. Examples:
  416. ^beep(            Probably the start of the C function "beep".
  417.  
  418. [a-zA-Z]$        Any alphabetic character at the end of a line.
  419.  
  420. \<\I\i*        or
  421. \<\h\w*
  422. \<[a-zA-Z_][a-zA-Z0-9_]*
  423.             An identifier (e.g., in a C program).
  424.  
  425. \(\.$\|\. \)        A period followed by <EOL> or a space.
  426.             Note that "\(\. \|\.$\)" does not do the same,
  427.             because '$' is not <EOL> in front of '\)'.
  428.             This was done to remain Vi-compatible.
  429.  
  430. [.!?][])"']*\($\|[ ]\)    A search pattern that finds the end of a sentence,
  431.             with almost the same definition as the ")" command.
  432.  
  433. Technical detail:
  434. <Nul> characters in the file are stored as <NL> in memory.  In the display
  435. they are shown as "^@".  The translation is done when reading and writing
  436. files.  To match a <Nul> with a search pattern you can just enter CTRL-@ or
  437. "CTRL-V 000".  This is probably just what you expect.  Internally the
  438. character is replaced with a <NL> in the search pattern.  What is unusual is
  439. that typing CTRL-V CTRL-J also inserts a <NL>, thus also searches for a <Nul>
  440. in the file.  {Vi cannot handle <Nul> characters in the file at all}
  441.  
  442.  vim:tw=78:ts=8:sw=8:
  443.