neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

usr_27.txt (17740B)


      1 *usr_27.txt*	Nvim
      2 
      3 
      4 	     VIM USER MANUAL	by Bram Moolenaar
      5 
      6 
      7 		 Search commands and patterns
      8 
      9 
     10 In chapter 3 a few simple search patterns were mentioned |03.9|.  Vim can do
     11 much more complex searches.  This chapter explains the most often used ones.
     12 A detailed specification can be found here: |pattern|
     13 Options affecting how search is done can be found here: |search-options|
     14 
     15 |27.1|	Ignoring case
     16 |27.2|	Wrapping around the file end
     17 |27.3|	Offsets
     18 |27.4|	Matching multiple times
     19 |27.5|	Alternatives
     20 |27.6|	Character ranges
     21 |27.7|	Character classes
     22 |27.8|	Matching a line break
     23 |27.9|	Examples
     24 
     25     Next chapter: |usr_28.txt|  Folding
     26 Previous chapter: |usr_26.txt|  Repeating
     27 Table of contents: |usr_toc.txt|
     28 
     29 ==============================================================================
     30 *27.1*	Ignoring case
     31 
     32 By default, Vim's searches are case sensitive.  Therefore, "include",
     33 "INCLUDE", and "Include" are three different words and a search will match
     34 only one of them.
     35   Now switch on the 'ignorecase' option: >
     36 
     37 :set ignorecase
     38 
     39 Search for "include" again, and now it will match "Include", "INCLUDE" and
     40 "InClUDe".  (Set the 'hlsearch' option to quickly see where a pattern
     41 matches.)
     42   You can switch this off again with: >
     43 
     44 :set noignorecase
     45 
     46 But let's keep it set, and search for "INCLUDE".  It will match exactly the
     47 same text as "include" did.  Now set the 'smartcase' option: >
     48 
     49 :set ignorecase smartcase
     50 
     51 If you have a pattern with at least one uppercase character, the search
     52 becomes case sensitive.  The idea is that you didn't have to type that
     53 uppercase character, so you must have done it because you wanted case to
     54 match.  That's smart!
     55    With these two options set you find the following matches:
     56 
     57 pattern			matches	~
     58 word			word, Word, WORD, WoRd, etc.
     59 Word			Word
     60 WORD			WORD
     61 WoRd			WoRd
     62 
     63 
     64 CASE IN ONE PATTERN
     65 
     66 If you want to ignore case for one specific pattern, you can do this by
     67 prepending the "\c" string.  Using "\C" will make the pattern to match case.
     68 This overrules the 'ignorecase' and 'smartcase' options, when "\c" or "\C" is
     69 used their value doesn't matter.
     70 
     71 pattern			matches	~
     72 \Cword			word
     73 \CWord			Word
     74 \cword			word, Word, WORD, WoRd, etc.
     75 \cWord			word, Word, WORD, WoRd, etc.
     76 
     77 A big advantage of using "\c" and "\C" is that it sticks with the pattern.
     78 Thus if you repeat a pattern from the search history, the same will happen, no
     79 matter if 'ignorecase' or 'smartcase' was changed.
     80 
     81 Note:
     82 The use of "\" items in search patterns depends on the 'magic' option.
     83 In this chapter we will assume 'magic' is on, because that is the
     84 standard and recommended setting.  If you would change 'magic', many
     85 search patterns would suddenly become invalid.
     86 
     87 Note:
     88 If your search takes much longer than you expected, you can interrupt
     89 it with CTRL-C on Unix and CTRL-Break on MS-Windows.
     90 
     91 ==============================================================================
     92 *27.2*	Wrapping around the file end
     93 
     94 By default, a forward search starts searching for the given string at the
     95 current cursor location.  It then proceeds to the end of the file.  If it has
     96 not found the string by that time, it starts from the beginning and searches
     97 from the start of the file to the cursor location.
     98   Keep in mind that when repeating the "n" command to search for the next
     99 match, you eventually get back to the first match.  If you don't notice this
    100 you keep searching forever!  To give you a hint, Vim displays this message:
    101 
    102 search hit BOTTOM, continuing at TOP ~
    103 
    104 If you use the "?" command, to search in the other direction, you get this
    105 message:
    106 
    107 search hit TOP, continuing at BOTTOM ~
    108 
    109 Still, you don't know when you are back at the first match.  One way to see
    110 this is by switching on the 'ruler' option: >
    111 
    112 :set ruler
    113 
    114 Vim will display the cursor position in the lower righthand corner of the
    115 window (in the status line if there is one).  It looks like this:
    116 
    117 101,29       84% ~
    118 
    119 The first number is the line number of the cursor.  Remember the line number
    120 where you started, so that you can check if you passed this position again.
    121 
    122 
    123 NOT WRAPPING
    124 
    125 To turn off search wrapping, use the following command: >
    126 
    127 :set nowrapscan
    128 
    129 Now when the search hits the end of the file, an error message displays:
    130 
    131 E385: search hit BOTTOM without match for: forever ~
    132 
    133 Thus you can find all matches by going to the start of the file with "gg" and
    134 keep searching until you see this message.
    135   If you search in the other direction, using "?", you get:
    136 
    137 E384: search hit TOP without match for: forever ~
    138 
    139 ==============================================================================
    140 *27.3*	Offsets
    141 
    142 By default, the search command leaves the cursor positioned on the beginning
    143 of the pattern.  You can tell Vim to leave it some other place by specifying
    144 an offset.  For the forward search command "/", the offset is specified by
    145 appending a slash (/) and the offset: >
    146 
    147 /default/2
    148 
    149 This command searches for the pattern "default" and then moves to the
    150 beginning of the second line past the pattern.  Using this command on the
    151 paragraph above, Vim finds the word "default" in the first line.  Then the
    152 cursor is moved two lines down and lands on "an offset".
    153 
    154 If the offset is a simple number, the cursor will be placed at the beginning
    155 of the line that many lines from the match.  The offset number can be positive
    156 or negative.  If it is positive, the cursor moves down that many lines; if
    157 negative, it moves up.
    158 
    159 
    160 CHARACTER OFFSETS
    161 
    162 The "e" offset indicates an offset from the end of the match.  It moves the
    163 cursor onto the last character of the match.  The command: >
    164 
    165 /const/e
    166 
    167 puts the cursor on the "t" of "const".
    168   From that position, adding a number moves forward that many characters.
    169 This command moves to the character just after the match: >
    170 
    171 /const/e+1
    172 
    173 A positive number moves the cursor to the right, a negative number moves it to
    174 the left.  For example: >
    175 
    176 /const/e-1
    177 
    178 moves the cursor to the "s" of "const".
    179 
    180 If the offset begins with "b", the cursor moves to the beginning of the
    181 pattern.  That's not very useful, since leaving out the "b" does the same
    182 thing.  It does get useful when a number is added or subtracted.  The cursor
    183 then goes forward or backward that many characters.  For example: >
    184 
    185 /const/b+2
    186 
    187 Moves the cursor to the beginning of the match and then two characters to the
    188 right.  Thus it lands on the "n".
    189 
    190 
    191 REPEATING
    192 
    193 To repeat searching for the previously used search pattern, but with a
    194 different offset, leave out the pattern: >
    195 
    196 /that
    197 //e
    198 
    199 Is equal to: >
    200 
    201 /that/e
    202 
    203 To repeat with the same offset: >
    204 
    205 /
    206 
    207 "n" does the same thing.  To repeat while removing a previously used offset: >
    208 
    209 //
    210 
    211 
    212 SEARCHING BACKWARDS
    213 
    214 The "?" command uses offsets in the same way, but you must use "?" to separate
    215 the offset from the pattern, instead of "/": >
    216 
    217 ?const?e-2
    218 
    219 The "b" and "e" keep their meaning, they don't change direction with the use
    220 of "?".
    221 
    222 
    223 START POSITION
    224 
    225 When starting a search, it normally starts at the cursor position.  When you
    226 specify a line offset, this can cause trouble.  For example: >
    227 
    228 /const/-2
    229 
    230 This finds the next word "const" and then moves two lines up.  If you
    231 use "n" to search again, Vim could start at the current position and find the
    232 same "const" match.  Then using the offset again, you would be back where you
    233 started.  You would be stuck!
    234   It could be worse: Suppose there is another match with "const" in the next
    235 line.  Then repeating the forward search would find this match and move two
    236 lines up.  Thus you would actually move the cursor back!
    237 
    238 When you specify a character offset, Vim will compensate for this.  Thus the
    239 search starts a few characters forward or backward, so that the same match
    240 isn't found again.
    241 
    242 ==============================================================================
    243 *27.4*	Matching multiple times
    244 
    245 The "*" item specifies that the item before it can match any number of times.
    246 Thus: >
    247 
    248 /a*
    249 
    250 matches "a", "aa", "aaa", etc.  But also "" (the empty string), because zero
    251 times is included.
    252   The "*" only applies to the item directly before it.  Thus "ab*" matches
    253 "a", "ab", "abb", "abbb", etc.  To match a whole string multiple times, it
    254 must be grouped into one item.  This is done by putting "\(" before it and
    255 "\)" after it.  Thus this command: >
    256 
    257 /\(ab\)*
    258 
    259 Matches: "ab", "abab", "ababab", etc.  And also "".
    260 
    261 To avoid matching the empty string, use "\+".  This makes the previous item
    262 match one or more times. >
    263 
    264 /ab\+
    265 
    266 Matches "ab", "abb", "abbb", etc.  It does not match "a" when no "b" follows.
    267 
    268 To match an optional item, use "\=".  Example: >
    269 
    270 /folders\=
    271 
    272 Matches "folder" and "folders".
    273 
    274 
    275 SPECIFIC COUNTS
    276 
    277 To match a specific number of items use the form "\{n,m}".  "n" and "m" are
    278 numbers.  The item before it will be matched "n" to "m" times |inclusive|.
    279 Example: >
    280 
    281 /ab\{3,5}
    282 
    283 matches "abbb", "abbbb" and "abbbbb".
    284  When "n" is omitted, it defaults to zero.  When "m" is omitted it defaults
    285 to infinity.  When ",m" is omitted, it matches exactly "n" times.
    286 Examples:
    287 
    288 pattern		match count ~
    289 \{,4}		0, 1, 2, 3 or 4
    290 \{3,}		3, 4, 5, etc.
    291 \{0,1}		0 or 1, same as \=
    292 \{0,}		0 or more, same as *
    293 \{1,}		1 or more, same as \+
    294 \{3}		3
    295 
    296 
    297 MATCHING AS LITTLE AS POSSIBLE
    298 
    299 The items so far match as many characters as they can find.  To match as few
    300 as possible, use "\{-n,m}".  It works the same as "\{n,m}", except that the
    301 minimal amount possible is used.
    302   For example, use: >
    303 
    304 /ab\{-1,3}
    305 
    306 Will match "ab" in "abbb".  Actually, it will never match more than one b,
    307 because there is no reason to match more.  It requires something else to force
    308 it to match more than the lower limit.
    309   The same rules apply to removing "n" and "m".  It's even possible to remove
    310 both of the numbers, resulting in "\{-}".  This matches the item before it
    311 zero or more times, as few as possible.  The item by itself always matches
    312 zero times.  It is useful when combined with something else.  Example: >
    313 
    314 /a.\{-}b
    315 
    316 This matches "axb" in "axbxb".  If this pattern would be used: >
    317 
    318 /a.*b
    319 
    320 It would try to match as many characters as possible with `.*`, thus it
    321 matches "axbxb" as a whole.
    322 
    323 ==============================================================================
    324 *27.5*	Alternatives
    325 
    326 The "or" operator in a pattern is "\|".  Example: >
    327 
    328 /foo\|bar
    329 
    330 This matches "foo" or "bar".  More alternatives can be concatenated: >
    331 
    332 /one\|two\|three
    333 
    334 Matches "one", "two" and "three".
    335   To match multiple times, the whole thing must be placed in "\(" and "\)": >
    336 
    337 /\(foo\|bar\)\+
    338 
    339 This matches "foo", "foobar", "foofoo", "barfoobar", etc.
    340   Another example: >
    341 
    342 /end\(if\|while\|for\)
    343 
    344 This matches "endif", "endwhile" and "endfor".
    345 
    346 A related item is "\&".  This requires that both alternatives match in the
    347 same place.  The resulting match uses the last alternative.  Example: >
    348 
    349 /forever\&...
    350 
    351 This matches "for" in "forever".  It will not match "fortuin", for example.
    352 
    353 ==============================================================================
    354 *27.6*	Character ranges
    355 
    356 To match "a", "b" or "c" you could use "/a\|b\|c".  When you want to match all
    357 letters from "a" to "z" this gets very long.  There is a shorter method: >
    358 
    359 /[a-z]
    360 
    361 The [] construct matches a single character.  Inside you specify which
    362 characters to match.  You can include a list of characters, like this: >
    363 
    364 /[0123456789abcdef]
    365 
    366 This will match any of the characters included.  For consecutive characters
    367 you can specify the range.  "0-3" stands for "0123".  "w-z" stands for "wxyz".
    368 Thus the same command as above can be shortened to: >
    369 
    370 /[0-9a-f]
    371 
    372 To match the "-" character itself make it the first or last one in the range.
    373 These special characters are accepted to make it easier to use them inside a
    374 [] range (they can actually be used anywhere in the search pattern):
    375 
    376 \e	<Esc>
    377 \t	<Tab>
    378 \r	<CR>
    379 \b	<BS>
    380 
    381 There are a few more special cases for [] ranges, see |/[]| for the whole
    382 story.
    383 
    384 
    385 COMPLEMENTED RANGE
    386 
    387 To avoid matching a specific character, use "^" at the start of the range.
    388 The [] item then matches everything but the characters included.  Example: >
    389 
    390 /"[^"]*"
    391 <
    392  "	  a double quote
    393   [^"]	  any character that is not a double quote
    394       *	  as many as possible
    395        "  a double quote again
    396 
    397 This matches "foo" and "3!x", including the double quotes.
    398 
    399 
    400 PREDEFINED RANGES
    401 
    402 A number of ranges are used very often.  Vim provides a shortcut for these.
    403 For example: >
    404 
    405 /\a
    406 
    407 Finds alphabetic characters.  This is equal to using "/[a-zA-Z]".  Here are a
    408 few more of these:
    409 
    410 item	matches			equivalent ~
    411 \d	digit			[0-9]
    412 \D	non-digit		[^0-9]
    413 \x	hex digit		[0-9a-fA-F]
    414 \X	non-hex digit		[^0-9a-fA-F]
    415 \s	white space		[ 	]     (<Tab> and <Space>)
    416 \S	non-white characters	[^ 	]     (not <Tab> and <Space>)
    417 \l	lowercase alpha		[a-z]
    418 \L	non-lowercase alpha	[^a-z]
    419 \u	uppercase alpha		[A-Z]
    420 \U	non-uppercase alpha	[^A-Z]
    421 
    422 Note:
    423 Using these predefined ranges works a lot faster than the character
    424 range it stands for.
    425 These items can not be used inside [].  Thus "[\d\l]" does NOT work to
    426 match a digit or lowercase alpha.  Use "\(\d\|\l\)" instead.
    427 
    428 See |/\s| for the whole list of these ranges.
    429 
    430 ==============================================================================
    431 *27.7*	Character classes
    432 
    433 The character range matches a fixed set of characters.  A character class is
    434 similar, but with an essential difference: The set of characters can be
    435 redefined without changing the search pattern.
    436   For example, search for this pattern: >
    437 
    438 /\f\+
    439 
    440 The "\f" item stands for file name characters.  Thus this matches a sequence
    441 of characters that can be a file name.
    442   Which characters can be part of a file name depends on the system you are
    443 using.  On MS-Windows, the backslash is included, on Unix it is not.  This is
    444 specified with the 'isfname' option.  The default value for Unix is: >
    445 
    446 :set isfname
    447 isfname=@,48-57,/,.,-,_,+,,,#,$,%,~,=
    448 
    449 For other systems the default value is different.  Thus you can make a search
    450 pattern with "\f" to match a file name, and it will automatically adjust to
    451 the system you are using it on.
    452 
    453 Note:
    454 Actually, Unix allows using just about any character in a file name,
    455 including white space.  Including these characters in 'isfname' would
    456 be theoretically correct.  But it would make it impossible to find the
    457 end of a file name in text.  Thus the default value of 'isfname' is a
    458 compromise.
    459 
    460 The character classes are:
    461 
    462 item	matches				option ~
    463 \i	identifier characters		'isident'
    464 \I	like \i, excluding digits
    465 \k	keyword characters		'iskeyword'
    466 \K	like \k, excluding digits
    467 \p	printable characters		'isprint'
    468 \P	like \p, excluding digits
    469 \f	file name characters		'isfname'
    470 \F	like \f, excluding digits
    471 
    472 ==============================================================================
    473 *27.8*	Matching a line break
    474 
    475 Vim can find a pattern that includes a line break.  You need to specify where
    476 the line break happens, because all items mentioned so far don't match a line
    477 break.
    478   To check for a line break in a specific place, use the "\n" item: >
    479 
    480 /one\ntwo
    481 
    482 This will match at a line that ends in "one" and the next line starts with
    483 "two".  To match "one two" as well, you need to match a space or a line
    484 break.  The item to use for it is `\_s`: >
    485 
    486 /one\_stwo
    487 
    488 To allow any amount of white space: >
    489 
    490 /one\_s\+two
    491 
    492 This also matches when "one  " is at the end of a line and "   two" at the
    493 start of the next one.
    494 
    495 "\s" matches white space, "\_s" matches white space or a line break.
    496 Similarly, "\a" matches an alphabetic character, and "\_a" matches an
    497 alphabetic character or a line break.  The other character classes and ranges
    498 can be modified in the same way by inserting a "_".
    499 
    500 Many other items can be made to match a line break by prepending "\_".  For
    501 example: "\_." matches any character or a line break.
    502 
    503 Note:
    504 `\_.*` matches everything until the end of the file.  Be careful with
    505 this, it can make a search command very slow.
    506 
    507 Another example is `\_[]`, a character range that includes a line break: >
    508 
    509 /"\_[^"]*"
    510 
    511 This finds a text in double quotes that may be split up in several lines.
    512 
    513 ==============================================================================
    514 *27.9*	Examples
    515 
    516 Here are a few search patterns you might find useful.  This shows how the
    517 items mentioned above can be combined.
    518 
    519 
    520 FINDING A CALIFORNIA LICENSE PLATE
    521 
    522 A sample license plate number is "1MGU103".  It has one digit, three uppercase
    523 letters and three digits.  Directly putting this into a search pattern: >
    524 
    525 /\d\u\u\u\d\d\d
    526 
    527 Another way is to specify that there are three digits and letters with a
    528 count: >
    529 
    530 /\d\u\{3}\d\{3}
    531 
    532 Using [] ranges instead: >
    533 
    534 /[0-9][A-Z]\{3}[0-9]\{3}
    535 
    536 Which one of these you should use?  Whichever one you can remember.  The
    537 simple way you can remember is much faster than the fancy way that you can't.
    538 If you can remember them all, then avoid the last one, because it's both more
    539 typing and slower to execute.
    540 
    541 
    542 FINDING AN IDENTIFIER
    543 
    544 In C programs (and many other computer languages) an identifier starts with a
    545 letter and further consists of letters and digits.  Underscores can be used
    546 too.  This can be found with: >
    547 
    548 /\<\h\w*\>
    549 
    550 "\<" and "\>" are used to find only whole words.  "\h" stands for "[A-Za-z_]"
    551 and "\w" for "[0-9A-Za-z_]".
    552 
    553 Note:
    554 "\<" and "\>" depend on the 'iskeyword' option.  If it includes "-",
    555 for example, then "ident-" is not matched.  In this situation use: >
    556 
    557 	/\w\@<!\h\w*\w\@!
    558 <
    559 This checks if "\w" does not match before or after the identifier.
    560 See |/\@<!| and |/\@!|.
    561 
    562 ==============================================================================
    563 
    564 Next chapter: |usr_28.txt|  Folding
    565 
    566 Copyright: see |manual-copyright|  vim:tw=78:ts=8:noet:ft=help:norl: