Home Explore Blog CI



neovim

2nd chunk of `runtime/doc/usr_44.txt`
2e583f456a83dbbb232509c78a6697bbe7dcd42410a40e510000000100000fa1
 keyword, use the
following form: >

	: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 endif

This 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 Statement

This tells Vim to highlight "xType" like "Type" and "xStatement" like
"Statement".  See |group-name| for the standard names.


UNUSUAL KEYWORDS

The characters used in a keyword must be in the 'iskeyword' option.  If you
use another character, the word will never match.  Vim doesn't give a warning
message for this.
   The x language uses the '-' character in keywords.  This is how it's done:
>
	:setlocal iskeyword+=-
	:syntax keyword xStatement when-not

The ":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).

The x language allows for abbreviations.  For example, "next" can be
abbreviated to "n", "ne" or "nex".  You can define them by using this command:
>
	:syntax keyword xStatement n[ext]

This doesn't match "nextone", keywords always match whole words only.

==============================================================================
*44.3*	Matches

Consider defining something a bit more complex.  You want to match ordinary
identifiers.  To do this, you define a match syntax item.  This one matches
any word consisting of only lowercase letters: >

	:syntax match xIdentifier /\<\l\+\>/
<
	Note:
	Keywords overrule any other syntax item.  Thus the keywords "if",
	"then", etc., will be keywords, as defined with the ":syntax keyword"
	commands above, even though they also match the pattern for
	xIdentifier.

The part at the end is a pattern, like it's used for searching.  The // is
used to surround the pattern (like how it's done in a ":substitute" command).
You can use any other character, like a plus or a quote.

Now define a match for a comment.  In the x language it is anything from # to
the end of a line: >

	: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.

==============================================================================
*44.4*	Regions

In the example x language, strings are enclosed in double quotation marks (").
To highlight strings you define a region.  You need a region start (double
quote) and a region end (double quote).  The definition is as follows: >

	: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?

	"A string with a double quote (\") in it" ~

This creates a problem: The double quotation marks in the middle of the string
will end the region.  You need to tell Vim to skip over any escaped double
quotes in the string.  Do this with the skip keyword: >

	:syntax region xString start=/"/ skip=/\\"/ end=/"/

The double backslash matches a single backslash, since the backslash is a
special character in search patterns.

When to

Title: Keywords, Matches, and Regions in Syntax Highlighting
Summary
This section details how to define syntax highlighting elements in Vim, including keywords, matches, and regions. It covers defining keywords with `:syntax keyword`, unusual keyword characters using the 'iskeyword' option, matching patterns with `:syntax match`, and defining regions with start and end patterns using `:syntax region`, along with skipping escaped characters.