Syntax highlighting is the feature to color words in a document depending on specific rules. A typical use is to colorize programming source code according to the grammatical function of terms, such as commands or variables, to make the structure of the program easier to understand. Other uses are coloring headings in structured languages such as Markdown, making certain characters that should not be used stand out, and many other uses depending on the type of document.
In CotEditor, a syntax rules the semantic meaning of the syntax highlighting, and the color scheme used in the actual highlighting is defined as a theme.
To define highlighting defines for a syntax, select a syntax element to add a highlighting condition from the list on the left of the syntax editor, then add conditions to the list on the right side. See Define a syntax for how to open the syntax editor.
CotEditor predefines the types of elements for the syntax highlighting already for the theme. They are:
| Element | Description | Examples |
|---|---|---|
| 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 the elements from the top to the bottom: like Keywords, Commands, Types, and so on. When the document contains an item matching multiple categories (for example, keywords and commands), the color of the latter in the order overwrites the former’s. The name of each element is just for reference and thus can actually apply also to other grammatical uses.
To define the way to highlight, register either a specific string, a pair of the start and end strings, or a regular expression pattern as a highlighting condition for each highlighting element.
Highlighting rules are determined by the following settings:
| Field | Description |
|---|---|
| RE | If selected, both the begin and end strings are considered as regular expressions (RE stands for Regular Expression). |
| IC | If selected, the case is ignored for searching (IC stands for Ignore Case). For example, if you set “abc” as the begin string and select IC, “ABC” or “abc” or “Abc” in your document will be matched. |
| Begin String | A string that denotes the beginning of the search pattern. This is required to search the code elements you want to be colored. |
| End String | A string that denotes the end of the search pattern. This field can be left blank. |
| Description | You can use this field to leave a comment about the term. Description doesn’t affect syntax highlighting. This field can be left blank. |
By defining conditions:
A condition whose end string is blank and the regular expression option is unselected is used for defining the completion list as well.