pattern.txt - html version

pattern.txt - html version


*pattern.txt*   For Vim version 5.1.  Last modification: 1998 Apr 05


		  VIM REFERENCE MANUAL    by Bram Moolenaar




Patterns and search commands				*pattern-searches*

1. Search commands		|search-commands|
2. The definition of a pattern	|search-pattern|



1. Search commands					*search-commands*



							*/*
/{pattern}[/]<CR>	Search forward for the [count]'th occurrence of
			{pattern} (exclusive).

/{pattern}/{offset}<CR>	Search forward for the [count]'th occurrence of
			{pattern} and go |{offset}| lines up or down.
			(linewise).



							*/<CR>*
/<CR>			Search forward for the [count]'th latest used
			pattern with latest used |{offset}|.

//{offset}<CR>		Search forward for the [count]'th latest used
			pattern with new |{offset}|.  If {offset} is empty no
			offset is used.



							*?*
?{pattern}[?]<CR>	Search backward for the [count]'th previous
			occurrence of {pattern} (exclusive).

?{pattern}?{offset}<CR>	Search backward for the [count]'th previous
			occurrence of {pattern} and go |{offset}| lines up or
			down (linewise).



							*?<CR>*
?<CR>			Search backward for the [count]'th latest used
			pattern with latest used |{offset}|.

??{offset}<CR>		Search backward for the [count]'th latest used
			pattern with new |{offset}|.  If {offset} is empty no
			offset is used.



							*n*
n			Repeat the latest "/" or "?" [count] times.  {Vi: no
			count}



							*N*
N			Repeat the latest "/" or "?" [count] times in
			opposite direction.  {Vi: no count}



							*star*
*			Search forward for the [count]'th occurrence of the
			word nearest to the cursor.  The word used for the
			search is the first of:
				1. the keyword under the cursor |'iskeyword'|
				2. the first keyword after the cursor, in the
				   current line
				3. the non-blank word under the cursor
				4. the first non-blank word after the cursor,
				   in the current line
			Only whole keywords are searched for, like with the
			command "/\<keyword\>".  (exclusive)  {not in Vi}



							*#*
#			Same as "*", but search backward.  The pound sign
			(character 163) also works.  If the "#" key works as
			backspace, try using "stty erase <BS>" before starting
			Vim (<BS> is CTRL-H or a real backspace).  {not in Vi}



							*gstar*
g*			Like "*", but don't put "\<" and "\>" around the word.
			This makes the search also find matches that are not a
			whole word.  {not in Vi}



							*g#*
g#			Like "#", but don't put "\<" and "\>" around the word.
			This makes the search also find matches that are not a
			whole word.  {not in Vi}



							*gd*
gd			Goto local Declaration.  When the cursor is on a local
			variable, this command will jump to its declaration.
			First a search is made for the end of the previous
			function, just like "[]".  If it is not found the
			search stops in line 1.  From this position a search is
			made for the keyword under the cursor, like with "*",
			but lines that look like a comment are ignored (see
			'comments' option).  Note that this is not guaranteed
			to work, Vim does not really check the syntax, it only
			searches for a match with the keyword.  If included
			files also need to be searched use the commands listed
			in |include-search|.  {not in Vi}



							*gD*
gD			Goto global Declaration.  When the cursor is on a
			global variable that is defined in the file, this
			command will jump to its declaration.  This works just
			like "gd", except that the search for the keyword
			always starts in line 1.  {not in Vi}



							*CTRL-C*
CTRL-C			Interrupt current (search) command.  Use CTRL-Break on
			MS-DOS |dos-CTRL-Break|.
			In Normal mode, any pending command is aborted.

While typing the search pattern the current match will be shown if the
'incsearch' option is on.  Remember that you still have to finish the search
command with <CR> to actually position the cursor at the displayed match.  Or
use <Esc> to abandon the search.

All matches for the last used search pattern will be highlighted if you set
the 'hlsearch' option.



					*search-offset* *{offset}*
These commands search for the specified pattern.  With "/" and "?" an
additional offset may be given.  There are two types of offsets: line offsets
and character offsets.  {the character offsets are not in Vi}

The offset gives the cursor position relative to the found match:
    [num]	[num] lines downwards, in column 1
    +[num]	[num] lines downwards, in column 1
    -[num]	[num] lines upwards, in column 1
    e[+num]	[num] characters to the right of the end of the match
    e[-num]	[num] characters to the left of the end of the match
    s[+num]	[num] characters to the right of the start of the match
    s[-num]	[num] characters to the left of the start of the match
    b[+num]	[num] characters to the right of the start (begin) of the match
    b[-num]	[num] characters to the left of the start (begin) of the match

If a '-' or '+' is given but [num] is omitted, a count of one will be used.
When including an offset with 'e', the search becomes inclusive (the
character the cursor lands on is included in operations).

Examples:

pattern			cursor position	
/test/+1		one line below "test", in column 1
/test/e			on the last t of "test"
/test/s+2		on the 's' of "test"
/test/b-3		three characters before "test"

If one of these commands is used after an operator, the characters between
the cursor position before and after the search is affected.  However, if a
line offset is given, the whole lines between the two cursor positions are
affected.



							*//;*
A very special offset is ';' followed by another search command.  For example:

  /test 1/;/test
  /test.*/+1;?ing?

The first one first finds the next occurence of "test 1", and then the first
occurence of "test" after that.

This is like executing two search commands after each other, except that:
- It can be used as a single motion command after an operator.
- The direction for a following "n" or "N" command comes from the first
  search command.
- When an error occurs the cursor is not moved at all.

The last used pattern and offset are remembered.  They can be used to repeat
the search, possibly in another direction or with another count.  Note that
two patterns are remembered: one for 'normal' search commands and one for the
substitute command ":s".  Each time an empty pattern is given, the previously
used pattern is used.

All matches for the last used search pattern will be highlighted if you set
the 'hlsearch' option.

In Vi the ":tag" command sets the last search pattern when the tag is searched
for.  In Vim this is not done, the previous search pattern is still remembered,
unless the 't' flag is present in 'cpoptions'.  The search pattern is always
put in the search history.

If the 'wrapscan' option is on (which is the default), searches wrap around
the end of the buffer.  If 'wrapscan' is not set, the backward search stops
at the beginning and the forward search stops at the end of the buffer.  If
'wrapscan' is set and the pattern was not found the error message "pattern
not found" is given, and the cursor will not be moved.  If 'wrapscan' is not
set the message becomes "search hit BOTTOM without match" when searching
forward, or "search hit TOP without match" when searching backward.  If
wrapscan is set and the search wraps around the end of the file the message
"search hit TOP, continuing at BOTTOM" or "search hit BOTTOM, continuing at
TOP" is given when searching backwards or forwards respectively.  This can be
switched off by setting the 's' flag in the 'shortmess' option.  The highlight
method 'w' is used for this message (default: standout).



							*search-range*
You cannot limit the search command "/" to a certain range of lines.  A trick
to do this anyway is to use the ":substitute" command with the 'c' flag.
Example:
  :.,300s/Pattern//gc
This command will search from the cursor position until line 300 for
"Pattern".  At the match, you will be asked to type a character.  Type 'q' to
stop at this match, type 'n' to find the next match.

The "*", "#", "g*" and "g#" commands look for a word near the cursor in this
order, the first one that is found is used:
- The keyword currently under the cursor.
- The first keyword to the right of the cursor, in the same line.
- The WORD currently under the cursor.
- The first WORD to the right of the cursor, in the same line.
The keyword may only contain letters and characters in 'iskeyword'.
The WORD may contain any non-blanks (<Tab>s and/or <Space>s).
Note that if you type with ten fingers, the characters are easy to remember:
the "#" is under your left hand middle finger (search to the left and up) and
the "*" is under your right hand middle finger (search to the right and down).



2. The definition of a pattern		*search-pattern* *pattern* *[pattern]*


					*regular-expression* *regexp*

Patterns may contain special characters, depending on the setting of the
'magic' option.



							*/bar* */\bar*
1. A pattern is one or more branches, separated by "\|".  It matches anything
   that matches one of the branches.  Example: "foo\|beep" matches "foo" and
   "beep".  If more than one branch matches, the first one is used.

2. A branch is one or more pieces, concatenated.  It matches a match for the
   first, followed by a match for the second, etc.  Example: "foo[0-9]beep",
   first match "foo", then a digit and then "beep".

3. A piece is an atom, possibly followed by:
      magic   nomagic	


							*/star* */\star*
	*	\*	matches 0 or more of the preceding atom, as much as
			possible


							*/\+*
	\+	\+	matches 1 or more of the preceding atom, as much as
			possible {not in Vi}


							*/\=*
	\=	\=	matches 0 or 1 of the preceding atom, as much as
			possible {not in Vi}


							*/\{*
	\{n,m}  \{n,m}	matches n to m of the preceding atom, as much as
			possible {not in Vi}
	\{n}    \{n}	matches n of the preceding atom {not in Vi}
	\{n,}   \{n,}	matches at least n of the preceding atom, as much as
			possible {not in Vi}
	\{,m}   \{,m}	matches 0 to m of the preceding atom, as much as
			possible {not in Vi}
	\{}     \{}	matches 0 or more of the preceding atom, as much as
			possible (same as *) {not in Vi}


							*/\{-*
	\{-n,m}  \{-n,m} matches n to m of the preceding atom, as few as
			possible {not in Vi}
	\{-n}    \{-n}	matches n of the preceding atom {not in Vi}
	\{-n,}   \{-n,}	matches at least n of the preceding atom, as few as
			possible {not in Vi}
	\{-,m}   \{-,m}	matches 0 to m of the preceding atom, as few as
			possible {not in Vi}
	\{-}     \{-}	matches 0 or more of the preceding atom, as few as
			possible {not in Vi}

		(n and m are decimal numbers between 0 and 32000)

		If a "-" appears immediately after the "{", then a shortest
		match first algorithm is used (see example below).  In
		particular, "\{-}" is the same as "*" but uses the shortest
		match first algorithm.  BUT: A match that starts earlier is
		preferred over a shorter match: "a\{-}b" matches "aaab" in
		"xaaab".

    Examples:
       .*	.\*	matches anything, also empty string
       ^.\+$	^.\+$	matches any non-empty line
       foo\=	foo\=	matches "fo" and "foo"
       ab\{2,3}c	matches "abbc" or "abbbc"
       a\{5}		matches "aaaaa".
       ab\{2,}c		matches "abbc", "abbbc", "abbbbc", etc
       ab\{,3}c		matches "ac", "abc", "abbc" or "abbbc".
       a[bc]\{3}d	matches "abbbd", "abbcd", "acbcd", "acccd", etc.
       a\(bc\)\{1,2}d	matches "abcd" or "abcbcd"
       a[bc]\{-}[cd]	matches "abc" in "abcd"
       a[bc]*[cd]	matches "abcd" in "abcd"


4. An atom can be:
      magic   nomagic	


	^	^	at beginning of pattern, matches start of line	*/^*


	$	$	at end of pattern or in front of "\|",		*/$*
			matches <EOL>


	.	\.	matches any single character		  */.* */\.*


	\<	\<	matches the beginning of a word			*/\<*


	\>	\>	matches the end of a word			*/\>*


	\i	\i	matches any identifier character (see		*/\i*
			'isident' option) {not in Vi}


	\I	\I	like "\i", but excluding digits {not in Vi}	*/\I*


	\k	\k	matches any keyword character (see		*/\k*
			'iskeyword' option) {not in Vi}


	\K	\K	like "\k", but excluding digits {not in Vi}	*/\K*


	\f	\f	matches any file name character (see		*/\f*
			'isfname' option) {not in Vi}


	\F	\F	like "\f", but excluding digits {not in Vi}	*/\F*


	\p	\p	matches any printable character (see		*/\p*
			'isprint' option) {not in Vi}


	\P	\P	like "\p", but excluding digits {not in Vi}	*/\P*


						*whitespace* *white-space*


	\s	\s	matches a whitespace character: <Space> and	*/\s*
			<Tab> {not in Vi}


	\S	\S	matches a non-whitespace character; opposite	*/\S*
			of \s  {not in Vi}


	\e	\e	<Esc>						*/\e*


	\t	\t	<Tab>						*/\t*


	\r	\r	<CR>						*/\r*


	\b	\b	<BS>						*/\b*


	\n	\n	<NL> Not available yet!  Will be used for	*/\n*
			multi-line patterns


	~	\~	matches the last given substitute string    */~* */\~*


	\(\)	\(\)	A pattern enclosed by escaped parentheses      */\(\)*
			(e.g., "\(^a\)") matches that pattern


	\1      \1	Matches the same string that was matched by	*/\1*
			the first sub-expression in \( and \). {not in Vi}
			Example: "\([a-z]\).\1" matches "ata", "ehe", "tot",
			etc.


	\2      \2	Like "\1", but uses second sub-expression,	*/\2*
	   ...


	\9      \9	Like "\1", but uses ninth sub-expression.	*/\9*
	x	x	A single character, with no special meaning,
			matches itself


	\x	\x	A backslash followed by a single character,	*/\*
			with no special meaning, matches the single
			character


	[]	\[]	A range. This is a sequence of characters	*/[]*


			enclosed in "[]" or "\[]".  It matches any	*/\[]*
			single character from the sequence.  E.g., "[xyz]"
			matches any 'x', 'y' or 'z'.
			- If the sequence begins with "^", it matches any
			  single character NOT in the sequence: "[^xyz]"
			  matches anything but 'x', 'y' and 'z'.
			- If two characters in the sequence are separated by
			  '-', this is shorthand for the full list of ASCII
			  characters between them.  E.g., "[0-9]" matches any
			  decimal digit.
			- To include a literal ']', '^', '-' or '\' in the
			  sequence, put a backslash before it: "[xyz\]]",
			  "[\^xyz]", "[xy\-z]" and "[xyz\\]".
			  For ']' you can also make it the first character
			  (following a possible "^"):  "[]xyz]" or "[^]xyz]"
			  {not in Vi}.
			  For '-' you can also make it the first or last
			  character: "[-xyz]", "[^-xyz]" or "[xyz-]".
			  For '\' you can also let it be followed by any
			  character that's not in "^]-\etrb".  "[\xyz]"
			  matches '\', 'x', 'y' and 'z'.  It's better to use
			  "\\" though, future expansions may use other
			  characters after '\'.
			- The following translations are accepted when the 'l'
			  flag is not included in 'cpoptions' {not in Vi}:
				\e	<Esc>
				\t	<Tab>
				\r	<CR>
				\b	<BS>

If the 'ignorecase' option is on, the case of letters is ignored.

It is impossible to have a pattern that contains a line break (Sorry!).

Examples:
^beep(			Probably the start of the C function "beep".

[a-zA-Z]$		Any alphabetic character at the end of a line.

\<\I\i*		or
\(^\|[^a-zA-Z0-9_]\)[a-zA-Z_][a-zA-Z0-9_]*
			A C identifier (will stop in front of it).

\(\.$\|\. \)		A period followed by <EOL> or a space.
			Note that "\(\. \|\.$\)" does not do the same,
			because '$' is not <EOL> in front of '\)'.
			This was done to remain Vi-compatible.

[.!?][])"']*\($\|[ ]\)	A search pattern that finds the end of a sentence,
			with almost the same definition as the ")" command.

Technical detail:
<Nul> characters in the file are stored as <NL> in memory.  In the display
they are shown as "^@".  The translation is done when reading and writing
files.  To match a <Nul> with a search pattern you can just enter CTRL-@ or
"CTRL-V 000".  This is probably just what you expect.  Internally the
character is replaced with a <NL> in the search pattern.  What is unusual is
that typing CTRL-V CTRL-J also inserts a <NL>, thus also searches for a <Nul>
in the file.  {Vi cannot handle <Nul> characters in the file at all}

top - back to help