usr_44.txt 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. *usr_44.txt* Nvim
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Your own syntax highlighted
  4. Vim comes with highlighting for a couple of hundred different file types. If
  5. the file you are editing isn't included, read this chapter to find out how to
  6. get this type of file highlighted. Also see |:syn-define| in the reference
  7. manual.
  8. |44.1| Basic syntax commands
  9. |44.2| Keywords
  10. |44.3| Matches
  11. |44.4| Regions
  12. |44.5| Nested items
  13. |44.6| Following groups
  14. |44.7| Other arguments
  15. |44.8| Clusters
  16. |44.9| Including another syntax file
  17. |44.10| Synchronizing
  18. |44.11| Installing a syntax file
  19. |44.12| Portable syntax file layout
  20. Next chapter: |usr_45.txt| Select your language
  21. Previous chapter: |usr_43.txt| Using filetypes
  22. Table of contents: |usr_toc.txt|
  23. ==============================================================================
  24. *44.1* Basic syntax commands
  25. Using an existing syntax file to start with will save you a lot of time. Try
  26. finding a syntax file in $VIMRUNTIME/syntax for a language that is similar.
  27. These files will also show you the normal layout of a syntax file. To
  28. understand it, you need to read the following.
  29. Let's start with the basic arguments. Before we start defining any new
  30. syntax, we need to clear out any old definitions: >
  31. :syntax clear
  32. This isn't required in the final syntax file, but very useful when
  33. experimenting.
  34. There are more simplifications in this chapter. If you are writing a syntax
  35. file to be used by others, read all the way through the end to find out the
  36. details.
  37. LISTING DEFINED ITEMS
  38. To check which syntax items are currently defined, use this command: >
  39. :syntax
  40. You can use this to check which items have actually been defined. Quite
  41. useful when you are experimenting with a new syntax file. It also shows the
  42. colors used for each item, which helps to find out what is what.
  43. To list the items in a specific syntax group use: >
  44. :syntax list {group-name}
  45. This also can be used to list clusters (explained in |44.8|). Just include
  46. the @ in the name.
  47. MATCHING CASE
  48. Some languages are not case sensitive, such as Pascal. Others, such as C, are
  49. case sensitive. You need to tell which type you have with the following
  50. commands: >
  51. :syntax case match
  52. :syntax case ignore
  53. The "match" argument means that Vim will match the case of syntax elements.
  54. Therefore, "int" differs from "Int" and "INT". If the "ignore" argument is
  55. used, the following are equivalent: "Procedure", "PROCEDURE" and "procedure".
  56. The ":syntax case" commands can appear anywhere in a syntax file and affect
  57. the syntax definitions that follow. In most cases, you have only one ":syntax
  58. case" command in your syntax file; if you work with an unusual language that
  59. contains both case-sensitive and non-case-sensitive elements, however, you can
  60. scatter the ":syntax case" command throughout the file.
  61. ==============================================================================
  62. *44.2* Keywords
  63. The most basic syntax elements are keywords. To define a keyword, use the
  64. following form: >
  65. :syntax keyword {group} {keyword} ...
  66. The {group} is the name of a syntax group. With the ":highlight" command you
  67. can assign colors to a {group}. The {keyword} argument is an actual keyword.
  68. Here are a few examples: >
  69. :syntax keyword xType int long char
  70. :syntax keyword xStatement if then else endif
  71. This example uses the group names "xType" and "xStatement". By convention,
  72. each group name is prefixed by the filetype for the language being defined.
  73. This example defines syntax for the x language (eXample language without an
  74. interesting name). In a syntax file for "csh" scripts the name "cshType"
  75. would be used. Thus the prefix is equal to the value of 'filetype'.
  76. These commands cause the words "int", "long" and "char" to be highlighted
  77. one way and the words "if", "then", "else" and "endif" to be highlighted
  78. another way. Now you need to connect the x group names to standard Vim
  79. names. You do this with the following commands: >
  80. :highlight link xType Type
  81. :highlight link xStatement Statement
  82. This tells Vim to highlight "xType" like "Type" and "xStatement" like
  83. "Statement". See |group-name| for the standard names.
  84. UNUSUAL KEYWORDS
  85. The characters used in a keyword must be in the 'iskeyword' option. If you
  86. use another character, the word will never match. Vim doesn't give a warning
  87. message for this.
  88. The x language uses the '-' character in keywords. This is how it's done:
  89. >
  90. :setlocal iskeyword+=-
  91. :syntax keyword xStatement when-not
  92. The ":setlocal" command is used to change 'iskeyword' only for the current
  93. buffer. Still it does change the behavior of commands like "w" and "*". If
  94. that is not wanted, don't define a keyword but use a match (explained in the
  95. next section).
  96. The x language allows for abbreviations. For example, "next" can be
  97. abbreviated to "n", "ne" or "nex". You can define them by using this command:
  98. >
  99. :syntax keyword xStatement n[ext]
  100. This doesn't match "nextone", keywords always match whole words only.
  101. ==============================================================================
  102. *44.3* Matches
  103. Consider defining something a bit more complex. You want to match ordinary
  104. identifiers. To do this, you define a match syntax item. This one matches
  105. any word consisting of only lowercase letters: >
  106. :syntax match xIdentifier /\<\l\+\>/
  107. <
  108. Note:
  109. Keywords overrule any other syntax item. Thus the keywords "if",
  110. "then", etc., will be keywords, as defined with the ":syntax keyword"
  111. commands above, even though they also match the pattern for
  112. xIdentifier.
  113. The part at the end is a pattern, like it's used for searching. The // is
  114. used to surround the pattern (like how it's done in a ":substitute" command).
  115. You can use any other character, like a plus or a quote.
  116. Now define a match for a comment. In the x language it is anything from # to
  117. the end of a line: >
  118. :syntax match xComment /#.*/
  119. Since you can use any search pattern, you can highlight very complex things
  120. with a match item. See |pattern| for help on search patterns.
  121. ==============================================================================
  122. *44.4* Regions
  123. In the example x language, strings are enclosed in double quotation marks (").
  124. To highlight strings you define a region. You need a region start (double
  125. quote) and a region end (double quote). The definition is as follows: >
  126. :syntax region xString start=/"/ end=/"/
  127. The "start" and "end" directives define the patterns used to find the start
  128. and end of the region. But what about strings that look like this?
  129. "A string with a double quote (\") in it" ~
  130. This creates a problem: The double quotation marks in the middle of the string
  131. will end the region. You need to tell Vim to skip over any escaped double
  132. quotes in the string. Do this with the skip keyword: >
  133. :syntax region xString start=/"/ skip=/\\"/ end=/"/
  134. The double backslash matches a single backslash, since the backslash is a
  135. special character in search patterns.
  136. When to use a region instead of a match? The main difference is that a match
  137. item is a single pattern, which must match as a whole. A region starts as
  138. soon as the "start" pattern matches. Whether the "end" pattern is found or
  139. not doesn't matter. Thus when the item depends on the "end" pattern to match,
  140. you cannot use a region. Otherwise, regions are often simpler to define. And
  141. it is easier to use nested items, as is explained in the next section.
  142. ==============================================================================
  143. *44.5* Nested items
  144. Take a look at this comment:
  145. %Get input TODO: Skip white space ~
  146. You want to highlight TODO in big yellow letters, even though it is in a
  147. comment that is highlighted blue. To let Vim know about this, you define the
  148. following syntax groups: >
  149. :syntax keyword xTodo TODO contained
  150. :syntax match xComment /%.*/ contains=xTodo
  151. In the first line, the "contained" argument tells Vim that this keyword can
  152. exist only inside another syntax item. The next line has "contains=xTodo".
  153. This indicates that the xTodo syntax element is inside it. The result is that
  154. the comment line as a whole is matched with "xComment" and made blue. The
  155. word TODO inside it is matched by xTodo and highlighted yellow (highlighting
  156. for xTodo was setup for this).
  157. RECURSIVE NESTING
  158. The x language defines code blocks in curly braces. And a code block may
  159. contain other code blocks. This can be defined this way: >
  160. :syntax region xBlock start=/{/ end=/}/ contains=xBlock
  161. Suppose you have this text:
  162. while i < b { ~
  163. if a { ~
  164. b = c; ~
  165. } ~
  166. } ~
  167. First a xBlock starts at the { in the first line. In the second line another
  168. { is found. Since we are inside a xBlock item, and it contains itself, a
  169. nested xBlock item will start here. Thus the "b = c" line is inside the
  170. second level xBlock region. Then a } is found in the next line, which matches
  171. with the end pattern of the region. This ends the nested xBlock. Because the
  172. } is included in the nested region, it is hidden from the first xBlock region.
  173. Then at the last } the first xBlock region ends.
  174. KEEPING THE END
  175. Consider the following two syntax items: >
  176. :syntax region xComment start=/%/ end=/$/ contained
  177. :syntax region xPreProc start=/#/ end=/$/ contains=xComment
  178. You define a comment as anything from % to the end of the line. A
  179. preprocessor directive is anything from # to the end of the line. Because you
  180. can have a comment on a preprocessor line, the preprocessor definition
  181. includes a "contains=xComment" argument. Now look what happens with this
  182. text:
  183. #define X = Y % Comment text ~
  184. int foo = 1; ~
  185. What you see is that the second line is also highlighted as xPreProc. The
  186. preprocessor directive should end at the end of the line. That is why
  187. you have used "end=/$/". So what is going wrong?
  188. The problem is the contained comment. The comment starts with % and ends
  189. at the end of the line. After the comment ends, the preprocessor syntax
  190. continues. This is after the end of the line has been seen, so the next
  191. line is included as well.
  192. To avoid this problem and to avoid a contained syntax item eating a needed
  193. end of line, use the "keepend" argument. This takes care of
  194. the double end-of-line matching: >
  195. :syntax region xComment start=/%/ end=/$/ contained
  196. :syntax region xPreProc start=/#/ end=/$/ contains=xComment keepend
  197. CONTAINING MANY ITEMS
  198. You can use the contains argument to specify that everything can be contained.
  199. For example: >
  200. :syntax region xList start=/\[/ end=/\]/ contains=ALL
  201. All syntax items will be contained in this one. It also contains itself, but
  202. not at the same position (that would cause an endless loop).
  203. You can specify that some groups are not contained. Thus contain all
  204. groups but the ones that are listed:
  205. >
  206. :syntax region xList start=/\[/ end=/\]/ contains=ALLBUT,xString
  207. With the "TOP" item you can include all items that don't have a "contained"
  208. argument. "CONTAINED" is used to only include items with a "contained"
  209. argument. See |:syn-contains| for the details.
  210. ==============================================================================
  211. *44.6* Following groups
  212. The x language has statements in this form:
  213. if (condition) then ~
  214. You want to highlight the three items differently. But "(condition)" and
  215. "then" might also appear in other places, where they get different
  216. highlighting. This is how you can do this: >
  217. :syntax match xIf /if/ nextgroup=xIfCondition skipwhite
  218. :syntax match xIfCondition /([^)]*)/ contained nextgroup=xThen skipwhite
  219. :syntax match xThen /then/ contained
  220. The "nextgroup" argument specifies which item can come next. This is not
  221. required. If none of the items that are specified are found, nothing happens.
  222. For example, in this text:
  223. if not (condition) then ~
  224. The "if" is matched by xIf. "not" doesn't match the specified nextgroup
  225. xIfCondition, thus only the "if" is highlighted.
  226. The "skipwhite" argument tells Vim that white space (spaces and tabs) may
  227. appear in between the items. Similar arguments are "skipnl", which allows a
  228. line break in between the items, and "skipempty", which allows empty lines.
  229. Notice that "skipnl" doesn't skip an empty line, something must match after
  230. the line break.
  231. ==============================================================================
  232. *44.7* Other arguments
  233. MATCHGROUP
  234. When you define a region, the entire region is highlighted according to the
  235. group name specified. To highlight the text enclosed in parentheses () with
  236. the group xInside, for example, use the following command: >
  237. :syntax region xInside start=/(/ end=/)/
  238. Suppose, that you want to highlight the parentheses differently. You can do
  239. this with a lot of convoluted region statements, or you can use the
  240. "matchgroup" argument. This tells Vim to highlight the start and end of a
  241. region with a different highlight group (in this case, the xParen group): >
  242. :syntax region xInside matchgroup=xParen start=/(/ end=/)/
  243. The "matchgroup" argument applies to the start or end match that comes after
  244. it. In the previous example both start and end are highlighted with xParen.
  245. To highlight the end with xParenEnd: >
  246. :syntax region xInside matchgroup=xParen start=/(/
  247. \ matchgroup=xParenEnd end=/)/
  248. A side effect of using "matchgroup" is that contained items will not match in
  249. the start or end of the region. The example for "transparent" uses this.
  250. TRANSPARENT
  251. In a C language file you would like to highlight the () text after a "while"
  252. differently from the () text after a "for". In both of these there can be
  253. nested () items, which should be highlighted in the same way. You must make
  254. sure the () highlighting stops at the matching ). This is one way to do this:
  255. >
  256. :syntax region cWhile matchgroup=cWhile start=/while\s*(/ end=/)/
  257. \ contains=cCondNest
  258. :syntax region cFor matchgroup=cFor start=/for\s*(/ end=/)/
  259. \ contains=cCondNest
  260. :syntax region cCondNest start=/(/ end=/)/ contained transparent
  261. Now you can give cWhile and cFor different highlighting. The cCondNest item
  262. can appear in either of them, but take over the highlighting of the item it is
  263. contained in. The "transparent" argument causes this.
  264. Notice that the "matchgroup" argument has the same group as the item
  265. itself. Why define it then? Well, the side effect of using a matchgroup is
  266. that contained items are not found in the match with the start item then.
  267. This avoids that the cCondNest group matches the ( just after the "while" or
  268. "for". If this would happen, it would span the whole text until the matching
  269. ) and the region would continue after it. Now cCondNest only matches after
  270. the match with the start pattern, thus after the first (.
  271. OFFSETS
  272. Suppose you want to define a region for the text between ( and ) after an
  273. "if". But you don't want to include the "if" or the ( and ). You can do this
  274. by specifying offsets for the patterns. Example: >
  275. :syntax region xCond start=/if\s*(/ms=e+1 end=/)/me=s-1
  276. The offset for the start pattern is "ms=e+1". "ms" stands for Match Start.
  277. This defines an offset for the start of the match. Normally the match starts
  278. where the pattern matches. "e+1" means that the match now starts at the end
  279. of the pattern match, and then one character further.
  280. The offset for the end pattern is "me=s-1". "me" stands for Match End.
  281. "s-1" means the start of the pattern match and then one character back. The
  282. result is that in this text:
  283. if (foo == bar) ~
  284. Only the text "foo == bar" will be highlighted as xCond.
  285. More about offsets here: |:syn-pattern-offset|.
  286. ONELINE
  287. The "oneline" argument indicates that the region does not cross a line
  288. boundary. For example: >
  289. :syntax region xIfThen start=/if/ end=/then/ oneline
  290. This defines a region that starts at "if" and ends at "then". But if there is
  291. no "then" after the "if", the region doesn't match.
  292. Note:
  293. When using "oneline" the region doesn't start if the end pattern
  294. doesn't match in the same line. Without "oneline" Vim does _not_
  295. check if there is a match for the end pattern. The region starts even
  296. when the end pattern doesn't match in the rest of the file.
  297. CONTINUATION LINES AND AVOIDING THEM
  298. Things now become a little more complex. Let's define a preprocessor line.
  299. This starts with a # in the first column and continues until the end of the
  300. line. A line that ends with \ makes the next line a continuation line. The
  301. way you handle this is to allow the syntax item to contain a continuation
  302. pattern: >
  303. :syntax region xPreProc start=/^#/ end=/$/ contains=xLineContinue
  304. :syntax match xLineContinue "\\$" contained
  305. In this case, although xPreProc normally matches a single line, the group
  306. contained in it (namely xLineContinue) lets it go on for more than one line.
  307. For example, it would match both of these lines:
  308. #define SPAM spam spam spam \ ~
  309. bacon and spam ~
  310. In this case, this is what you want. If it is not what you want, you can call
  311. for the region to be on a single line by adding "excludenl" to the contained
  312. pattern. For example, you want to highlight "end" in xPreProc, but only at
  313. the end of the line. To avoid making the xPreProc continue on the next line,
  314. like xLineContinue does, use "excludenl" like this: >
  315. :syntax region xPreProc start=/^#/ end=/$/
  316. \ contains=xLineContinue,xPreProcEnd
  317. :syntax match xPreProcEnd excludenl /end$/ contained
  318. :syntax match xLineContinue "\\$" contained
  319. "excludenl" must be placed before the pattern. Since "xLineContinue" doesn't
  320. have "excludenl", a match with it will extend xPreProc to the next line as
  321. before.
  322. ==============================================================================
  323. *44.8* Clusters
  324. One of the things you will notice as you start to write a syntax file is that
  325. you wind up generating a lot of syntax groups. Vim enables you to define a
  326. collection of syntax groups called a cluster.
  327. Suppose you have a language that contains for loops, if statements, while
  328. loops, and functions. Each of them contains the same syntax elements: numbers
  329. and identifiers. You define them like this: >
  330. :syntax match xFor /^for.*/ contains=xNumber,xIdent
  331. :syntax match xIf /^if.*/ contains=xNumber,xIdent
  332. :syntax match xWhile /^while.*/ contains=xNumber,xIdent
  333. You have to repeat the same "contains=" every time. If you want to add
  334. another contained item, you have to add it three times. Syntax clusters
  335. simplify these definitions by enabling you to have one cluster stand for
  336. several syntax groups.
  337. To define a cluster for the two items that the three groups contain, use
  338. the following command: >
  339. :syntax cluster xState contains=xNumber,xIdent
  340. Clusters are used inside other syntax items just like any syntax group.
  341. Their names start with @. Thus, you can define the three groups like this: >
  342. :syntax match xFor /^for.*/ contains=@xState
  343. :syntax match xIf /^if.*/ contains=@xState
  344. :syntax match xWhile /^while.*/ contains=@xState
  345. You can add new group names to this cluster with the "add" argument: >
  346. :syntax cluster xState add=xString
  347. You can remove syntax groups from this list as well: >
  348. :syntax cluster xState remove=xNumber
  349. ==============================================================================
  350. *44.9* Including another syntax file
  351. The C++ language syntax is a superset of the C language. Because you do not
  352. want to write two syntax files, you can have the C++ syntax file read in the
  353. one for C by using the following command: >
  354. :runtime! syntax/c.vim
  355. The ":runtime!" command searches 'runtimepath' for all "syntax/c.vim" files.
  356. This makes the C parts of the C++ syntax be defined like for C files. If you
  357. have replaced the c.vim syntax file, or added items with an extra file, these
  358. will be loaded as well.
  359. After loading the C syntax items the specific C++ items can be defined.
  360. For example, add keywords that are not used in C: >
  361. :syntax keyword cppStatement new delete this friend using
  362. This works just like in any other syntax file.
  363. Now consider the Perl language. A Perl script consists of two distinct parts:
  364. a documentation section in POD format, and a program written in Perl itself.
  365. The POD section starts with "=head" and ends with "=cut".
  366. You want to define the POD syntax in one file, and use it from the Perl
  367. syntax file. The ":syntax include" command reads in a syntax file and stores
  368. the elements it defined in a syntax cluster. For Perl, the statements are as
  369. follows: >
  370. :syntax include @Pod <sfile>:p:h/pod.vim
  371. :syntax region perlPOD start=/^=head/ end=/^=cut/ contains=@Pod
  372. When "=head" is found in a Perl file, the perlPOD region starts. In this
  373. region the @Pod cluster is contained. All the items defined as top-level
  374. items in the pod.vim syntax files will match here. When "=cut" is found, the
  375. region ends and we go back to the items defined in the Perl file.
  376. The ":syntax include" command is clever enough to ignore a ":syntax clear"
  377. command in the included file. And an argument such as "contains=ALL" will
  378. only contain items defined in the included file, not in the file that includes
  379. it.
  380. The "<sfile>:p:h/" part uses the name of the current file (<sfile>),
  381. expands it to a full path (:p) and then takes the head (:h). This results in
  382. the directory name of the file. This causes the pod.vim file in the same
  383. directory to be included.
  384. ==============================================================================
  385. *44.10* Synchronizing
  386. Compilers have it easy. They start at the beginning of a file and parse it
  387. straight through. Vim does not have it so easy. It must start in the middle,
  388. where the editing is being done. So how does it tell where it is?
  389. The secret is the ":syntax sync" command. This tells Vim how to figure out
  390. where it is. For example, the following command tells Vim to scan backward
  391. for the beginning or end of a C-style comment and begin syntax coloring from
  392. there: >
  393. :syntax sync ccomment
  394. You can tune this processing with some arguments. The "minlines" argument
  395. tells Vim the minimum number of lines to look backward, and "maxlines" tells
  396. the editor the maximum number of lines to scan.
  397. For example, the following command tells Vim to look at least 10 lines
  398. before the top of the screen: >
  399. :syntax sync ccomment minlines=10 maxlines=500
  400. If it cannot figure out where it is in that space, it starts looking farther
  401. and farther back until it figures out what to do. But it looks no farther
  402. back than 500 lines. (A large "maxlines" slows down processing. A small one
  403. might cause synchronization to fail.)
  404. To make synchronizing go a bit faster, tell Vim which syntax items can be
  405. skipped. Every match and region that only needs to be used when actually
  406. displaying text can be given the "display" argument.
  407. By default, the comment to be found will be colored as part of the Comment
  408. syntax group. If you want to color things another way, you can specify a
  409. different syntax group: >
  410. :syntax sync ccomment xAltComment
  411. If your programming language does not have C-style comments in it, you can try
  412. another method of synchronization. The simplest way is to tell Vim to space
  413. back a number of lines and try to figure out things from there. The following
  414. command tells Vim to go back 150 lines and start parsing from there: >
  415. :syntax sync minlines=150
  416. A large "minlines" value can make Vim slower, especially when scrolling
  417. backwards in the file.
  418. Finally, you can specify a syntax group to look for by using this command:
  419. >
  420. :syntax sync match {sync-group-name}
  421. \ grouphere {group-name} {pattern}
  422. This tells Vim that when it sees {pattern} the syntax group named {group-name}
  423. begins just after the pattern given. The {sync-group-name} is used to give a
  424. name to this synchronization specification. For example, the sh scripting
  425. language begins an if statement with "if" and ends it with "fi":
  426. if [ --f file.txt ] ; then ~
  427. echo "File exists" ~
  428. fi ~
  429. To define a "grouphere" directive for this syntax, you use the following
  430. command: >
  431. :syntax sync match shIfSync grouphere shIf "\<if\>"
  432. The "groupthere" argument tells Vim that the pattern ends a group. For
  433. example, the end of the if/fi group is as follows: >
  434. :syntax sync match shIfSync groupthere NONE "\<fi\>"
  435. In this example, the NONE tells Vim that you are not in any special syntax
  436. region. In particular, you are not inside an if block.
  437. You also can define matches and regions that are with no "grouphere" or
  438. "groupthere" arguments. These groups are for syntax groups skipped during
  439. synchronization. For example, the following skips over anything inside {},
  440. even if it would normally match another synchronization method: >
  441. :syntax sync match xSpecial /{.*}/
  442. More about synchronizing in the reference manual: |:syn-sync|.
  443. ==============================================================================
  444. *44.11* Installing a syntax file
  445. When your new syntax file is ready to be used, drop it in a "syntax" directory
  446. in 'runtimepath'. For Unix that would be "~/.config/nvim/syntax".
  447. The name of the syntax file must be equal to the file type, with ".vim"
  448. added. Thus for the x language, the full path of the file would be:
  449. ~/.config/nvim/syntax/x.vim ~
  450. You must also make the file type be recognized. See |43.2|.
  451. If your file works well, you might want to make it available to other Vim
  452. users. First read the next section to make sure your file works well for
  453. others. Then e-mail it to the Vim maintainer: <maintainer@vim.org>. Also
  454. explain how the filetype can be detected. With a bit of luck your file will
  455. be included in the next Vim version!
  456. ADDING TO AN EXISTING SYNTAX FILE
  457. We were assuming you were adding a completely new syntax file. When an existing
  458. syntax file works, but is missing some items, you can add items in a separate
  459. file. That avoids changing the distributed syntax file, which will be lost
  460. when installing a new version of Vim.
  461. Write syntax commands in your file, possibly using group names from the
  462. existing syntax. For example, to add new variable types to the C syntax file:
  463. >
  464. :syntax keyword cType off_t uint
  465. Write the file with the same name as the original syntax file. In this case
  466. "c.vim". Place it in a directory near the end of 'runtimepath'. This makes
  467. it loaded after the original syntax file. For Unix this would be:
  468. ~/.config/nvim/after/syntax/c.vim ~
  469. ==============================================================================
  470. *44.12* Portable syntax file layout
  471. Wouldn't it be nice if all Vim users exchange syntax files? To make this
  472. possible, the syntax file must follow a few guidelines.
  473. Start with a header that explains what the syntax file is for, who maintains
  474. it and when it was last updated. Don't include too much information about
  475. changes history, not many people will read it. Example: >
  476. " Vim syntax file
  477. " Language: C
  478. " Maintainer: Bram Moolenaar <Bram@vim.org>
  479. " Last Change: 2001 Jun 18
  480. " Remark: Included by the C++ syntax.
  481. Use the same layout as the other syntax files. Using an existing syntax file
  482. as an example will save you a lot of time.
  483. Choose a good, descriptive name for your syntax file. Use lowercase letters
  484. and digits. Don't make it too long, it is used in many places: The name of
  485. the syntax file "name.vim", 'filetype', b:current_syntax and the start of each
  486. syntax group (nameType, nameStatement, nameString, etc).
  487. Start with a check for "b:current_syntax". If it is defined, some other
  488. syntax file, earlier in 'runtimepath' was already loaded: >
  489. if exists("b:current_syntax")
  490. finish
  491. endif
  492. Set "b:current_syntax" to the name of the syntax at the end. Don't forget
  493. that included files do this too, you might have to reset "b:current_syntax" if
  494. you include two files.
  495. Do not include anything that is a user preference. Don't set 'tabstop',
  496. 'expandtab', etc. These belong in a filetype plugin.
  497. Do not include mappings or abbreviations. Only include setting 'iskeyword' if
  498. it is really necessary for recognizing keywords.
  499. To allow users select their own preferred colors, make a different group name
  500. for every kind of highlighted item. Then link each of them to one of the
  501. standard highlight groups. That will make it work with every color scheme.
  502. If you select specific colors it will look bad with some color schemes. And
  503. don't forget that some people use a different background color, or have only
  504. eight colors available.
  505. For the linking use "hi def link", so that the user can select different
  506. highlighting before your syntax file is loaded. Example: >
  507. hi def link nameString String
  508. hi def link nameNumber Number
  509. hi def link nameCommand Statement
  510. ... etc ...
  511. Add the "display" argument to items that are not used when syncing, to speed
  512. up scrolling backwards and CTRL-L.
  513. ==============================================================================
  514. Next chapter: |usr_45.txt| Select your language
  515. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: