Define syntax highlighting in CotEditor on Mac

Syntax highlighting helps you distinguish different elements in a document by displaying them in different styles. In CotEditor, you can define which parts of text are highlighted and how they appear by configuring syntax highlighting rules.

CotEditor already defines the element types used for syntax highlighting in each theme. These elements are:

ElementDescriptionExamples
Keywords

Keywords in source code such as reserved words of programming languages.

for, while, if
Commands

Functions or commands in source code.

print, filter, func()
Types

Types in source code or headings in structured language.

int, bool, Object
Attributes

Attributes in source code.

<html lang="ja">, @classmethod
Variables

Variables in source code.

$foo, self
Values

Value constants in source code.

Null, True, False
Numbers

Numeric constants in source code.

Int i = 0;
Strings

String constants in source code.

String s = "Hello World"
Characters

Character constants in source code.

Char c = 'a'
Comments

Comments in source code.

/* 〜 */, # 〜

CotEditor colors syntax elements from top to bottom in the list, such as Keywords, Commands, and Types. If a document contains text that matches multiple categories (for example, both a keyword and a command) the style of the element that appears later in the order overrides the earlier one. The names of the elements are provided for reference and can also be used for other grammatical purposes.

Define a syntax highlighting rule

To define highlighting rules for a syntax, select a syntax element from the list on the sidebar of the syntax editor, then add highlighting conditions to the list on the right. For instructions on opening the syntax editor, see Define a syntax.

To define how text is highlighted, register one of the following as a highlighting condition for each element: a specific piece of text, a pair of start and end strings, or a regular expression pattern.

Highlighting rules are determined by the following settings:

FieldDescription
RE

If selected, both the begin and end strings are treated as regular expression patterns (RE stands for Regular Expression).

IC

If selected, the search ignores case (IC stands for Ignore Case). For example, if you set “abc” as the begin string and turn on IC, occurrences such as “ABC,” “abc,” and “aBc” in your document all match.

Begin String

The text that marks the beginning of the search pattern. This field is required for defining a syntax element to highlight.

End String

The text that marks the end of the search pattern. This field may be left blank.

Multiline

If selected, the search allows matches spanning multiple lines.

Description

A note or comment for your reference. The description doesn’t affect syntax highlighting and may be left blank.

By defining conditions:

A condition whose end string is blank and whose regular expression option is not selected is also used for completion candidates.

See also