matchit.txt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. *matchit.txt* Extended "%" matching
  2. For instructions on installing this file, type
  3. `:help matchit-install`
  4. inside Vim.
  5. For Vim version 8.1. Last change: 2020 Mar 01
  6. VIM REFERENCE MANUAL by Benji Fisher et al
  7. *matchit* *matchit.vim*
  8. 1. Extended matching with "%" |matchit-intro|
  9. 2. Activation |matchit-activate|
  10. 3. Configuration |matchit-configure|
  11. 4. Supporting a New Language |matchit-newlang|
  12. 5. Known Bugs and Limitations |matchit-bugs|
  13. The functionality mentioned here is a plugin, see |add-plugin|.
  14. This plugin is only available if 'compatible' is not set.
  15. ==============================================================================
  16. 1. Extended matching with "%" *matchit-intro*
  17. *matchit-%*
  18. % Cycle forward through matching groups, such as "if", "else", "endif",
  19. as specified by |b:match_words|.
  20. *g%* *v_g%* *o_g%*
  21. g% Cycle backwards through matching groups, as specified by
  22. |b:match_words|. For example, go from "if" to "endif" to "else".
  23. *[%* *v_[%* *o_[%*
  24. [% Go to [count] previous unmatched group, as specified by
  25. |b:match_words|. Similar to |[{|.
  26. *]%* *v_]%* *o_]%*
  27. ]% Go to [count] next unmatched group, as specified by
  28. |b:match_words|. Similar to |]}|.
  29. *v_a%*
  30. a% In Visual mode, select the matching group, as specified by
  31. |b:match_words|, containing the cursor. Similar to |v_a[|.
  32. A [count] is ignored, and only the first character of the closing
  33. pattern is selected.
  34. In Vim, as in plain vi, the percent key, |%|, jumps the cursor from a brace,
  35. bracket, or paren to its match. This can be configured with the 'matchpairs'
  36. option. The matchit plugin extends this in several ways:
  37. You can match whole words, such as "if" and "endif", not just
  38. single characters. You can also specify a |regular-expression|.
  39. You can define groups with more than two words, such as "if",
  40. "else", "endif". Banging on the "%" key will cycle from the "if" to
  41. the first "else", the next "else", ..., the closing "endif", and back
  42. to the opening "if". Nested structures are skipped. Using |g%| goes
  43. in the reverse direction.
  44. By default, words inside comments and strings are ignored, unless
  45. the cursor is inside a comment or string when you type "%". If the
  46. only thing you want to do is modify the behavior of "%" so that it
  47. behaves this way, you do not have to define |b:match_words|, since the
  48. script uses the 'matchpairs' option as well as this variable.
  49. See |matchit-details| for details on what the script does, and |b:match_words|
  50. for how to specify matching patterns.
  51. MODES: *matchit-modes* *matchit-v_%* *matchit-o_%*
  52. Mostly, % and related motions (|g%| and |[%| and |]%|) should just work like built-in
  53. |motion| commands in |Operator-pending| and |Visual| modes (as of 8.1.648)
  54. LANGUAGES: *matchit-languages*
  55. Currently, the following languages are supported: Ada, ASP with VBS, Csh,
  56. DTD, Entity, Essbase, Fortran, HTML, JSP (same as HTML), LaTeX, Lua, Pascal,
  57. SGML, Shell, Tcsh, Vim, XML. Other languages may already have support via
  58. the default |filetype-plugin|s in the standard vim distribution.
  59. To support a new language, see |matchit-newlang| below.
  60. DETAILS: *matchit-details* *matchit-parse*
  61. Here is an outline of what matchit.vim does each time you hit the "%" key. If
  62. there are |backref|s in |b:match_words| then the first step is to produce a
  63. version in which these back references have been eliminated; if there are no
  64. |backref|s then this step is skipped. This step is called parsing. For
  65. example, "\(foo\|bar\):end\1" is parsed to yield
  66. "\(foo\|bar\):end\(foo\|bar\)". This can get tricky, especially if there are
  67. nested groups. If debugging is turned on, the parsed version is saved as
  68. |b:match_pat|.
  69. *matchit-choose*
  70. Next, the script looks for a word on the current line that matches the pattern
  71. just constructed. It includes the patterns from the 'matchpairs' option.
  72. The goal is to do what you expect, which turns out to be a little complicated.
  73. The script follows these rules:
  74. Insist on a match that ends on or after the cursor.
  75. Prefer a match that includes the cursor position (that is, one that
  76. starts on or before the cursor).
  77. Prefer a match that starts as close to the cursor as possible.
  78. If more than one pattern in |b:match_words| matches, choose the one
  79. that is listed first.
  80. Examples:
  81. Suppose you >
  82. :let b:match_words = '<:>,<tag>:</tag>'
  83. < and hit "%" with the cursor on or before the "<" in "a <tag> is born".
  84. The pattern '<' comes first, so it is preferred over '<tag>', which
  85. also matches. If the cursor is on the "t", however, then '<tag>' is
  86. preferred, because this matches a bit of text containing the cursor.
  87. If the two groups of patterns were reversed then '<' would never be
  88. preferred.
  89. Suppose you >
  90. :let b:match_words = 'if:end if'
  91. < (Note the space!) and hit "%" with the cursor at the end of "end if".
  92. Then "if" matches, which is probably not what you want, but if the
  93. cursor starts on the "end " then "end if" is chosen. (You can avoid
  94. this problem by using a more complicated pattern.)
  95. If there is no match, the cursor does not move. (Before version 1.13 of the
  96. script, it would fall back on the usual behavior of |%|). If debugging is
  97. turned on, the matched bit of text is saved as |b:match_match| and the cursor
  98. column of the start of the match is saved as |b:match_col|.
  99. Next, the script looks through |b:match_words| (original and parsed versions)
  100. for the group and pattern that match. If debugging is turned on, the group is
  101. saved as |b:match_ini| (the first pattern) and |b:match_tail| (the rest). If
  102. there are |backref|s then, in addition, the matching pattern is saved as
  103. |b:match_word| and a table of translations is saved as |b:match_table|. If
  104. there are |backref|s, these are determined from the matching pattern and
  105. |b:match_match| and substituted into each pattern in the matching group.
  106. The script decides whether to search forwards or backwards and chooses
  107. arguments for the |searchpair()| function. Then, the cursor is moved to the
  108. start of the match, and |searchpair()| is called. By default, matching
  109. structures inside strings and comments are ignored. This can be changed by
  110. setting |b:match_skip|.
  111. ==============================================================================
  112. 2. Activation *matchit-activate*
  113. To use the matchit plugin add this line to your |vimrc|: >
  114. packadd! matchit
  115. The script should start working the next time you start Vim.
  116. (Earlier versions of the script did nothing unless a |buffer-variable| named
  117. |b:match_words| was defined. Even earlier versions contained autocommands
  118. that set this variable for various file types. Now, |b:match_words| is
  119. defined in many of the default |filetype-plugin|s instead.)
  120. For a new language, you can add autocommands to the script or to your vimrc
  121. file, but the recommended method is to add a line such as >
  122. let b:match_words = '\<foo\>:\<bar\>'
  123. to the |filetype-plugin| for your language. See |b:match_words| below for how
  124. this variable is interpreted.
  125. TROUBLESHOOTING *matchit-troubleshoot*
  126. The script should work in most installations of Vim. It may not work if Vim
  127. was compiled with a minimal feature set, for example if the |+syntax| option
  128. was not enabled. If your Vim has support for syntax compiled in, but you do
  129. not have |syntax| highlighting turned on, matchit.vim should work, but it may
  130. fail to skip matching groups in comments and strings. If the |filetype|
  131. mechanism is turned off, the |b:match_words| variable will probably not be
  132. defined automatically.
  133. ==============================================================================
  134. 3. Configuration *matchit-configure*
  135. There are several variables that govern the behavior of matchit.vim. Note
  136. that these are variables local to the buffer, not options, so use |:let| to
  137. define them, not |:set|. Some of these variables have values that matter; for
  138. others, it only matters whether the variable has been defined. All of these
  139. can be defined in the |filetype-plugin| or autocommand that defines
  140. |b:match_words| or "on the fly."
  141. The main variable is |b:match_words|. It is described in the section below on
  142. supporting a new language.
  143. *MatchError* *matchit-hl* *matchit-highlight*
  144. MatchError is the highlight group for error messages from the script. By
  145. default, it is linked to WarningMsg. If you do not want to be bothered by
  146. error messages, you can define this to be something invisible. For example,
  147. if you use the GUI version of Vim and your command line is normally white, you
  148. can do >
  149. :hi MatchError guifg=white guibg=white
  150. <
  151. *b:match_ignorecase*
  152. If you >
  153. :let b:match_ignorecase = 1
  154. then matchit.vim acts as if 'ignorecase' is set: for example, "end" and "END"
  155. are equivalent. If you >
  156. :let b:match_ignorecase = 0
  157. then matchit.vim treats "end" and "END" differently. (There will be no
  158. b:match_infercase option unless someone requests it.)
  159. *b:match_debug*
  160. Define b:match_debug if you want debugging information to be saved. See
  161. |matchit-debug|, below.
  162. *b:match_skip*
  163. If b:match_skip is defined, it is passed as the skip argument to
  164. |searchpair()|. This controls when matching structures are skipped, or
  165. ignored. By default, they are ignored inside comments and strings, as
  166. determined by the |syntax| mechanism. (If syntax highlighting is turned off,
  167. nothing is skipped.) You can set b:match_skip to a string, which evaluates to
  168. a non-zero, numerical value if the match is to be skipped or zero if the match
  169. should not be skipped. In addition, the following special values are
  170. supported by matchit.vim:
  171. s:foo becomes (current syntax item) =~ foo
  172. S:foo becomes (current syntax item) !~ foo
  173. r:foo becomes (line before cursor) =~ foo
  174. R:foo becomes (line before cursor) !~ foo
  175. (The "s" is meant to suggest "syntax", and the "r" is meant to suggest
  176. "regular expression".)
  177. Examples:
  178. You can get the default behavior with >
  179. :let b:match_skip = 's:comment\|string'
  180. <
  181. If you want to skip matching structures unless they are at the start
  182. of the line (ignoring whitespace) then you can >
  183. :let b:match_skip = 'R:^\s*'
  184. < Do not do this if strings or comments can span several lines, since
  185. the normal syntax checking will not be done if you set b:match_skip.
  186. In LaTeX, since "%" is used as the comment character, you can >
  187. :let b:match_skip = 'r:%'
  188. < Unfortunately, this will skip anything after "\%", an escaped "%". To
  189. allow for this, and also "\\%" (an escaped backslash followed by the
  190. comment character) you can >
  191. :let b:match_skip = 'r:\(^\|[^\\]\)\(\\\\\)*%'
  192. <
  193. See the $VIMRUNTIME/ftplugin/vim.vim for an example that uses both
  194. syntax and a regular expression.
  195. ==============================================================================
  196. 4. Supporting a New Language *matchit-newlang*
  197. *b:match_words*
  198. In order for matchit.vim to support a new language, you must define a suitable
  199. pattern for |b:match_words|. You may also want to set some of the
  200. |matchit-configure| variables, as described above. If your language has a
  201. complicated syntax, or many keywords, you will need to know something about
  202. Vim's |regular-expression|s.
  203. The format for |b:match_words| is similar to that of the 'matchpairs' option:
  204. it is a comma (,)-separated list of groups; each group is a colon(:)-separated
  205. list of patterns (regular expressions). Commas and backslashes that are part
  206. of a pattern should be escaped with backslashes ('\:' and '\,'). It is OK to
  207. have only one group; the effect is undefined if a group has only one pattern.
  208. A simple example is >
  209. :let b:match_words = '\<if\>:\<endif\>,'
  210. \ . '\<while\>:\<continue\>:\<break\>:\<endwhile\>'
  211. (In Vim regular expressions, |\<| and |\>| denote word boundaries. Thus "if"
  212. matches the end of "endif" but "\<if\>" does not.) Then banging on the "%"
  213. key will bounce the cursor between "if" and the matching "endif"; and from
  214. "while" to any matching "continue" or "break", then to the matching "endwhile"
  215. and back to the "while". It is almost always easier to use |literal-string|s
  216. (single quotes) as above: '\<if\>' rather than "\\<if\\>" and so on.
  217. Exception: If the ":" character does not appear in b:match_words, then it is
  218. treated as an expression to be evaluated. For example, >
  219. :let b:match_words = 'GetMatchWords()'
  220. allows you to define a function. This can return a different string depending
  221. on the current syntax, for example.
  222. Once you have defined the appropriate value of |b:match_words|, you will
  223. probably want to have this set automatically each time you edit the
  224. appropriate file type. The recommended way to do this is by adding the
  225. definition to a |filetype-plugin| file.
  226. Tips: Be careful that your initial pattern does not match your final pattern.
  227. See the example above for the use of word-boundary expressions. It is usually
  228. better to use ".\{-}" (as many as necessary) instead of ".*" (as many as
  229. possible). See |\{-|. For example, in the string "<tag>label</tag>", "<.*>"
  230. matches the whole string whereas "<.\{-}>" and "<[^>]*>" match "<tag>" and
  231. "</tag>".
  232. *matchit-spaces* *matchit-s:notend*
  233. If "if" is to be paired with "end if" (Note the space!) then word boundaries
  234. are not enough. Instead, define a regular expression s:notend that will match
  235. anything but "end" and use it as follows: >
  236. :let s:notend = '\%(\<end\s\+\)\@<!'
  237. :let b:match_words = s:notend . '\<if\>:\<end\s\+if\>'
  238. < *matchit-s:sol*
  239. This is a simplified version of what is done for Ada. The s:notend is a
  240. |script-variable|. Similarly, you may want to define a start-of-line regular
  241. expression >
  242. :let s:sol = '\%(^\|;\)\s*'
  243. if keywords are only recognized after the start of a line or after a
  244. semicolon (;), with optional white space.
  245. *matchit-backref* *matchit-\1*
  246. In any group, the expressions |\1|, |\2|, ..., |\9| refer to parts of the
  247. INITIAL pattern enclosed in |\(|escaped parentheses|\)|. These are referred
  248. to as back references, or backrefs. For example, >
  249. :let b:match_words = '\<b\(o\+\)\>:\(h\)\1\>'
  250. means that "bo" pairs with "ho" and "boo" pairs with "hoo" and so on. Note
  251. that "\1" does not refer to the "\(h\)" in this example. If you have
  252. "\(nested \(parentheses\)\) then "\d" refers to the d-th "\(" and everything
  253. up to and including the matching "\)": in "\(nested\(parentheses\)\)", "\1"
  254. refers to everything and "\2" refers to "\(parentheses\)". If you use a
  255. variable such as |s:notend| or |s:sol| in the previous paragraph then remember
  256. to count any "\(" patterns in this variable. You do not have to count groups
  257. defined by |\%(\)|.
  258. It should be possible to resolve back references from any pattern in the
  259. group. For example, >
  260. :let b:match_words = '\(foo\)\(bar\):more\1:and\2:end\1\2'
  261. would not work because "\2" cannot be determined from "morefoo" and "\1"
  262. cannot be determined from "andbar". On the other hand, >
  263. :let b:match_words = '\(\(foo\)\(bar\)\):\3\2:end\1'
  264. should work (and have the same effect as "foobar:barfoo:endfoobar"), although
  265. this has not been thoroughly tested.
  266. You can use |zero-width| patterns such as |\@<=| and |\zs|. (The latter has
  267. not been thouroughly tested in matchit.vim.) For example, if the keyword "if"
  268. must occur at the start of the line, with optional white space, you might use
  269. the pattern "\(^\s*\)\@<=if" so that the cursor will end on the "i" instead of
  270. at the start of the line. For another example, if HTML had only one tag then
  271. one could >
  272. :let b:match_words = '<:>,<\@<=tag>:<\@<=/tag>'
  273. so that "%" can bounce between matching "<" and ">" pairs or (starting on
  274. "tag" or "/tag") between matching tags. Without the |\@<=|, the script would
  275. bounce from "tag" to the "<" in "</tag>", and another "%" would not take you
  276. back to where you started.
  277. DEBUGGING *matchit-debug* *:MatchDebug*
  278. If you are having trouble figuring out the appropriate definition of
  279. |b:match_words| then you can take advantage of the same information I use when
  280. debugging the script. This is especially true if you are not sure whether
  281. your patterns or my script are at fault! To make this more convenient, I have
  282. made the command :MatchDebug, which defines the variable |b:match_debug| and
  283. creates a Matchit menu. This menu makes it convenient to check the values of
  284. the variables described below. You will probably also want to read
  285. |matchit-details| above.
  286. Defining the variable |b:match_debug| causes the script to set the following
  287. variables, each time you hit the "%" key. Several of these are only defined
  288. if |b:match_words| includes |backref|s.
  289. *b:match_pat*
  290. The b:match_pat variable is set to |b:match_words| with |backref|s parsed.
  291. *b:match_match*
  292. The b:match_match variable is set to the bit of text that is recognized as a
  293. match.
  294. *b:match_col*
  295. The b:match_col variable is set to the cursor column of the start of the
  296. matching text.
  297. *b:match_wholeBR*
  298. The b:match_wholeBR variable is set to the comma-separated group of patterns
  299. that matches, with |backref|s unparsed.
  300. *b:match_iniBR*
  301. The b:match_iniBR variable is set to the first pattern in |b:match_wholeBR|.
  302. *b:match_ini*
  303. The b:match_ini variable is set to the first pattern in |b:match_wholeBR|,
  304. with |backref|s resolved from |b:match_match|.
  305. *b:match_tail*
  306. The b:match_tail variable is set to the remaining patterns in
  307. |b:match_wholeBR|, with |backref|s resolved from |b:match_match|.
  308. *b:match_word*
  309. The b:match_word variable is set to the pattern from |b:match_wholeBR| that
  310. matches |b:match_match|.
  311. *b:match_table*
  312. The back reference '\'.d refers to the same thing as '\'.b:match_table[d] in
  313. |b:match_word|.
  314. ==============================================================================
  315. 5. Known Bugs and Limitations *matchit-bugs*
  316. Repository: https://github.com/chrisbra/matchit/
  317. Bugs can be reported at the repository (alternatively you can send me a mail).
  318. The latest development snapshot can also be downloaded there.
  319. Just because I know about a bug does not mean that it is on my todo list. I
  320. try to respond to reports of bugs that cause real problems. If it does not
  321. cause serious problems, or if there is a work-around, a bug may sit there for
  322. a while. Moral: if a bug (known or not) bothers you, let me know.
  323. It would be nice if "\0" were recognized as the entire pattern. That is, it
  324. would be nice if "foo:\end\0" had the same effect as "\(foo\):\end\1". I may
  325. try to implement this in a future version. (This is not so easy to arrange as
  326. you might think!)
  327. ==============================================================================
  328. vim:tw=78:ts=8:fo=tcq2:ft=help: