pattern.txt 58 KB

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