pattern.txt 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. *pattern.txt* Nvim
  2. VIM REFERENCE MANUAL by Bram Moolenaar
  3. Patterns and search commands *pattern-searches*
  4. The very basics can be found in section |03.9| of the user manual. A few more
  5. explanations are in chapter 27 |usr_27.txt|.
  6. Type |gO| to see the table of contents.
  7. ==============================================================================
  8. 1. Search commands *search-commands*
  9. */*
  10. /{pattern}[/]<CR> Search forward for the [count]'th occurrence of
  11. {pattern} |exclusive|.
  12. /{pattern}/{offset}<CR> Search forward for the [count]'th occurrence of
  13. {pattern} and go |{offset}| lines up or down.
  14. |linewise|.
  15. */<CR>*
  16. /<CR> Search forward for the [count]'th occurrence of the
  17. latest used pattern |last-pattern| with latest used
  18. |{offset}|.
  19. //{offset}<CR> Search forward for the [count]'th occurrence of the
  20. latest used pattern |last-pattern| with new
  21. |{offset}|. If {offset} is empty no offset is used.
  22. *?*
  23. ?{pattern}[?]<CR> Search backward for the [count]'th previous
  24. occurrence of {pattern} |exclusive|.
  25. ?{pattern}?{offset}<CR> Search backward for the [count]'th previous
  26. occurrence of {pattern} and go |{offset}| lines up or
  27. down |linewise|.
  28. *?<CR>*
  29. ?<CR> Search backward for the [count]'th occurrence of the
  30. latest used pattern |last-pattern| with latest used
  31. |{offset}|.
  32. ??{offset}<CR> Search backward for the [count]'th occurrence of the
  33. latest used pattern |last-pattern| with new
  34. |{offset}|. If {offset} is empty no offset is used.
  35. *n*
  36. n Repeat the latest "/" or "?" [count] times.
  37. If the cursor doesn't move the search is repeated with
  38. count + 1.
  39. |last-pattern|
  40. *N*
  41. N Repeat the latest "/" or "?" [count] times in
  42. opposite direction. |last-pattern|
  43. *star* *E348* *E349*
  44. * Search forward for the [count]'th occurrence of the
  45. word nearest to the cursor. The word used for the
  46. search is the first of:
  47. 1. the keyword under the cursor |'iskeyword'|
  48. 2. the first keyword after the cursor, in the
  49. current line
  50. 3. the non-blank word under the cursor
  51. 4. the first non-blank word after the cursor,
  52. in the current line
  53. Only whole keywords are searched for, like with the
  54. command "/\<keyword\>". |exclusive|
  55. 'ignorecase' is used, 'smartcase' is not.
  56. *v_star-default*
  57. {Visual}* In Visual mode, search forward for the current selection.
  58. |default-mappings|
  59. *#*
  60. # Same as "*", but search backward. The pound sign
  61. (character 163) also works. If the "#" key works as
  62. backspace, try using "stty erase <BS>" before starting
  63. Vim (<BS> is CTRL-H or a real backspace).
  64. *v_#-default*
  65. {Visual}# In Visual mode, search backward for the current selection.
  66. |default-mappings|
  67. *gstar*
  68. g* Like "*", but don't put "\<" and "\>" around the word.
  69. This makes the search also find matches that are not a
  70. whole word.
  71. *g#*
  72. g# Like "#", but don't put "\<" and "\>" around the word.
  73. This makes the search also find matches that are not a
  74. whole word.
  75. *gd*
  76. gd Goto local Declaration. When the cursor is on a local
  77. variable, this command will jump to its declaration.
  78. This was made to work for C code, in other languages
  79. it may not work well.
  80. First Vim searches for the start of the current
  81. function, just like "[[". If it is not found the
  82. search stops in line 1. If it is found, Vim goes back
  83. until a blank line is found. From this position Vim
  84. searches for the keyword under the cursor, like with
  85. "*", but lines that look like a comment are ignored
  86. (see 'comments' option).
  87. Note that this is not guaranteed to work, Vim does not
  88. really check the syntax, it only searches for a match
  89. with the keyword. If included files also need to be
  90. searched use the commands listed in |include-search|.
  91. After this command |n| searches forward for the next
  92. match (not backward).
  93. *gD*
  94. gD Goto global Declaration. When the cursor is on a
  95. global variable that is defined in the file, this
  96. command will jump to its declaration. This works just
  97. like "gd", except that the search for the keyword
  98. always starts in line 1.
  99. *1gd*
  100. 1gd Like "gd", but ignore matches inside a {} block that
  101. ends before the cursor position.
  102. *1gD*
  103. 1gD Like "gD", but ignore matches inside a {} block that
  104. ends before the cursor position.
  105. *CTRL-C*
  106. CTRL-C Interrupt current (search) command.
  107. In Normal mode, any pending command is aborted.
  108. *:noh* *:nohlsearch*
  109. :noh[lsearch] Stop the highlighting for the 'hlsearch' option. It
  110. is automatically turned back on when using a search
  111. command, or setting the 'hlsearch' option.
  112. This command doesn't work in an autocommand, because
  113. the highlighting state is saved and restored when
  114. executing autocommands |autocmd-searchpat|.
  115. Same thing for when invoking a user function.
  116. While typing the search pattern the current match will be shown if the
  117. 'incsearch' option is on. Remember that you still have to finish the search
  118. command with <CR> to actually position the cursor at the displayed match. Or
  119. use <Esc> to abandon the search.
  120. *nohlsearch-auto*
  121. All matches for the last used search pattern will be highlighted if you set
  122. the 'hlsearch' option. This can be suspended with the |:nohlsearch| command
  123. or auto suspended with nohlsearch plugin. See |nohlsearch-install|.
  124. When 'shortmess' does not include the "S" flag, Vim will automatically show an
  125. index, on which the cursor is. This can look like this: >
  126. [1/5] Cursor is on first of 5 matches.
  127. [1/>99] Cursor is on first of more than 99 matches.
  128. [>99/>99] Cursor is after 99 match of more than 99 matches.
  129. [?/??] Unknown how many matches exists, generating the
  130. statistics was aborted because of search timeout.
  131. Note: the count does not take offset into account.
  132. When no match is found you get the error: *E486* Pattern not found
  133. Note that for the `:global` command, you get a normal message "Pattern not
  134. found", for Vi compatibility.
  135. For the |:s| command the "e" flag can be used to avoid the error message
  136. |:s_flags|.
  137. *search-offset* *{offset}*
  138. These commands search for the specified pattern. With "/" and "?" an
  139. additional offset may be given. There are two types of offsets: line offsets
  140. and character offsets.
  141. The offset gives the cursor position relative to the found match:
  142. [num] [num] lines downwards, in column 1
  143. +[num] [num] lines downwards, in column 1
  144. -[num] [num] lines upwards, in column 1
  145. e[+num] [num] characters to the right of the end of the match
  146. e[-num] [num] characters to the left of the end of the match
  147. s[+num] [num] characters to the right of the start of the match
  148. s[-num] [num] characters to the left of the start of the match
  149. b[+num] [num] identical to s[+num] above (mnemonic: begin)
  150. b[-num] [num] identical to s[-num] above (mnemonic: begin)
  151. ;{pattern} perform another search, see |//;|
  152. If a '-' or '+' is given but [num] is omitted, a count of one will be used.
  153. When including an offset with 'e', the search becomes inclusive (the
  154. character the cursor lands on is included in operations).
  155. Examples:
  156. pattern cursor position ~
  157. /test/+1 one line below "test", in column 1
  158. /test/e on the last t of "test"
  159. /test/s+2 on the 's' of "test"
  160. /test/b-3 three characters before "test"
  161. If one of these commands is used after an operator, the characters between
  162. the cursor position before and after the search is affected. However, if a
  163. line offset is given, the whole lines between the two cursor positions are
  164. affected.
  165. An example of how to search for matches with a pattern and change the match
  166. with another word: >
  167. /foo<CR> find "foo"
  168. c//e<CR> change until end of match
  169. bar<Esc> type replacement
  170. //<CR> go to start of next match
  171. c//e<CR> change until end of match
  172. beep<Esc> type another replacement
  173. etc.
  174. <
  175. *//;* *E386*
  176. A very special offset is ';' followed by another search command. For example: >
  177. /test 1/;/test
  178. /test.*/+1;?ing?
  179. The first one first finds the next occurrence of "test 1", and then the first
  180. occurrence of "test" after that.
  181. This is like executing two search commands after each other, except that:
  182. - It can be used as a single motion command after an operator.
  183. - The direction for a following "n" or "N" command comes from the first
  184. search command.
  185. - When an error occurs the cursor is not moved at all.
  186. *last-pattern*
  187. The last used pattern and offset are remembered. They can be used to repeat
  188. the search, possibly in another direction or with another count. Note that
  189. two patterns are remembered: One for "normal" search commands and one for the
  190. substitute command ":s". Each time an empty pattern is given, the previously
  191. used pattern is used. However, if there is no previous search command, a
  192. previous substitute pattern is used, if possible.
  193. The 'magic' option sticks with the last used pattern. If you change 'magic',
  194. this will not change how the last used pattern will be interpreted.
  195. The 'ignorecase' option does not do this. When 'ignorecase' is changed, it
  196. will result in the pattern to match other text.
  197. All matches for the last used search pattern will be highlighted if you set
  198. the 'hlsearch' option.
  199. To clear the last used search pattern: >
  200. :let @/ = ""
  201. This will not set the pattern to an empty string, because that would match
  202. everywhere. The pattern is really cleared, like when starting Vim.
  203. The search usually skips matches that don't move the cursor. Whether the next
  204. match is found at the next character or after the skipped match depends on the
  205. 'c' flag in 'cpoptions'. See |cpo-c|.
  206. with 'c' flag: "/..." advances 1 to 3 characters
  207. without 'c' flag: "/..." advances 1 character
  208. The unpredictability with the 'c' flag is caused by starting the search in the
  209. first column, skipping matches until one is found past the cursor position.
  210. When searching backwards, searching starts at the start of the line, using the
  211. 'c' flag in 'cpoptions' as described above. Then the last match before the
  212. cursor position is used.
  213. In Vi the ":tag" command sets the last search pattern when the tag is searched
  214. for. In Vim this is not done, the previous search pattern is still remembered,
  215. unless the 't' flag is present in 'cpoptions'. The search pattern is always
  216. put in the search history.
  217. If the 'wrapscan' option is on (which is the default), searches wrap around
  218. the end of the buffer. If 'wrapscan' is not set, the backward search stops
  219. at the beginning and the forward search stops at the end of the buffer. If
  220. 'wrapscan' is set and the pattern was not found the error message "pattern
  221. not found" is given, and the cursor will not be moved. If 'wrapscan' is not
  222. set the message becomes "search hit BOTTOM without match" when searching
  223. forward, or "search hit TOP without match" when searching backward. If
  224. wrapscan is set and the search wraps around the end of the file the message
  225. "search hit TOP, continuing at BOTTOM" or "search hit BOTTOM, continuing at
  226. TOP" is given when searching backwards or forwards respectively. This can be
  227. switched off by setting the 's' flag in the 'shortmess' option. The highlight
  228. method 'w' is used for this message (default: standout).
  229. *search-range*
  230. You can limit the search command "/" to a certain range of lines by including
  231. \%>l items. For example, to match the word "limit" below line 199 and above
  232. line 300: >
  233. /\%>199l\%<300llimit
  234. Also see |/\%>l|.
  235. Another way is to use the ":substitute" command with the 'c' flag. Example: >
  236. :.,300s/Pattern//gc
  237. This command will search from the cursor position until line 300 for
  238. "Pattern". At the match, you will be asked to type a character. Type 'q' to
  239. stop at this match, type 'n' to find the next match.
  240. The "*", "#", "g*" and "g#" commands look for a word near the cursor in this
  241. order, the first one that is found is used:
  242. - The keyword currently under the cursor.
  243. - The first keyword to the right of the cursor, in the same line.
  244. - The WORD currently under the cursor.
  245. - The first WORD to the right of the cursor, in the same line.
  246. The keyword may only contain letters and characters in 'iskeyword'.
  247. The WORD may contain any non-blanks (<Tab>s and/or <Space>s).
  248. Note that if you type with ten fingers, the characters are easy to remember:
  249. the "#" is under your left hand middle finger (search to the left and up) and
  250. the "*" is under your right hand middle finger (search to the right and down).
  251. (this depends on your keyboard layout though).
  252. *E956*
  253. In very rare cases a regular expression is used recursively. This can happen
  254. when executing a pattern takes a long time and when checking for messages on
  255. channels a callback is invoked that also uses a pattern or an autocommand is
  256. triggered. In most cases this should be fine, but if a pattern is in use when
  257. it's used again it fails. Usually this means there is something wrong with
  258. the pattern.
  259. ==============================================================================
  260. 2. The definition of a pattern *search-pattern* *pattern* *[pattern]*
  261. *regular-expression* *regexp* *Pattern*
  262. *E383* *E476*
  263. For starters, read chapter 27 of the user manual |usr_27.txt|.
  264. */bar* */\bar* */pattern*
  265. 1. A pattern is one or more branches, separated by "\|". It matches anything
  266. that matches one of the branches. Example: "foo\|beep" matches "foo" and
  267. matches "beep". If more than one branch matches, the first one is used.
  268. pattern ::= branch
  269. or branch \| branch
  270. or branch \| branch \| branch
  271. etc.
  272. */branch* */\&*
  273. 2. A branch is one or more concats, separated by "\&". It matches the last
  274. concat, but only if all the preceding concats also match at the same
  275. position. Examples:
  276. "foobeep\&..." matches "foo" in "foobeep".
  277. ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
  278. branch ::= concat
  279. or concat \& concat
  280. or concat \& concat \& concat
  281. etc.
  282. */concat*
  283. 3. A concat is one or more pieces, concatenated. It matches a match for the
  284. first piece, followed by a match for the second piece, etc. Example:
  285. "f[0-9]b", first matches "f", then a digit and then "b".
  286. concat ::= piece
  287. or piece piece
  288. or piece piece piece
  289. etc.
  290. */piece*
  291. 4. A piece is an atom, possibly followed by a multi, an indication of how many
  292. times the atom can be matched. Example: "a*" matches any sequence of "a"
  293. characters: "", "a", "aa", etc. See |/multi|.
  294. piece ::= atom
  295. or atom multi
  296. */atom*
  297. 5. An atom can be one of a long list of items. Many atoms match one character
  298. in the text. It is often an ordinary character or a character class.
  299. Parentheses can be used to make a pattern into an atom. The "\z(\)"
  300. construct is only for syntax highlighting.
  301. atom ::= ordinary-atom |/ordinary-atom|
  302. or \( pattern \) |/\(|
  303. or \%( pattern \) |/\%(|
  304. or \z( pattern \) |/\z(|
  305. */\%#=* *two-engines* *NFA*
  306. Vim includes two regexp engines:
  307. 1. An old, backtracking engine that supports everything.
  308. 2. A new, NFA engine that works much faster on some patterns, possibly slower
  309. on some patterns.
  310. *E1281*
  311. Vim will automatically select the right engine for you. However, if you run
  312. into a problem or want to specifically select one engine or the other, you can
  313. prepend one of the following to the pattern:
  314. \%#=0 Force automatic selection. Only has an effect when
  315. 'regexpengine' has been set to a non-zero value.
  316. \%#=1 Force using the old engine.
  317. \%#=2 Force using the NFA engine.
  318. You can also use the 'regexpengine' option to change the default.
  319. *E864* *E868* *E874* *E875* *E876* *E877* *E878*
  320. If selecting the NFA engine and it runs into something that is not implemented
  321. the pattern will not match. This is only useful when debugging Vim.
  322. ==============================================================================
  323. 3. Magic */magic*
  324. Some characters in the pattern, such as letters, are taken literally. They
  325. match exactly the same character in the text. When preceded with a backslash
  326. however, these characters may get a special meaning. For example, "a" matches
  327. the letter "a", while "\a" matches any alphabetic character.
  328. Other characters have a special meaning without a backslash. They need to be
  329. preceded with a backslash to match literally. For example "." matches any
  330. character while "\." matches a dot.
  331. If a character is taken literally or not depends on the 'magic' option and the
  332. items in the pattern mentioned next. The 'magic' option should always be set,
  333. but it can be switched off for Vi compatibility. We mention the effect of
  334. 'nomagic' here for completeness, but we recommend against using that.
  335. */\m* */\M*
  336. Use of "\m" makes the pattern after it be interpreted as if 'magic' is set,
  337. ignoring the actual value of the 'magic' option.
  338. Use of "\M" makes the pattern after it be interpreted as if 'nomagic' is used.
  339. */\v* */\V*
  340. Use of "\v" means that after it, all ASCII characters except '0'-'9', 'a'-'z',
  341. 'A'-'Z' and '_' have special meaning: "very magic"
  342. Use of "\V" means that after it, only a backslash and the terminating
  343. character (usually / or ?) have special meaning: "very nomagic"
  344. Examples:
  345. after: \v \m \M \V matches ~
  346. 'magic' 'nomagic'
  347. a a a a literal 'a'
  348. \a \a \a \a any alphabetic character
  349. . . \. \. any character
  350. \. \. . . literal dot
  351. $ $ $ \$ end-of-line
  352. * * \* \* any number of the previous atom
  353. ~ ~ \~ \~ latest substitute string
  354. () \(\) \(\) \(\) group as an atom
  355. | \| \| \| nothing: separates alternatives
  356. \\ \\ \\ \\ literal backslash
  357. \{ { { { literal curly brace
  358. If you want to you can make a pattern immune to the 'magic' option being set
  359. or not by putting "\m" or "\M" at the start of the pattern.
  360. ==============================================================================
  361. 4. Overview of pattern items *pattern-overview*
  362. *E865* *E866* *E867* *E869*
  363. Overview of multi items. */multi* *E61* *E62*
  364. More explanation and examples below, follow the links. *E64* *E871*
  365. multi ~
  366. 'magic' 'nomagic' matches of the preceding atom ~
  367. |/star| * \* 0 or more as many as possible
  368. |/\+| \+ \+ 1 or more as many as possible
  369. |/\=| \= \= 0 or 1 as many as possible
  370. |/\?| \? \? 0 or 1 as many as possible
  371. |/\{| \{n,m} \{n,m} n to m as many as possible
  372. \{n} \{n} n exactly
  373. \{n,} \{n,} at least n as many as possible
  374. \{,m} \{,m} 0 to m as many as possible
  375. \{} \{} 0 or more as many as possible (same as "*")
  376. |/\{-| \{-n,m} \{-n,m} n to m as few as possible
  377. \{-n} \{-n} n exactly
  378. \{-n,} \{-n,} at least n as few as possible
  379. \{-,m} \{-,m} 0 to m as few as possible
  380. \{-} \{-} 0 or more as few as possible
  381. *E59*
  382. |/\@>| \@> \@> 1, like matching a whole pattern
  383. |/\@=| \@= \@= nothing, requires a match |/zero-width|
  384. |/\@!| \@! \@! nothing, requires NO match |/zero-width|
  385. |/\@<=| \@<= \@<= nothing, requires a match behind |/zero-width|
  386. |/\@<!| \@<! \@<! nothing, requires NO match behind |/zero-width|
  387. Overview of ordinary atoms. */ordinary-atom*
  388. More explanation and examples below, follow the links.
  389. ordinary atom ~
  390. magic nomagic matches ~
  391. |/^| ^ ^ start-of-line (at start of pattern) |/zero-width|
  392. |/\^| \^ \^ literal '^'
  393. |/\_^| \_^ \_^ start-of-line (used anywhere) |/zero-width|
  394. |/$| $ $ end-of-line (at end of pattern) |/zero-width|
  395. |/\$| \$ \$ literal '$'
  396. |/\_$| \_$ \_$ end-of-line (used anywhere) |/zero-width|
  397. |/.| . \. any single character (not an end-of-line)
  398. |/\_.| \_. \_. any single character or end-of-line
  399. |/\<| \< \< beginning of a word |/zero-width|
  400. |/\>| \> \> end of a word |/zero-width|
  401. |/\zs| \zs \zs anything, sets start of match
  402. |/\ze| \ze \ze anything, sets end of match
  403. |/\%^| \%^ \%^ beginning of file |/zero-width| *E71*
  404. |/\%$| \%$ \%$ end of file |/zero-width|
  405. |/\%V| \%V \%V inside Visual area |/zero-width|
  406. |/\%#| \%# \%# cursor position |/zero-width|
  407. |/\%'m| \%'m \%'m mark m position |/zero-width|
  408. |/\%l| \%23l \%23l in line 23 |/zero-width|
  409. |/\%c| \%23c \%23c in column 23 |/zero-width|
  410. |/\%v| \%23v \%23v in virtual column 23 |/zero-width|
  411. Character classes: */character-classes*
  412. magic nomagic matches ~
  413. |/\i| \i \i identifier character (see 'isident' option)
  414. |/\I| \I \I like "\i", but excluding digits
  415. |/\k| \k \k keyword character (see 'iskeyword' option)
  416. |/\K| \K \K like "\k", but excluding digits
  417. |/\f| \f \f file name character (see 'isfname' option)
  418. |/\F| \F \F like "\f", but excluding digits
  419. |/\p| \p \p printable character (see 'isprint' option)
  420. |/\P| \P \P like "\p", but excluding digits
  421. |/\s| \s \s whitespace character: <Space> and <Tab>
  422. |/\S| \S \S non-whitespace character; opposite of \s
  423. |/\d| \d \d digit: [0-9]
  424. |/\D| \D \D non-digit: [^0-9]
  425. |/\x| \x \x hex digit: [0-9A-Fa-f]
  426. |/\X| \X \X non-hex digit: [^0-9A-Fa-f]
  427. |/\o| \o \o octal digit: [0-7]
  428. |/\O| \O \O non-octal digit: [^0-7]
  429. |/\w| \w \w word character: [0-9A-Za-z_]
  430. |/\W| \W \W non-word character: [^0-9A-Za-z_]
  431. |/\h| \h \h head of word character: [A-Za-z_]
  432. |/\H| \H \H non-head of word character: [^A-Za-z_]
  433. |/\a| \a \a alphabetic character: [A-Za-z]
  434. |/\A| \A \A non-alphabetic character: [^A-Za-z]
  435. |/\l| \l \l lowercase character: [a-z]
  436. |/\L| \L \L non-lowercase character: [^a-z]
  437. |/\u| \u \u uppercase character: [A-Z]
  438. |/\U| \U \U non-uppercase character [^A-Z]
  439. |/\_| \_x \_x where x is any of the characters above: character
  440. class with end-of-line included
  441. (end of character classes)
  442. magic nomagic matches ~
  443. |/\e| \e \e <Esc>
  444. |/\t| \t \t <Tab>
  445. |/\r| \r \r <CR>
  446. |/\b| \b \b <BS>
  447. |/\n| \n \n end-of-line
  448. |/~| ~ \~ last given substitute string
  449. |/\1| \1 \1 same string as matched by first \(\)
  450. |/\2| \2 \2 Like "\1", but uses second \(\)
  451. ...
  452. |/\9| \9 \9 Like "\1", but uses ninth \(\)
  453. *E68*
  454. |/\z1| \z1 \z1 only for syntax highlighting, see |:syn-ext-match|
  455. ...
  456. |/\z1| \z9 \z9 only for syntax highlighting, see |:syn-ext-match|
  457. x x a character with no special meaning matches itself
  458. |/[]| [] \[] any character specified inside the []
  459. |/\%[]| \%[] \%[] a sequence of optionally matched atoms
  460. |/\c| \c \c ignore case, do not use the 'ignorecase' option
  461. |/\C| \C \C match case, do not use the 'ignorecase' option
  462. |/\Z| \Z \Z ignore differences in Unicode "combining characters".
  463. Useful when searching voweled Hebrew or Arabic text.
  464. magic nomagic matches ~
  465. |/\m| \m \m 'magic' on for the following chars in the pattern
  466. |/\M| \M \M 'magic' off for the following chars in the pattern
  467. |/\v| \v \v the following chars in the pattern are "very magic"
  468. |/\V| \V \V the following chars in the pattern are "very nomagic"
  469. |/\%#=| \%#=1 \%#=1 select regexp engine |/zero-width|
  470. |/\%d| \%d \%d match specified decimal character (eg \%d123)
  471. |/\%x| \%x \%x match specified hex character (eg \%x2a)
  472. |/\%o| \%o \%o match specified octal character (eg \%o040)
  473. |/\%u| \%u \%u match specified multibyte character (eg \%u20ac)
  474. |/\%U| \%U \%U match specified large multibyte character (eg
  475. \%U12345678)
  476. |/\%C| \%C \%C match any composing characters
  477. Example matches ~
  478. \<\I\i* or
  479. \<\h\w*
  480. \<[a-zA-Z_][a-zA-Z0-9_]*
  481. An identifier (e.g., in a C program).
  482. \(\.$\|\. \) A period followed by <EOL> or a space.
  483. [.!?][])"']*\($\|[ ]\) A search pattern that finds the end of a sentence,
  484. with almost the same definition as the ")" command.
  485. cat\Z Both "cat" and "càt" ("a" followed by 0x0300)
  486. Does not match "càt" (character 0x00e0), even
  487. though it may look the same.
  488. ==============================================================================
  489. 5. Multi items *pattern-multi-items*
  490. An atom can be followed by an indication of how many times the atom can be
  491. matched and in what way. This is called a multi. See |/multi| for an
  492. overview.
  493. */star* */\star*
  494. * (use \* when 'magic' is not set)
  495. Matches 0 or more of the preceding atom, as many as possible.
  496. Example 'nomagic' matches ~
  497. a* a\* "", "a", "aa", "aaa", etc.
  498. .* \.\* anything, also an empty string, no end-of-line
  499. \_.* \_.\* everything up to the end of the buffer
  500. \_.*END \_.\*END everything up to and including the last "END"
  501. in the buffer
  502. Exception: When "*" is used at the start of the pattern or just after
  503. "^" it matches the star character.
  504. Be aware that repeating "\_." can match a lot of text and take a long
  505. time. For example, "\_.*END" matches all text from the current
  506. position to the last occurrence of "END" in the file. Since the "*"
  507. will match as many as possible, this first skips over all lines until
  508. the end of the file and then tries matching "END", backing up one
  509. character at a time.
  510. */\+*
  511. \+ Matches 1 or more of the preceding atom, as many as possible.
  512. Example matches ~
  513. ^.\+$ any non-empty line
  514. \s\+ white space of at least one character
  515. */\=*
  516. \= Matches 0 or 1 of the preceding atom, as many as possible.
  517. Example matches ~
  518. foo\= "fo" and "foo"
  519. */\?*
  520. \? Just like \=. Cannot be used when searching backwards with the "?"
  521. command.
  522. */\{* *E60* *E554* *E870*
  523. \{n,m} Matches n to m of the preceding atom, as many as possible
  524. \{n} Matches n of the preceding atom
  525. \{n,} Matches at least n of the preceding atom, as many as possible
  526. \{,m} Matches 0 to m of the preceding atom, as many as possible
  527. \{} Matches 0 or more of the preceding atom, as many as possible (like "*")
  528. */\{-*
  529. \{-n,m} matches n to m of the preceding atom, as few as possible
  530. \{-n} matches n of the preceding atom
  531. \{-n,} matches at least n of the preceding atom, as few as possible
  532. \{-,m} matches 0 to m of the preceding atom, as few as possible
  533. \{-} matches 0 or more of the preceding atom, as few as possible
  534. n and m are positive decimal numbers or zero
  535. *non-greedy*
  536. If a "-" appears immediately after the "{", then a shortest match
  537. first algorithm is used (see example below). In particular, "\{-}" is
  538. the same as "*" but uses the shortest match first algorithm. BUT: A
  539. match that starts earlier is preferred over a shorter match: "a\{-}b"
  540. matches "aaab" in "xaaab".
  541. Example matches ~
  542. ab\{2,3}c "abbc" or "abbbc"
  543. a\{5} "aaaaa"
  544. ab\{2,}c "abbc", "abbbc", "abbbbc", etc.
  545. ab\{,3}c "ac", "abc", "abbc" or "abbbc"
  546. a[bc]\{3}d "abbbd", "abbcd", "acbcd", "acccd", etc.
  547. a\(bc\)\{1,2}d "abcd" or "abcbcd"
  548. a[bc]\{-}[cd] "abc" in "abcd"
  549. a[bc]*[cd] "abcd" in "abcd"
  550. The } may optionally be preceded with a backslash: \{n,m\}.
  551. */\@=*
  552. \@= Matches the preceding atom with zero width.
  553. Like "(?=pattern)" in Perl.
  554. Example matches ~
  555. foo\(bar\)\@= "foo" in "foobar"
  556. foo\(bar\)\@=foo nothing
  557. */zero-width*
  558. When using "\@=" (or "^", "$", "\<", "\>") no characters are included
  559. in the match. These items are only used to check if a match can be
  560. made. This can be tricky, because a match with following items will
  561. be done in the same position. The last example above will not match
  562. "foobarfoo", because it tries match "foo" in the same position where
  563. "bar" matched.
  564. Note that using "\&" works the same as using "\@=": "foo\&.." is the
  565. same as "\(foo\)\@=..". But using "\&" is easier, you don't need the
  566. parentheses.
  567. */\@!*
  568. \@! Matches with zero width if the preceding atom does NOT match at the
  569. current position. |/zero-width|
  570. Like "(?!pattern)" in Perl.
  571. Example matches ~
  572. foo\(bar\)\@! any "foo" not followed by "bar"
  573. a.\{-}p\@! "a", "ap", "app", "appp", etc. not immediately
  574. followed by a "p"
  575. if \(\(then\)\@!.\)*$ "if " not followed by "then"
  576. Using "\@!" is tricky, because there are many places where a pattern
  577. does not match. "a.*p\@!" will match from an "a" to the end of the
  578. line, because ".*" can match all characters in the line and the "p"
  579. doesn't match at the end of the line. "a.\{-}p\@!" will match any
  580. "a", "ap", "app", etc. that isn't followed by a "p", because the "."
  581. can match a "p" and "p\@!" doesn't match after that.
  582. You can't use "\@!" to look for a non-match before the matching
  583. position: "\(foo\)\@!bar" will match "bar" in "foobar", because at the
  584. position where "bar" matches, "foo" does not match. To avoid matching
  585. "foobar" you could use "\(foo\)\@!...bar", but that doesn't match a
  586. bar at the start of a line. Use "\(foo\)\@<!bar".
  587. Useful example: to find "foo" in a line that does not contain "bar": >
  588. /^\%(.*bar\)\@!.*\zsfoo
  589. < This pattern first checks that there is not a single position in the
  590. line where "bar" matches. If ".*bar" matches somewhere the \@! will
  591. reject the pattern. When there is no match any "foo" will be found.
  592. The "\zs" is to have the match start just before "foo".
  593. */\@<=*
  594. \@<= Matches with zero width if the preceding atom matches just before what
  595. follows. |/zero-width|
  596. Like "(?<=pattern)" in Perl, but Vim allows non-fixed-width patterns.
  597. Example matches ~
  598. \(an\_s\+\)\@<=file "file" after "an" and white space or an
  599. end-of-line
  600. For speed it's often much better to avoid this multi. Try using "\zs"
  601. instead |/\zs|. To match the same as the above example:
  602. an\_s\+\zsfile
  603. At least set a limit for the look-behind, see below.
  604. "\@<=" and "\@<!" check for matches just before what follows.
  605. Theoretically these matches could start anywhere before this position.
  606. But to limit the time needed, only the line where what follows matches
  607. is searched, and one line before that (if there is one). This should
  608. be sufficient to match most things and not be too slow.
  609. In the old regexp engine the part of the pattern after "\@<=" and
  610. "\@<!" are checked for a match first, thus things like "\1" don't work
  611. to reference \(\) inside the preceding atom. It does work the other
  612. way around:
  613. Bad example matches ~
  614. \%#=1\1\@<=,\([a-z]\+\) ",abc" in "abc,abc"
  615. However, the new regexp engine works differently, it is better to not
  616. rely on this behavior, do not use \@<= if it can be avoided:
  617. Example matches ~
  618. \([a-z]\+\)\zs,\1 ",abc" in "abc,abc"
  619. \@123<=
  620. Like "\@<=" but only look back 123 bytes. This avoids trying lots
  621. of matches that are known to fail and make executing the pattern very
  622. slow. Example, check if there is a "<" just before "span":
  623. /<\@1<=span
  624. This will try matching "<" only one byte before "span", which is the
  625. only place that works anyway.
  626. After crossing a line boundary, the limit is relative to the end of
  627. the line. Thus the characters at the start of the line with the match
  628. are not counted (this is just to keep it simple).
  629. The number zero is the same as no limit.
  630. */\@<!*
  631. \@<! Matches with zero width if the preceding atom does NOT match just
  632. before what follows. Thus this matches if there is no position in the
  633. current or previous line where the atom matches such that it ends just
  634. before what follows. |/zero-width|
  635. Like "(?<!pattern)" in Perl, but Vim allows non-fixed-width patterns.
  636. The match with the preceding atom is made to end just before the match
  637. with what follows, thus an atom that ends in ".*" will work.
  638. Warning: This can be slow (because many positions need to be checked
  639. for a match). Use a limit if you can, see below.
  640. Example matches ~
  641. \(foo\)\@<!bar any "bar" that's not in "foobar"
  642. \(\/\/.*\)\@<!in "in" which is not after "//"
  643. \@123<!
  644. Like "\@<!" but only look back 123 bytes. This avoids trying lots of
  645. matches that are known to fail and make executing the pattern very
  646. slow.
  647. */\@>*
  648. \@> Matches the preceding atom like matching a whole pattern.
  649. Like "(?>pattern)" in Perl.
  650. Example matches ~
  651. \(a*\)\@>a nothing (the "a*" takes all the "a"'s, there can't be
  652. another one following)
  653. This matches the preceding atom as if it was a pattern by itself. If
  654. it doesn't match, there is no retry with shorter sub-matches or
  655. anything. Observe this difference: "a*b" and "a*ab" both match
  656. "aaab", but in the second case the "a*" matches only the first two
  657. "a"s. "\(a*\)\@>ab" will not match "aaab", because the "a*" matches
  658. the "aaa" (as many "a"s as possible), thus the "ab" can't match.
  659. ==============================================================================
  660. 6. Ordinary atoms *pattern-atoms*
  661. An ordinary atom can be:
  662. */^*
  663. ^ At beginning of pattern or after "\|", "\(", "\%(" or "\n": matches
  664. start-of-line; at other positions, matches literal '^'. |/zero-width|
  665. Example matches ~
  666. ^beep( the start of the C function "beep" (probably).
  667. */\^*
  668. \^ Matches literal '^'. Can be used at any position in the pattern, but
  669. not inside [].
  670. */\_^*
  671. \_^ Matches start-of-line. |/zero-width| Can be used at any position in
  672. the pattern, but not inside [].
  673. Example matches ~
  674. \_s*\_^foo white space and blank lines and then "foo" at
  675. start-of-line
  676. */$*
  677. $ At end of pattern or in front of "\|", "\)" or "\n" ('magic' on):
  678. matches end-of-line <EOL>; at other positions, matches literal '$'.
  679. |/zero-width|
  680. */\$*
  681. \$ Matches literal '$'. Can be used at any position in the pattern, but
  682. not inside [].
  683. */\_$*
  684. \_$ Matches end-of-line. |/zero-width| Can be used at any position in the
  685. pattern, but not inside []. Note that "a\_$b" never matches, since
  686. "b" cannot match an end-of-line. Use "a\nb" instead |/\n|.
  687. Example matches ~
  688. foo\_$\_s* "foo" at end-of-line and following white space and
  689. blank lines
  690. . (with 'nomagic': \.) */.* */\.*
  691. Matches any single character, but not an end-of-line.
  692. */\_.*
  693. \_. Matches any single character or end-of-line.
  694. Careful: "\_.*" matches all text to the end of the buffer!
  695. */\<*
  696. \< Matches the beginning of a word: The next char is the first char of a
  697. word. The 'iskeyword' option specifies what is a word character.
  698. |/zero-width|
  699. */\>*
  700. \> Matches the end of a word: The previous char is the last char of a
  701. word. The 'iskeyword' option specifies what is a word character.
  702. |/zero-width|
  703. */\zs*
  704. \zs Matches at any position, but not inside [], and sets the start of the
  705. match there: The next char is the first char of the whole match.
  706. |/zero-width|
  707. Example: >
  708. /^\s*\zsif
  709. < matches an "if" at the start of a line, ignoring white space.
  710. Can be used multiple times, the last one encountered in a matching
  711. branch is used. Example: >
  712. /\(.\{-}\zsFab\)\{3}
  713. < Finds the third occurrence of "Fab".
  714. This cannot be followed by a multi. *E888*
  715. */\ze*
  716. \ze Matches at any position, but not inside [], and sets the end of the
  717. match there: The previous char is the last char of the whole match.
  718. |/zero-width|
  719. Can be used multiple times, the last one encountered in a matching
  720. branch is used.
  721. Example: "end\ze\(if\|for\)" matches the "end" in "endif" and
  722. "endfor".
  723. This cannot be followed by a multi. |E888|
  724. */\%^* *start-of-file*
  725. \%^ Matches start of the file. When matching with a string, matches the
  726. start of the string.
  727. For example, to find the first "VIM" in a file: >
  728. /\%^\_.\{-}\zsVIM
  729. <
  730. */\%$* *end-of-file*
  731. \%$ Matches end of the file. When matching with a string, matches the
  732. end of the string.
  733. Note that this does NOT find the last "VIM" in a file: >
  734. /VIM\_.\{-}\%$
  735. < It will find the next VIM, because the part after it will always
  736. match. This one will find the last "VIM" in the file: >
  737. /VIM\ze\(\(VIM\)\@!\_.\)*\%$
  738. < This uses |/\@!| to ascertain that "VIM" does NOT match in any
  739. position after the first "VIM".
  740. Searching from the end of the file backwards is easier!
  741. */\%V*
  742. \%V Match inside the Visual area. When Visual mode has already been
  743. stopped match in the area that |gv| would reselect.
  744. This is a |/zero-width| match. To make sure the whole pattern is
  745. inside the Visual area put it at the start and just before the end of
  746. the pattern, e.g.: >
  747. /\%Vfoo.*ba\%Vr
  748. < This also works if only "foo bar" was Visually selected. This: >
  749. /\%Vfoo.*bar\%V
  750. < would match "foo bar" if the Visual selection continues after the "r".
  751. Only works for the current buffer.
  752. */\%#* *cursor-position*
  753. \%# Matches with the cursor position. Only works when matching in a
  754. buffer displayed in a window.
  755. WARNING: When the cursor is moved after the pattern was used, the
  756. result becomes invalid. Vim doesn't automatically update the matches.
  757. This is especially relevant for syntax highlighting and 'hlsearch'.
  758. In other words: When the cursor moves the display isn't updated for
  759. this change. An update is done for lines which are changed (the whole
  760. line is updated) or when using the |CTRL-L| command (the whole screen
  761. is updated). Example, to highlight the word under the cursor: >
  762. /\k*\%#\k*
  763. < When 'hlsearch' is set and you move the cursor around and make changes
  764. this will clearly show when the match is updated or not.
  765. */\%'m* */\%<'m* */\%>'m*
  766. \%'m Matches with the position of mark m.
  767. \%<'m Matches before the position of mark m.
  768. \%>'m Matches after the position of mark m.
  769. Example, to highlight the text from mark 's to 'e: >
  770. /.\%>'s.*\%<'e..
  771. < Note that two dots are required to include mark 'e in the match. That
  772. is because "\%<'e" matches at the character before the 'e mark, and
  773. since it's a |/zero-width| match it doesn't include that character.
  774. WARNING: When the mark is moved after the pattern was used, the result
  775. becomes invalid. Vim doesn't automatically update the matches.
  776. Similar to moving the cursor for "\%#" |/\%#|.
  777. */\%l* */\%>l* */\%<l* *E951* *E1204*
  778. \%23l Matches in a specific line.
  779. \%<23l Matches above a specific line (lower line number).
  780. \%>23l Matches below a specific line (higher line number).
  781. \%.l Matches at the cursor line.
  782. \%<.l Matches above the cursor line.
  783. \%>.l Matches below the cursor line.
  784. These six can be used to match specific lines in a buffer. The "23"
  785. can be any line number. The first line is 1.
  786. WARNING: When inserting or deleting lines Vim does not automatically
  787. update the matches. This means Syntax highlighting quickly becomes
  788. wrong. Also when referring to the cursor position (".") and
  789. the cursor moves the display isn't updated for this change. An update
  790. is done when using the |CTRL-L| command (the whole screen is updated).
  791. Example, to highlight the line where the cursor currently is: >
  792. :exe '/\%' .. line(".") .. 'l'
  793. < Alternatively use: >
  794. /\%.l
  795. < When 'hlsearch' is set and you move the cursor around and make changes
  796. this will clearly show when the match is updated or not.
  797. */\%c* */\%>c* */\%<c*
  798. \%23c Matches in a specific column.
  799. \%<23c Matches before a specific column.
  800. \%>23c Matches after a specific column.
  801. \%.c Matches at the cursor column.
  802. \%<.c Matches before the cursor column.
  803. \%>.c Matches after the cursor column.
  804. These six can be used to match specific columns in a buffer or string.
  805. The "23" can be any column number. The first column is 1. Actually,
  806. the column is the byte number (thus it's not exactly right for
  807. multibyte characters).
  808. WARNING: When inserting or deleting text Vim does not automatically
  809. update the matches. This means Syntax highlighting quickly becomes
  810. wrong. Also when referring to the cursor position (".") and
  811. the cursor moves the display isn't updated for this change. An update
  812. is done when using the |CTRL-L| command (the whole screen is updated).
  813. Example, to highlight the column where the cursor currently is: >
  814. :exe '/\%' .. col(".") .. 'c'
  815. < Alternatively use: >
  816. /\%.c
  817. < When 'hlsearch' is set and you move the cursor around and make changes
  818. this will clearly show when the match is updated or not.
  819. Example for matching a single byte in column 44: >
  820. /\%>43c.\%<46c
  821. < Note that "\%<46c" matches in column 45 when the "." matches a byte in
  822. column 44.
  823. */\%v* */\%>v* */\%<v*
  824. \%23v Matches in a specific virtual column.
  825. \%<23v Matches before a specific virtual column.
  826. \%>23v Matches after a specific virtual column.
  827. \%.v Matches at the current virtual column.
  828. \%<.v Matches before the current virtual column.
  829. \%>.v Matches after the current virtual column.
  830. These six can be used to match specific virtual columns in a buffer or
  831. string. When not matching with a buffer in a window, the option
  832. values of the current window are used (e.g., 'tabstop').
  833. The "23" can be any column number. The first column is 1.
  834. Note that some virtual column positions will never match, because they
  835. are halfway through a tab or other character that occupies more than
  836. one screen character.
  837. WARNING: When inserting or deleting text Vim does not automatically
  838. update highlighted matches. This means Syntax highlighting quickly
  839. becomes wrong. Also when referring to the cursor position (".") and
  840. the cursor moves the display isn't updated for this change. An update
  841. is done when using the |CTRL-L| command (the whole screen is updated).
  842. Example, to highlight all the characters after virtual column 72: >
  843. /\%>72v.*
  844. < When 'hlsearch' is set and you move the cursor around and make changes
  845. this will clearly show when the match is updated or not.
  846. To match the text up to column 17: >
  847. /^.*\%17v
  848. < To match all characters after the current virtual column (where the
  849. cursor is): >
  850. /\%>.v.*
  851. < Column 17 is not included, because this is a |/zero-width| match. To
  852. include the column use: >
  853. /^.*\%17v.
  854. < This command does the same thing, but also matches when there is no
  855. character in column 17: >
  856. /^.*\%<18v.
  857. < Note that without the "^" to anchor the match in the first column,
  858. this will also highlight column 17: >
  859. /.*\%17v
  860. < Column 17 is highlighted by 'hlsearch' because there is another match
  861. where ".*" matches zero characters.
  862. Character classes:
  863. \i identifier character (see 'isident' option) */\i*
  864. \I like "\i", but excluding digits */\I*
  865. \k keyword character (see 'iskeyword' option) */\k*
  866. \K like "\k", but excluding digits */\K*
  867. \f file name character (see 'isfname' option) */\f*
  868. \F like "\f", but excluding digits */\F*
  869. \p printable character (see 'isprint' option) */\p*
  870. \P like "\p", but excluding digits */\P*
  871. NOTE: the above also work for multibyte characters. The ones below only
  872. match ASCII characters, as indicated by the range.
  873. *whitespace* *white-space*
  874. \s whitespace character: <Space> and <Tab> */\s*
  875. \S non-whitespace character; opposite of \s */\S*
  876. \d digit: [0-9] */\d*
  877. \D non-digit: [^0-9] */\D*
  878. \x hex digit: [0-9A-Fa-f] */\x*
  879. \X non-hex digit: [^0-9A-Fa-f] */\X*
  880. \o octal digit: [0-7] */\o*
  881. \O non-octal digit: [^0-7] */\O*
  882. \w word character: [0-9A-Za-z_] */\w*
  883. \W non-word character: [^0-9A-Za-z_] */\W*
  884. \h head of word character: [A-Za-z_] */\h*
  885. \H non-head of word character: [^A-Za-z_] */\H*
  886. \a alphabetic character: [A-Za-z] */\a*
  887. \A non-alphabetic character: [^A-Za-z] */\A*
  888. \l lowercase character: [a-z] */\l*
  889. \L non-lowercase character: [^a-z] */\L*
  890. \u uppercase character: [A-Z] */\u*
  891. \U non-uppercase character: [^A-Z] */\U*
  892. NOTE: Using the atom is faster than the [] form.
  893. NOTE: 'ignorecase', "\c" and "\C" are not used by character classes.
  894. */\_* *E63* */\_i* */\_I* */\_k* */\_K* */\_f* */\_F*
  895. */\_p* */\_P* */\_s* */\_S* */\_d* */\_D* */\_x* */\_X*
  896. */\_o* */\_O* */\_w* */\_W* */\_h* */\_H* */\_a* */\_A*
  897. */\_l* */\_L* */\_u* */\_U*
  898. \_x Where "x" is any of the characters above: The character class with
  899. end-of-line added
  900. (end of character classes)
  901. \e matches <Esc> */\e*
  902. \t matches <Tab> */\t*
  903. \r matches <CR> */\r*
  904. \b matches <BS> */\b*
  905. \n matches an end-of-line */\n*
  906. When matching in a string instead of buffer text a literal newline
  907. character is matched.
  908. ~ matches the last given substitute string */~* */\~*
  909. \(\) A pattern enclosed by escaped parentheses. */\(* */\(\)* */\)*
  910. E.g., "\(^a\)" matches 'a' at the start of a line.
  911. There can only be nine of these. You can use "\%(" to add more, but
  912. not counting it as a sub-expression.
  913. *E51* *E54* *E55* *E872* *E873*
  914. \1 Matches the same string that was matched by */\1* *E65*
  915. the first sub-expression in \( and \).
  916. Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc.
  917. \2 Like "\1", but uses second sub-expression, */\2*
  918. ... */\3*
  919. \9 Like "\1", but uses ninth sub-expression. */\9*
  920. Note: The numbering of groups is done based on which "\(" comes first
  921. in the pattern (going left to right), NOT based on what is matched
  922. first.
  923. \%(\) A pattern enclosed by escaped parentheses. */\%(\)* */\%(* *E53*
  924. Just like \(\), but without counting it as a sub-expression. This
  925. allows using more groups and it's a little bit faster.
  926. x A single character, with no special meaning, matches itself
  927. */\* */\\*
  928. \x A backslash followed by a single character, with no special meaning,
  929. is reserved for future expansions
  930. [] (with 'nomagic': \[]) */[]* */\[]* */\_[]* */collection* *E76*
  931. \_[]
  932. A collection. This is a sequence of characters enclosed in square
  933. brackets. It matches any single character in the collection.
  934. Example matches ~
  935. [xyz] any 'x', 'y' or 'z'
  936. [a-zA-Z]$ any alphabetic character at the end of a line
  937. \c[a-z]$ same
  938. [А-яЁё] Russian alphabet (with utf-8 and cp1251)
  939. */[\n]*
  940. With "\_" prepended the collection also includes the end-of-line.
  941. The same can be done by including "\n" in the collection. The
  942. end-of-line is also matched when the collection starts with "^"! Thus
  943. "\_[^ab]" matches the end-of-line and any character but "a" and "b".
  944. This makes it Vi compatible: Without the "\_" or "\n" the collection
  945. does not match an end-of-line.
  946. *E769*
  947. When the ']' is not there Vim will not give an error message but
  948. assume no collection is used. Useful to search for '['. However, you
  949. do get E769 for internal searching. And be aware that in a
  950. `:substitute` command the whole command becomes the pattern. E.g.
  951. ":s/[/x/" searches for "[/x" and replaces it with nothing. It does
  952. not search for "[" and replaces it with "x"!
  953. *E944* *E945*
  954. If the sequence begins with "^", it matches any single character NOT
  955. in the collection: "[^xyz]" matches anything but 'x', 'y' and 'z'.
  956. - If two characters in the sequence are separated by '-', this is
  957. shorthand for the full list of ASCII characters between them. E.g.,
  958. "[0-9]" matches any decimal digit. If the starting character exceeds
  959. the ending character, e.g. [c-a], E944 occurs. Non-ASCII characters
  960. can be used, but the character values must not be more than 256 apart
  961. in the old regexp engine. For example, searching by [\u3000-\u4000]
  962. after setting re=1 emits a E945 error. Prepending \%#=2 will fix it.
  963. - A character class expression is evaluated to the set of characters
  964. belonging to that character class. The following character classes
  965. are supported:
  966. Name Func Contents ~
  967. *[:alnum:]* [:alnum:] isalnum ASCII letters and digits
  968. *[:alpha:]* [:alpha:] isalpha ASCII letters
  969. *[:blank:]* [:blank:] space and tab
  970. *[:cntrl:]* [:cntrl:] iscntrl ASCII control characters
  971. *[:digit:]* [:digit:] decimal digits '0' to '9'
  972. *[:graph:]* [:graph:] isgraph ASCII printable characters excluding
  973. space
  974. *[:lower:]* [:lower:] (1) lowercase letters (all letters when
  975. 'ignorecase' is used)
  976. *[:print:]* [:print:] (2) printable characters including space
  977. *[:punct:]* [:punct:] ispunct ASCII punctuation characters
  978. *[:space:]* [:space:] whitespace characters: space, tab, CR,
  979. NL, vertical tab, form feed
  980. *[:upper:]* [:upper:] (3) uppercase letters (all letters when
  981. 'ignorecase' is used)
  982. *[:xdigit:]* [:xdigit:] hexadecimal digits: 0-9, a-f, A-F
  983. *[:return:]* [:return:] the <CR> character
  984. *[:tab:]* [:tab:] the <Tab> character
  985. *[:escape:]* [:escape:] the <Esc> character
  986. *[:backspace:]* [:backspace:] the <BS> character
  987. *[:ident:]* [:ident:] identifier character (same as "\i")
  988. *[:keyword:]* [:keyword:] keyword character (same as "\k")
  989. *[:fname:]* [:fname:] file name character (same as "\f")
  990. The square brackets in character class expressions are additional to
  991. the square brackets delimiting a collection. For example, the
  992. following is a plausible pattern for a UNIX filename:
  993. "[-./[:alnum:]_~]\+". That is, a list of at least one character,
  994. each of which is either '-', '.', '/', alphabetic, numeric, '_' or
  995. '~'.
  996. These items only work for 8-bit characters, except [:lower:] and
  997. [:upper:] also work for multibyte characters when using the new
  998. regexp engine. See |two-engines|. In the future these items may
  999. work for multibyte characters. For now, to get all "alpha"
  1000. characters you can use: [[:lower:][:upper:]].
  1001. The "Func" column shows what library function is used. The
  1002. implementation depends on the system. Otherwise:
  1003. (1) Uses islower() for ASCII and Vim builtin rules for other
  1004. characters.
  1005. (2) Uses Vim builtin rules
  1006. (3) As with (1) but using isupper()
  1007. */[[=* *[==]*
  1008. - An equivalence class. This means that characters are matched that
  1009. have almost the same meaning, e.g., when ignoring accents. This
  1010. only works for Unicode, latin1 and latin9. The form is:
  1011. [=a=]
  1012. */[[.* *[..]*
  1013. - A collation element. This currently simply accepts a single
  1014. character in the form:
  1015. [.a.]
  1016. */\]*
  1017. - To include a literal ']', '^', '-' or '\' in the collection, put a
  1018. backslash before it: "[xyz\]]", "[\^xyz]", "[xy\-z]" and "[xyz\\]".
  1019. (Note: POSIX does not support the use of a backslash this way). For
  1020. ']' you can also make it the first character (following a possible
  1021. "^"): "[]xyz]" or "[^]xyz]".
  1022. For '-' you can also make it the first or last character: "[-xyz]",
  1023. "[^-xyz]" or "[xyz-]". For '\' you can also let it be followed by
  1024. any character that's not in "^]-\bdertnoUux". "[\xyz]" matches '\',
  1025. 'x', 'y' and 'z'. It's better to use "\\" though, future expansions
  1026. may use other characters after '\'.
  1027. - Omitting the trailing ] is not considered an error. "[]" works like
  1028. "[]]", it matches the ']' character.
  1029. - The following translations are accepted when the 'l' flag is not
  1030. included in 'cpoptions':
  1031. \e <Esc>
  1032. \t <Tab>
  1033. \r <CR> (NOT end-of-line!)
  1034. \b <BS>
  1035. \n line break, see above |/[\n]|
  1036. \d123 decimal number of character
  1037. \o40 octal number of character up to 0o377
  1038. \x20 hexadecimal number of character up to 0xff
  1039. \u20AC hex. number of multibyte character up to 0xffff
  1040. \U1234 hex. number of multibyte character up to 0xffffffff
  1041. NOTE: The other backslash codes mentioned above do not work inside
  1042. []!
  1043. - Matching with a collection can be slow, because each character in
  1044. the text has to be compared with each character in the collection.
  1045. Use one of the other atoms above when possible. Example: "\d" is
  1046. much faster than "[0-9]" and matches the same characters. However,
  1047. the new |NFA| regexp engine deals with this better than the old one.
  1048. */\%[]* *E69* *E70* *E369*
  1049. \%[] A sequence of optionally matched atoms. This always matches.
  1050. It matches as much of the list of atoms it contains as possible. Thus
  1051. it stops at the first atom that doesn't match. For example: >
  1052. /r\%[ead]
  1053. < matches "r", "re", "rea" or "read". The longest that matches is used.
  1054. To match the Ex command "function", where "fu" is required and
  1055. "nction" is optional, this would work: >
  1056. /\<fu\%[nction]\>
  1057. < The end-of-word atom "\>" is used to avoid matching "fu" in "full".
  1058. It gets more complicated when the atoms are not ordinary characters.
  1059. You don't often have to use it, but it is possible. Example: >
  1060. /\<r\%[[eo]ad]\>
  1061. < Matches the words "r", "re", "ro", "rea", "roa", "read" and "road".
  1062. There can be no \(\), \%(\) or \z(\) items inside the [] and \%[] does
  1063. not nest.
  1064. To include a "[" use "[[]" and for "]" use []]", e.g.,: >
  1065. /index\%[[[]0[]]]
  1066. < matches "index" "index[", "index[0" and "index[0]".
  1067. */\%d* */\%x* */\%o* */\%u* */\%U* *E678*
  1068. \%d123 Matches the character specified with a decimal number. Must be
  1069. followed by a non-digit.
  1070. \%o40 Matches the character specified with an octal number up to 0o377.
  1071. Numbers below 0o40 must be followed by a non-octal digit or a
  1072. non-digit.
  1073. \%x2a Matches the character specified with up to two hexadecimal characters.
  1074. \%u20AC Matches the character specified with up to four hexadecimal
  1075. characters.
  1076. \%U1234abcd Matches the character specified with up to eight hexadecimal
  1077. characters, up to 0x7fffffff
  1078. ==============================================================================
  1079. 7. Ignoring case in a pattern */ignorecase*
  1080. If the 'ignorecase' option is on, the case of normal letters is ignored.
  1081. 'smartcase' can be set to ignore case when the pattern contains lowercase
  1082. letters only.
  1083. */\c* */\C*
  1084. When "\c" appears anywhere in the pattern, the whole pattern is handled like
  1085. 'ignorecase' is on. The actual value of 'ignorecase' and 'smartcase' is
  1086. ignored. "\C" does the opposite: Force matching case for the whole pattern.
  1087. Note that 'ignorecase', "\c" and "\C" are not used for the character classes.
  1088. Examples:
  1089. pattern 'ignorecase' 'smartcase' matches ~
  1090. foo off - foo
  1091. foo on - foo Foo FOO
  1092. Foo on off foo Foo FOO
  1093. Foo on on Foo
  1094. \cfoo - - foo Foo FOO
  1095. foo\C - - foo
  1096. Technical detail: *NL-used-for-Nul*
  1097. <Nul> characters in the file are stored as <NL> in memory. In the display
  1098. they are shown as "^@". The translation is done when reading and writing
  1099. files. To match a <Nul> with a search pattern you can just enter CTRL-@ or
  1100. "CTRL-V 000". This is probably just what you expect. Internally the
  1101. character is replaced with a <NL> in the search pattern. What is unusual is
  1102. that typing CTRL-V CTRL-J also inserts a <NL>, thus also searches for a <Nul>
  1103. in the file.
  1104. *CR-used-for-NL*
  1105. When 'fileformat' is "mac", <NL> characters in the file are stored as <CR>
  1106. characters internally. In the text they are shown as "^J". Otherwise this
  1107. works similar to the usage of <NL> for a <Nul>.
  1108. When working with expression evaluation, a <NL> character in the pattern
  1109. matches a <NL> in the string. The use of "\n" (backslash n) to match a <NL>
  1110. doesn't work there, it only works to match text in the buffer.
  1111. *pattern-multi-byte* *pattern-multibyte*
  1112. Patterns will also work with multibyte characters, mostly as you would
  1113. expect. But invalid bytes may cause trouble, a pattern with an invalid byte
  1114. will probably never match.
  1115. ==============================================================================
  1116. 8. Composing characters *patterns-composing*
  1117. */\Z*
  1118. When "\Z" appears anywhere in the pattern, all composing characters are
  1119. ignored. Thus only the base characters need to match, the composing
  1120. characters may be different and the number of composing characters may differ.
  1121. Exception: If the pattern starts with one or more composing characters, these
  1122. must match.
  1123. */\%C*
  1124. Use "\%C" to skip any composing characters. For example, the pattern "a" does
  1125. not match in "càt" (where the a has the composing character 0x0300), but
  1126. "a\%C" does. Note that this does not match "cát" (where the á is character
  1127. 0xe1, it does not have a compositing character). It does match "cat" (where
  1128. the a is just an a).
  1129. When a composing character appears at the start of the pattern or after an
  1130. item that doesn't include the composing character, a match is found at any
  1131. character that includes this composing character.
  1132. When using a dot and a composing character, this works the same as the
  1133. composing character by itself, except that it doesn't matter what comes before
  1134. this.
  1135. The order of composing characters does not matter. Also, the text may have
  1136. more composing characters than the pattern, it still matches. But all
  1137. composing characters in the pattern must be found in the text.
  1138. Suppose B is a base character and x and y are composing characters:
  1139. pattern text match ~
  1140. Bxy Bxy yes (perfect match)
  1141. Bxy Byx yes (order ignored)
  1142. Bxy By no (x missing)
  1143. Bxy Bx no (y missing)
  1144. Bx Bx yes (perfect match)
  1145. Bx By no (x missing)
  1146. Bx Bxy yes (extra y ignored)
  1147. Bx Byx yes (extra y ignored)
  1148. ==============================================================================
  1149. 9. Compare with Perl patterns *perl-patterns*
  1150. Vim's regexes are most similar to Perl's, in terms of what you can do. The
  1151. difference between them is mostly just notation; here's a summary of where
  1152. they differ:
  1153. Capability in Vimspeak in Perlspeak ~
  1154. force case insensitivity \c (?i)
  1155. force case sensitivity \C (?-i)
  1156. backref-less grouping \%(atom\) (?:atom)
  1157. conservative quantifiers \{-n,m} `*?,` +?, ??, {}?
  1158. 0-width match atom\@= (?=atom)
  1159. 0-width non-match atom\@! (?!atom)
  1160. 0-width preceding match atom\@<= (?<=atom)
  1161. 0-width preceding non-match atom\@<! (?<!atom)
  1162. match without retry atom\@> (?>atom)
  1163. Vim and Perl handle newline characters inside a string a bit differently:
  1164. In Perl, ^ and $ only match at the very beginning and end of the text,
  1165. by default, but you can set the 'm' flag, which lets them match at
  1166. embedded newlines as well. You can also set the 's' flag, which causes
  1167. a . to match newlines as well. (Both these flags can be changed inside
  1168. a pattern using the same syntax used for the i flag above, BTW.)
  1169. On the other hand, Vim's ^ and $ always match at embedded newlines, and
  1170. you get two separate atoms, \%^ and \%$, which only match at the very
  1171. start and end of the text, respectively. Vim solves the second problem
  1172. by giving you the \_ "modifier": put it in front of a . or a character
  1173. class, and they will match newlines as well.
  1174. Finally, these constructs are unique to Perl:
  1175. - execution of arbitrary code in the regex: (?{perl code})
  1176. - conditional expressions: (?(condition)true-expr|false-expr)
  1177. ...and these are unique to Vim:
  1178. - changing the magic-ness of a pattern: \v \V \m \M
  1179. (very useful for avoiding backslashitis)
  1180. - sequence of optionally matching atoms: \%[atoms]
  1181. - \& (which is to \| what "and" is to "or"; it forces several branches
  1182. to match at one spot)
  1183. - matching lines/columns by number: \%5l \%5c \%5v
  1184. - setting the start and end of the match: \zs \ze
  1185. ==============================================================================
  1186. 10. Highlighting matches *match-highlight*
  1187. *syntax-vs-match*
  1188. Note that the match highlight mechanism is independent
  1189. of |syntax-highlighting|, which is (usually) a buffer-local
  1190. highlighting, while matching is window-local, both methods
  1191. can be freely mixed. Match highlighting functions give you
  1192. a bit more flexibility in when and how to apply, but are
  1193. typically only used for temporary highlighting, without strict
  1194. rules. Both methods can be used to conceal text.
  1195. Thus the matching functions like |matchadd()| won't consider
  1196. syntax rules and functions like |synconcealed()| and the
  1197. other way around.
  1198. *:mat* *:match*
  1199. :mat[ch] {group} /{pattern}/
  1200. Define a pattern to highlight in the current window. It will
  1201. be highlighted with {group}. Example: >
  1202. :highlight MyGroup ctermbg=green guibg=green
  1203. :match MyGroup /TODO/
  1204. < Instead of // any character can be used to mark the start and
  1205. end of the {pattern}. Watch out for using special characters,
  1206. such as '"' and '|'.
  1207. {group} must exist at the moment this command is executed.
  1208. The {group} highlighting still applies when a character is
  1209. to be highlighted for 'hlsearch', as the highlighting for
  1210. matches is given higher priority than that of 'hlsearch'.
  1211. Syntax highlighting (see 'syntax') is also overruled by
  1212. matches.
  1213. Note that highlighting the last used search pattern with
  1214. 'hlsearch' is used in all windows, while the pattern defined
  1215. with ":match" only exists in the current window. It is kept
  1216. when switching to another buffer.
  1217. 'ignorecase' does not apply, use |/\c| in the pattern to
  1218. ignore case. Otherwise case is not ignored.
  1219. 'redrawtime' defines the maximum time searched for pattern
  1220. matches.
  1221. When matching end-of-line and Vim redraws only part of the
  1222. display you may get unexpected results. That is because Vim
  1223. looks for a match in the line where redrawing starts.
  1224. Also see |matcharg()| and |getmatches()|. The former returns
  1225. the highlight group and pattern of a previous |:match|
  1226. command. The latter returns a list with highlight groups and
  1227. patterns defined by both |matchadd()| and |:match|.
  1228. Highlighting matches using |:match| are limited to three
  1229. matches (aside from |:match|, |:2match| and |:3match| are
  1230. available). |matchadd()| does not have this limitation and in
  1231. addition makes it possible to prioritize matches.
  1232. Another example, which highlights all characters in virtual
  1233. column 72 and more: >
  1234. :highlight rightMargin term=bold ctermfg=blue guifg=blue
  1235. :match rightMargin /.\%>72v/
  1236. < To highlight all character that are in virtual column 7: >
  1237. :highlight col8 ctermbg=grey guibg=grey
  1238. :match col8 /\%<8v.\%>7v/
  1239. < Note the use of two items to also match a character that
  1240. occupies more than one virtual column, such as a TAB.
  1241. :mat[ch]
  1242. :mat[ch] none
  1243. Clear a previously defined match pattern.
  1244. :2mat[ch] {group} /{pattern}/ *:2match*
  1245. :2mat[ch]
  1246. :2mat[ch] none
  1247. :3mat[ch] {group} /{pattern}/ *:3match*
  1248. :3mat[ch]
  1249. :3mat[ch] none
  1250. Just like |:match| above, but set a separate match. Thus
  1251. there can be three matches active at the same time. The match
  1252. with the lowest number has priority if several match at the
  1253. same position. It uses the match id 3.
  1254. The ":3match" command is used by (older Vims) |matchparen|
  1255. plugin. You are suggested to use ":match" for manual matching
  1256. and ":2match" for another plugin or even better make use of
  1257. the more flexible |matchadd()| (and similar) functions instead.
  1258. ==============================================================================
  1259. 11. Fuzzy matching *fuzzy-matching*
  1260. Fuzzy matching refers to matching strings using a non-exact search string.
  1261. Fuzzy matching will match a string, if all the characters in the search string
  1262. are present anywhere in the string in the same order. Case is ignored. In a
  1263. matched string, other characters can be present between two consecutive
  1264. characters in the search string. If the search string has multiple words, then
  1265. each word is matched separately. So the words in the search string can be
  1266. present in any order in a string.
  1267. Fuzzy matching assigns a score for each matched string based on the following
  1268. criteria:
  1269. - The number of sequentially matching characters.
  1270. - The number of characters (distance) between two consecutive matching
  1271. characters.
  1272. - Matches at the beginning of a word
  1273. - Matches at a camel case character (e.g. Case in CamelCase)
  1274. - Matches after a path separator or a hyphen.
  1275. - The number of unmatched characters in a string.
  1276. - A full/exact match is preferred.
  1277. The matching string with the highest score is returned first.
  1278. For example, when you search for the "get pat" string using fuzzy matching, it
  1279. will match the strings "GetPattern", "PatternGet", "getPattern", "patGetter",
  1280. "getSomePattern", "MatchpatternGet" etc.
  1281. The functions |matchfuzzy()| and |matchfuzzypos()| can be used to fuzzy search
  1282. a string in a List of strings. The matchfuzzy() function returns a List of
  1283. matching strings. The matchfuzzypos() functions returns the List of matches,
  1284. the matching positions and the fuzzy match scores.
  1285. The "f" flag of `:vimgrep` enables fuzzy matching.
  1286. To enable fuzzy matching for |ins-completion|, add the "fuzzy" value to the
  1287. 'completeopt' option.
  1288. vim:tw=78:ts=8:noet:ft=help:norl: