usr_27.txt 17 KB

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