1262 lines · 51.1 KB · Text
RawDownload
*pattern.txt*   For Vim version 7.1.  Last change: 2007 May 11  		  VIM REFERENCE MANUAL    by Bram Moolenaar  Patterns and search commands				*pattern-searches* The very basics can be found in section |03.9| of the user manual.  A few moreexplanations are in chapter 27 |usr_27.txt|. 1. Search commands		|search-commands|2. The definition of a pattern	|search-pattern|3. Magic			|/magic|4. Overview of pattern items	|pattern-overview|5. Multi items			|pattern-multi-items|6. Ordinary atoms		|pattern-atoms|7. Ignoring case in a pattern	|/ignorecase|8. Composing characters		|patterns-composing|9. Compare with Perl patterns	|perl-patterns|10. Highlighting matches	|match-highlight| ==============================================================================1. Search commands				*search-commands* *E486* 							*/*/{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 |last-pattern| with latest used |{offset}|. //{offset}<CR>		Search forward for the [count]'th latest used			pattern |last-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 |last-pattern| with latest used |{offset}|. ??{offset}<CR>		Search backward for the [count]'th latest used			pattern |last-pattern| with new |{offset}|.  If			{offset} is empty no offset is used. 							*n*n			Repeat the latest "/" or "?" [count] times.			|last-pattern| {Vi: no count} 							*N*N			Repeat the latest "/" or "?" [count] times in			opposite direction. |last-pattern| {Vi: no count} 							*star* *E348* *E349**			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}			'ignorecase' is used, 'smartcase' is not. 							*#*#			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 Vim searches for the start of the current			function, just like "[[".  If it is not found the			search stops in line 1.  If it is found, Vim goes back			until a blank line is found.  From this position Vim			searches 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|.			After this command |n| searches forward for the next			match (not backward).			{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} 							*1gd*1gd			Like "gd", but ignore matches inside a {} block that			ends before the cursor position. {not in Vi} 							*1gD*1gD			Like "gD", but ignore matches inside a {} block that			ends before the cursor position. {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. 							*:noh* *:nohlsearch*:noh[lsearch]		Stop the highlighting for the 'hlsearch' option.  It			is automatically turned back on when using a search			command, or setting the 'hlsearch' option.			This command doesn't work in an autocommand, because			the highlighting state is saved and restored when			executing autocommands |autocmd-searchpat|. 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 searchcommand with <CR> to actually position the cursor at the displayed match.  Oruse <Esc> to abandon the search. All matches for the last used search pattern will be highlighted if you setthe 'hlsearch' option.  This can be suspended with the |:nohlsearch| command. 					*search-offset* *{offset}*These commands search for the specified pattern.  With "/" and "?" anadditional offset may be given.  There are two types of offsets: line offsetsand 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] identical to s[+num] above (mnemonic: begin)    b[-num]	[num] identical to s[-num] above (mnemonic: begin)    ;{pattern}  perform another search, see |//;| 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 (thecharacter 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 betweenthe cursor position before and after the search is affected.  However, if aline offset is given, the whole lines between the two cursor positions areaffected. An example of how to search for matches with a pattern and change the matchwith another word: >	/foo<CR>	find "foo"	c//e		change until end of match	bar<Esc>	type replacement	//<CR>		go to start of next match	c//e		change until end of match	beep<Esc>	type another replacement			etc.<							*//;* *E386*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 occurrence of "test 1", and then the firstoccurrence 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. 							*last-pattern*The last used pattern and offset are remembered.  They can be used to repeatthe search, possibly in another direction or with another count.  Note thattwo patterns are remembered: One for 'normal' search commands and one for thesubstitute command ":s".  Each time an empty pattern is given, the previouslyused pattern is used. The 'magic' option sticks with the last used pattern.  If you change 'magic',this will not change how the last used pattern will be interpreted.The 'ignorecase' option does not do this.  When 'ignorecase' is changed, itwill result in the pattern to match other text. All matches for the last used search pattern will be highlighted if you setthe 'hlsearch' option. To clear the last used search pattern: >	:let @/ = ""This will not set the pattern to an empty string, because that would matcheverywhere.  The pattern is really cleared, like when starting Vim. The search usually skips matches that don't move the cursor.  Whether the nextmatch is found at the next character or after the skipped match depends on the'c' flag in 'cpoptions'.  See |cpo-c|.	   with 'c' flag:   "/..." advances 1 to 3 characters	without 'c' flag:   "/..." advances 1 characterThe unpredictability with the 'c' flag is caused by starting the search in thefirst column, skipping matches until one is found past the cursor position. When searching backwards, searching starts at the start of the line, using the'c' flag in 'cpoptions' as described above.  Then the last match before thecursor position is used. In Vi the ":tag" command sets the last search pattern when the tag is searchedfor.  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 alwaysput in the search history. If the 'wrapscan' option is on (which is the default), searches wrap aroundthe end of the buffer.  If 'wrapscan' is not set, the backward search stopsat 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 "patternnot found" is given, and the cursor will not be moved.  If 'wrapscan' is notset the message becomes "search hit BOTTOM without match" when searchingforward, or "search hit TOP without match" when searching backward.  Ifwrapscan 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 atTOP" is given when searching backwards or forwards respectively.  This can beswitched off by setting the 's' flag in the 'shortmess' option.  The highlightmethod 'w' is used for this message (default: standout). 							*search-range*You can limit the search command "/" to a certain range of lines by including\%>l items.  For example, to match the word "limit" below line 199 and aboveline 300: >	/\%>199l\%<300llimitAlso see |/\%>l|. Another way is to use the ":substitute" command with the 'c' flag.  Example: >   :.,300s/Pattern//gcThis 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' tostop at this match, type 'n' to find the next match. The "*", "#", "g*" and "g#" commands look for a word near the cursor in thisorder, 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) andthe "*" is under your right hand middle finger (search to the right and down).(this depends on your keyboard layout though). ==============================================================================2. The definition of a pattern		*search-pattern* *pattern* *[pattern]*					*regular-expression* *regexp* *Pattern*					*E76* *E383* *E476* For starters, read chapter 27 of the user manual |usr_27.txt|. 						*/bar* */\bar* */pattern*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   matches "beep".  If more than one branch matches, the first one is used.    pattern ::=	    branch		or  branch \| branch		or  branch \| branch \| branch		etc. 						*/branch* */\&*2. A branch is one or more concats, separated by "\&".  It matches the last   concat, but only if all the preceding concats also match at the same   position.  Examples:	"foobeep\&..." matches "foo" in "foobeep".	".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"    branch ::=	    concat		or  concat \& concat		or  concat \& concat \& concat		etc. 						*/concat*3. A concat is one or more pieces, concatenated.  It matches a match for the   first piece, followed by a match for the second piece, etc.  Example:   "f[0-9]b", first matches "f", then a digit and then "b".    concat  ::=	    piece		or  piece piece		or  piece piece piece		etc. 						*/piece*4. A piece is an atom, possibly followed by a multi, an indication of how many   times the atom can be matched.  Example: "a*" matches any sequence of "a"   characters: "", "a", "aa", etc.  See |/multi|.    piece   ::=	    atom		or  atom  multi 						*/atom*5. An atom can be one of a long list of items.  Many atoms match one character   in the text.  It is often an ordinary character or a character class.   Braces can be used to make a pattern into an atom.  The "\z(\)" construct   is only for syntax highlighting.    atom    ::=	    ordinary-atom		|/ordinary-atom|		or  \( pattern \)		|/\(|		or  \%( pattern \)		|/\%(|		or  \z( pattern \)		|/\z(|  ==============================================================================3. Magic							*/magic* Some characters in the pattern are taken literally.  They match with the samecharacter in the text.  When preceded with a backslash however, thesecharacters get a special meaning. Other characters have a special meaning without a backslash.  They need to bepreceded with a backslash to match literally. If a character is taken literally or not depends on the 'magic' option and theitems mentioned next.							*/\m* */\M*Use of "\m" makes the pattern after it be interpreted as if 'magic' is set,ignoring the actual value of the 'magic' option.Use of "\M" makes the pattern after it be interpreted as if 'nomagic' is used.							*/\v* */\V*Use of "\v" means that in the pattern after it all ASCII characters except'0'-'9', 'a'-'z', 'A'-'Z' and '_' have a special meaning.  "very magic" Use of "\V" means that in the pattern after it only the backslash has aspecial meaning.  "very nomagic" Examples:after:	  \v	   \m	    \M	     \V		matches ~		'magic' 'nomagic'	  $	   $	    $	     \$		matches end-of-line	  .	   .	    \.	     \.		matches any character	  *	   *	    \*	     \*		any number of the previous atom	  ()	   \(\)     \(\)     \(\)	grouping into an atom	  |	   \|	    \|	     \|		separating alternatives	  \a	   \a	    \a	     \a		alphabetic character	  \\	   \\	    \\	     \\		literal backslash	  \.	   \.	    .	     .		literal dot	  \{	   {	    {	     {		literal '{'	  a	   a	    a	     a		literal 'a' {only Vim supports \m, \M, \v and \V} It is recommended to always keep the 'magic' option at the default setting,which is 'magic'.  This avoids portability problems.  To make a pattern immuneto the 'magic' option being set or not, put "\m" or "\M" at the start of thepattern. ==============================================================================4. Overview of pattern items				*pattern-overview* Overview of multi items.				*/multi* *E61* *E62*More explanation and examples below, follow the links.			*E64* 	  multi ~     'magic' 'nomagic'	matches of the preceding atom ~|/star|	*	\*	0 or more	as many as possible|/\+|	\+	\+	1 or more	as many as possible (*)|/\=|	\=	\=	0 or 1		as many as possible (*)|/\?|	\?	\?	0 or 1		as many as possible (*) |/\{|	\{n,m}	\{n,m}	n to m		as many as possible (*)	\{n}	\{n}	n		exactly (*)	\{n,}	\{n,}	at least n	as many as possible (*)	\{,m}	\{,m}	0 to m		as many as possible (*)	\{}	\{}	0 or more	as many as possible (same as *) (*) |/\{-|	\{-n,m}	\{-n,m}	n to m		as few as possible (*)	\{-n}	\{-n}	n		exactly (*)	\{-n,}	\{-n,}	at least n	as few as possible (*)	\{-,m}	\{-,m}	0 to m		as few as possible (*)	\{-}	\{-}	0 or more	as few as possible (*) 							*E59*|/\@>|	\@>	\@>	1, like matching a whole pattern (*)|/\@=|	\@=	\@=	nothing, requires a match |/zero-width| (*)|/\@!|	\@!	\@!	nothing, requires NO match |/zero-width| (*)|/\@<=|	\@<=	\@<=	nothing, requires a match behind |/zero-width| (*)|/\@<!|	\@<!	\@<!	nothing, requires NO match behind |/zero-width| (*) (*) {not in Vi}  Overview of ordinary atoms.				*/ordinary-atom*More explanation and examples below, follow the links.       ordinary atom ~      magic   nomagic	matches ~|/^|	^	^	start-of-line (at start of pattern) |/zero-width||/\^|	\^	\^	literal '^'|/\_^|	\_^	\_^	start-of-line (used anywhere) |/zero-width||/$|	$	$	end-of-line (at end of pattern) |/zero-width||/\$|	\$	\$	literal '$'|/\_$|	\_$	\_$	end-of-line (used anywhere) |/zero-width||/.|	.	\.	any single character (not an end-of-line)|/\_.|	\_.	\_.	any single character or end-of-line|/\<|	\<	\<	beginning of a word |/zero-width||/\>|	\>	\>	end of a word |/zero-width||/\zs|	\zs	\zs	anything, sets start of match|/\ze|	\ze	\ze	anything, sets end of match|/\%^|	\%^	\%^	beginning of file |/zero-width|		*E71*|/\%$|	\%$	\%$	end of file |/zero-width||/\%V|	\%V	\%V	inside Visual area |/zero-width||/\%#|	\%#	\%#	cursor position |/zero-width||/\%'m|	\%'m	\%'m	mark m position |/zero-width||/\%l|	\%23l	\%23l	in line 23 |/zero-width||/\%c|	\%23c	\%23c	in column 23 |/zero-width||/\%v|	\%23v	\%23v	in virtual column 23 |/zero-width| Character classes {not in Vi}:				*/character-classes*|/\i|	\i	\i	identifier character (see 'isident' option)|/\I|	\I	\I	like "\i", but excluding digits|/\k|	\k	\k	keyword character (see 'iskeyword' option)|/\K|	\K	\K	like "\k", but excluding digits|/\f|	\f	\f	file name character (see 'isfname' option)|/\F|	\F	\F	like "\f", but excluding digits|/\p|	\p	\p	printable character (see 'isprint' option)|/\P|	\P	\P	like "\p", but excluding digits|/\s|	\s	\s	whitespace character: <Space> and <Tab>|/\S|	\S	\S	non-whitespace character; opposite of \s|/\d|	\d	\d	digit:				[0-9]|/\D|	\D	\D	non-digit:			[^0-9]|/\x|	\x	\x	hex digit:			[0-9A-Fa-f]|/\X|	\X	\X	non-hex digit:			[^0-9A-Fa-f]|/\o|	\o	\o	octal digit:			[0-7]|/\O|	\O	\O	non-octal digit:		[^0-7]|/\w|	\w	\w	word character:			[0-9A-Za-z_]|/\W|	\W	\W	non-word character:		[^0-9A-Za-z_]|/\h|	\h	\h	head of word character:		[A-Za-z_]|/\H|	\H	\H	non-head of word character:	[^A-Za-z_]|/\a|	\a	\a	alphabetic character:		[A-Za-z]|/\A|	\A	\A	non-alphabetic character:	[^A-Za-z]|/\l|	\l	\l	lowercase character:		[a-z]|/\L|	\L	\L	non-lowercase character:	[^a-z]|/\u|	\u	\u	uppercase character:		[A-Z]|/\U|	\U	\U	non-uppercase character		[^A-Z]|/\_|	\_x	\_x	where x is any of the characters above: character			class with end-of-line included(end of character classes) |/\e|	\e	\e	<Esc>|/\t|	\t	\t	<Tab>|/\r|	\r	\r	<CR>|/\b|	\b	\b	<BS>|/\n|	\n	\n	end-of-line|/~|	~	\~	last given substitute string|/\1|	\1	\1	same string as matched by first \(\) {not in Vi}|/\2|	\2	\2	Like "\1", but uses second \(\)	   ...|/\9|	\9	\9	Like "\1", but uses ninth \(\)								*E68*|/\z1|	\z1	\z1	only for syntax highlighting, see |:syn-ext-match|	   ...|/\z1|	\z9	\z9	only for syntax highlighting, see |:syn-ext-match| 	x	x	a character with no special meaning matches itself |/[]|	[]	\[]	any character specified inside the []|/\%[]| \%[]	\%[]	a sequence of optionally matched atoms |/\c|	\c	\c	ignore case

Lines 1–500 of 1262.