annotate runtime/doc/usr_44.txt @ 26877:06a137af96f8 v8.2.3967

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