Nvim :help
pages, generated
from source
using the tree-sitter-vimdoc parser.
:syntax clearThis isn't required in the final syntax file, but very useful when experimenting.
:syntaxYou can use this to check which items have actually been defined. Quite useful when you are experimenting with a new syntax file. It also shows the colors used for each item, which helps to find out what is what. To list the items in a specific syntax group use:
:syntax list {group-name}This also can be used to list clusters (explained in 44.8). Just include the @ in the name.
:syntax case match :syntax case ignoreThe "match" argument means that Vim will match the case of syntax elements. Therefore, "int" differs from "Int" and "INT". If the "ignore" argument is used, the following are equivalent: "Procedure", "PROCEDURE" and "procedure". The ":syntax case" commands can appear anywhere in a syntax file and affect the syntax definitions that follow. In most cases, you have only one ":syntax case" command in your syntax file; if you work with an unusual language that contains both case-sensitive and non-case-sensitive elements, however, you can scatter the ":syntax case" command throughout the file.
:syntax keyword {group} {keyword} ...The
{group}
is the name of a syntax group. With the ":highlight" command you
can assign colors to a {group}
. The {keyword}
argument is an actual keyword.
Here are a few examples::syntax keyword xType int long char :syntax keyword xStatement if then else endifThis example uses the group names "xType" and "xStatement". By convention, each group name is prefixed by the filetype for the language being defined. This example defines syntax for the x language (eXample language without an interesting name). In a syntax file for "csh" scripts the name "cshType" would be used. Thus the prefix is equal to the value of 'filetype'. These commands cause the words "int", "long" and "char" to be highlighted one way and the words "if", "then", "else" and "endif" to be highlighted another way. Now you need to connect the x group names to standard Vim names. You do this with the following commands:
:highlight link xType Type :highlight link xStatement StatementThis tells Vim to highlight "xType" like "Type" and "xStatement" like "Statement". See group-name for the standard names.
:setlocal iskeyword+=- :syntax keyword xStatement when-notThe ":setlocal" command is used to change 'iskeyword' only for the current buffer. Still it does change the behavior of commands like "w" and "*". If that is not wanted, don't define a keyword but use a match (explained in the next section).
:syntax keyword xStatement n[ext]This doesn't match "nextone", keywords always match whole words only.
:syntax match xIdentifier /\<\l\+\>/
:syntax match xComment /#.*/Since you can use any search pattern, you can highlight very complex things with a match item. See pattern for help on search patterns.
:syntax region xString start=/"/ end=/"/The "start" and "end" directives define the patterns used to find the start and end of the region. But what about strings that look like this?
:syntax region xString start=/"/ skip=/\\"/ end=/"/The double backslash matches a single backslash, since the backslash is a special character in search patterns.
:syntax keyword xTodo TODO contained :syntax match xComment /%.*/ contains=xTodoIn the first line, the "contained" argument tells Vim that this keyword can exist only inside another syntax item. The next line has "contains=xTodo". This indicates that the xTodo syntax element is inside it. The result is that the comment line as a whole is matched with "xComment" and made blue. The word TODO inside it is matched by xTodo and highlighted yellow (highlighting for xTodo was setup for this).
:syntax region xBlock start=/{/ end=/}/ contains=xBlockSuppose you have this text:
:syntax region xComment start=/%/ end=/$/ contained :syntax region xPreProc start=/#/ end=/$/ contains=xCommentYou define a comment as anything from % to the end of the line. A preprocessor directive is anything from # to the end of the line. Because you can have a comment on a preprocessor line, the preprocessor definition includes a "contains=xComment" argument. Now look what happens with this text:
:syntax region xComment start=/%/ end=/$/ contained :syntax region xPreProc start=/#/ end=/$/ contains=xComment keepend
:syntax region xList start=/\[/ end=/\]/ contains=ALLAll syntax items will be contained in this one. It also contains itself, but not at the same position (that would cause an endless loop). You can specify that some groups are not contained. Thus contain all groups but the ones that are listed:
:syntax region xList start=/\[/ end=/\]/ contains=ALLBUT,xStringWith the "TOP" item you can include all items that don't have a "contained" argument. "CONTAINED" is used to only include items with a "contained" argument. See :syn-contains for the details.
:syntax match xIf /if/ nextgroup=xIfCondition skipwhite :syntax match xIfCondition /([^)]*)/ contained nextgroup=xThen skipwhite :syntax match xThen /then/ containedThe "nextgroup" argument specifies which item can come next. This is not required. If none of the items that are specified are found, nothing happens. For example, in this text:
:syntax region xInside start=/(/ end=/)/Suppose, that you want to highlight the parentheses differently. You can do this with a lot of convoluted region statements, or you can use the "matchgroup" argument. This tells Vim to highlight the start and end of a region with a different highlight group (in this case, the xParen group):
:syntax region xInside matchgroup=xParen start=/(/ end=/)/The "matchgroup" argument applies to the start or end match that comes after it. In the previous example both start and end are highlighted with xParen. To highlight the end with xParenEnd:
:syntax region xInside matchgroup=xParen start=/(/ \ matchgroup=xParenEnd end=/)/A side effect of using "matchgroup" is that contained items will not match in the start or end of the region. The example for "transparent" uses this.
:syntax region cWhile matchgroup=cWhile start=/while\s*(/ end=/)/ \ contains=cCondNest :syntax region cFor matchgroup=cFor start=/for\s*(/ end=/)/ \ contains=cCondNest :syntax region cCondNest start=/(/ end=/)/ contained transparentNow you can give cWhile and cFor different highlighting. The cCondNest item can appear in either of them, but take over the highlighting of the item it is contained in. The "transparent" argument causes this. Notice that the "matchgroup" argument has the same group as the item itself. Why define it then? Well, the side effect of using a matchgroup is that contained items are not found in the match with the start item then. This avoids that the cCondNest group matches the ( just after the "while" or "for". If this would happen, it would span the whole text until the matching ) and the region would continue after it. Now cCondNest only matches after the match with the start pattern, thus after the first (.
:syntax region xCond start=/if\s*(/ms=e+1 end=/)/me=s-1The offset for the start pattern is "ms=e+1". "ms" stands for Match Start. This defines an offset for the start of the match. Normally the match starts where the pattern matches. "e+1" means that the match now starts at the end of the pattern match, and then one character further. The offset for the end pattern is "me=s-1". "me" stands for Match End. "s-1" means the start of the pattern match and then one character back. The result is that in this text:
:syntax region xIfThen start=/if/ end=/then/ onelineThis defines a region that starts at "if" and ends at "then". But if there is no "then" after the "if", the region doesn't match.
:syntax region xPreProc start=/^#/ end=/$/ contains=xLineContinue :syntax match xLineContinue "\\$" containedIn this case, although xPreProc normally matches a single line, the group contained in it (namely xLineContinue) lets it go on for more than one line. For example, it would match both of these lines:
:syntax region xPreProc start=/^#/ end=/$/ \ contains=xLineContinue,xPreProcEnd :syntax match xPreProcEnd excludenl /end$/ contained :syntax match xLineContinue "\\$" contained"excludenl" must be placed before the pattern. Since "xLineContinue" doesn't have "excludenl", a match with it will extend xPreProc to the next line as before.
:syntax match xFor /^for.*/ contains=xNumber,xIdent :syntax match xIf /^if.*/ contains=xNumber,xIdent :syntax match xWhile /^while.*/ contains=xNumber,xIdentYou have to repeat the same "contains=" every time. If you want to add another contained item, you have to add it three times. Syntax clusters simplify these definitions by enabling you to have one cluster stand for several syntax groups. To define a cluster for the two items that the three groups contain, use the following command:
:syntax cluster xState contains=xNumber,xIdentClusters are used inside other syntax items just like any syntax group. Their names start with @. Thus, you can define the three groups like this:
:syntax match xFor /^for.*/ contains=@xState :syntax match xIf /^if.*/ contains=@xState :syntax match xWhile /^while.*/ contains=@xStateYou can add new group names to this cluster with the "add" argument:
:syntax cluster xState add=xStringYou can remove syntax groups from this list as well:
:syntax cluster xState remove=xNumber
:runtime! syntax/c.vimThe ":runtime!" command searches 'runtimepath' for all "syntax/c.vim" files. This makes the C parts of the C++ syntax be defined like for C files. If you have replaced the c.vim syntax file, or added items with an extra file, these will be loaded as well. After loading the C syntax items the specific C++ items can be defined. For example, add keywords that are not used in C:
:syntax keyword cppStatement new delete this friend usingThis works just like in any other syntax file.
:syntax include @Pod <sfile>:p:h/pod.vim :syntax region perlPOD start=/^=head/ end=/^=cut/ contains=@PodWhen "=head" is found in a Perl file, the perlPOD region starts. In this region the @Pod cluster is contained. All the items defined as top-level items in the pod.vim syntax files will match here. When "=cut" is found, the region ends and we go back to the items defined in the Perl file. The ":syntax include" command is clever enough to ignore a ":syntax clear" command in the included file. And an argument such as "contains=ALL" will only contain items defined in the included file, not in the file that includes it. The "<sfile>:p:h/" part uses the name of the current file (
<sfile>
),
expands it to a full path (:p) and then takes the head (:h). This results in
the directory name of the file. This causes the pod.vim file in the same
directory to be included.:syntax sync ccommentYou can tune this processing with some arguments. The "minlines" argument tells Vim the minimum number of lines to look backward, and "maxlines" tells the editor the maximum number of lines to scan. For example, the following command tells Vim to look at least 10 lines before the top of the screen:
:syntax sync ccomment minlines=10 maxlines=500If it cannot figure out where it is in that space, it starts looking farther and farther back until it figures out what to do. But it looks no farther back than 500 lines. (A large "maxlines" slows down processing. A small one might cause synchronization to fail.) To make synchronizing go a bit faster, tell Vim which syntax items can be skipped. Every match and region that only needs to be used when actually displaying text can be given the "display" argument. By default, the comment to be found will be colored as part of the Comment syntax group. If you want to color things another way, you can specify a different syntax group:
:syntax sync ccomment xAltCommentIf your programming language does not have C-style comments in it, you can try another method of synchronization. The simplest way is to tell Vim to space back a number of lines and try to figure out things from there. The following command tells Vim to go back 150 lines and start parsing from there:
:syntax sync minlines=150A large "minlines" value can make Vim slower, especially when scrolling backwards in the file. Finally, you can specify a syntax group to look for by using this command:
:syntax sync match {sync-group-name} \ grouphere {group-name} {pattern}This tells Vim that when it sees
{pattern}
the syntax group named {group-name}
begins just after the pattern given. The {sync-group-name}
is used to give a
name to this synchronization specification. For example, the sh scripting
language begins an if statement with "if" and ends it with "fi"::syntax sync match shIfSync grouphere shIf "\<if\>"The "groupthere" argument tells Vim that the pattern ends a group. For example, the end of the if/fi group is as follows:
:syntax sync match shIfSync groupthere NONE "\<fi\>"In this example, the NONE tells Vim that you are not in any special syntax region. In particular, you are not inside an if block.
:syntax sync match xSpecial /{.*}/More about synchronizing in the reference manual: :syn-sync.
:syntax keyword cType off_t uintWrite the file with the same name as the original syntax file. In this case "c.vim". Place it in a directory near the end of 'runtimepath'. This makes it loaded after the original syntax file. For Unix this would be:
" Vim syntax file " Language: C " Maintainer: Bram Moolenaar <[email protected]> " Last Change: 2001 Jun 18 " Remark: Included by the C++ syntax.Use the same layout as the other syntax files. Using an existing syntax file as an example will save you a lot of time.
if exists("b:current_syntax") finish endifSet "b:current_syntax" to the name of the syntax at the end. Don't forget that included files do this too, you might have to reset "b:current_syntax" if you include two files.
hi def link nameString String hi def link nameNumber Number hi def link nameCommand Statement ... etc ...Add the "display" argument to items that are not used when syncing, to speed up scrolling backwards and
CTRL-L
.