
Syntax highlighting is a feature of
text editor
A text editor is a type of computer program that edits plain text. An example of such program is "notepad" software (e.g. Windows Notepad). Text editors are provided with operating systems and software development packages, and can be used to c ...
s that is used for
programming,
scripting, or
markup languages, such as
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
. The feature displays text, especially
source code
In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer.
Since a computer, at base, only ...
, in different
colours
Color (or colour in Commonwealth English; see spelling differences) is the visual perception based on the electromagnetic spectrum. Though color is not an inherent property of matter, color perception is related to an object's light absorpt ...
and
font
In metal typesetting, a font is a particular size, weight and style of a ''typeface'', defined as the set of fonts that share an overall design.
For instance, the typeface Bauer Bodoni (shown in the figure) includes fonts " Roman" (or "regul ...
s according to the category of terms. This feature facilitates writing in a structured language such as a programming language or a markup language as both structures and
syntax errors are visually distinct. This feature is also employed in many programming related contexts (such as programming manuals), either in the form of colourful books or online websites to make understanding code snippets easier for readers. Highlighting does not affect the meaning of the text itself; it is intended only for human readers.
Syntax highlighting is a form of
secondary notation, since the highlights are not part of the text meaning, but serve to reinforce it. Some editors also integrate syntax highlighting with other features, such as
spell checking or
code folding
Code or text folding, or less commonly holophrasting, is a feature of some graphical user interfaces that allows the user to selectively hide ("fold") or display ("unfold") parts of a document. This allows the user to manage large amounts of text ...
, as aids to editing which are external to the language.
Practical benefits

Syntax highlighting is one strategy to improve the readability and context of the text; especially for code that spans several pages. The reader can easily ignore large sections of comments or code, depending on what they are looking for.
Syntax highlighting also helps programmers find errors in their program. For example, most editors highlight
string literal
string literal or anonymous string is a literal for a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally "bracketed delimiters", as in x = "foo", where , "foo ...
s in a different colour. Consequently, spotting a missing
delimiter
A delimiter is a sequence of one or more Character (computing), characters for specifying the boundary between separate, independent regions in plain text, Expression (mathematics), mathematical expressions or other Data stream, data streams. An ...
is much easier because of the contrasting colour of the text.
Brace matching is another important feature with many popular editors. This makes it simple to see if a brace has been left out or locate the match of the brace the cursor is on by highlighting the pair in a different colour.
A study published in the conference
PPIG evaluated the effects of syntax highlighting on the comprehension of short programs, finding that the presence of syntax highlighting significantly reduces the time taken for a programmer to internalise the semantics of a program.
Additionally, data gathered from an eye-tracker during the study suggested that syntax highlighting enables programmers to pay less attention to standard syntactic components such as keywords.
Support in text editors

Some text editors can also export the coloured markup in a format that is suitable for printing or for importing into word-processing and other kinds of text-formatting software; for instance as a HTML, colourized LaTeX,
PostScript
PostScript (PS) is a page description language and dynamically typed, stack-based programming language. It is most commonly used in the electronic publishing and desktop publishing realm, but as a Turing complete programming language, it c ...
or
RTF version of its syntax highlighting. There are several syntax highlighting
libraries
A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
or "engines" that can be used in other applications, but are not complete programs in themselves, for example the Generic Syntax Highlighter (GeSHi) extension for
PHP.
For editors that support more than one language, the user can usually specify the language of the text, such as
C,
LaTeX
Latex is an emulsion (stable dispersion) of polymer microparticles in water. Latices are found in nature, but synthetic latices are common as well.
In nature, latex is found as a wikt:milky, milky fluid, which is present in 10% of all floweri ...
,
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
, or the text editor can automatically recognize it based on the file extension or by scanning contents of the file. This automatic language detection presents potential problems.
For example, a user may want to edit a document containing:
* more than one language (for example when editing an
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
file that contains embedded
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
code),
* a language that is not recognized (for example when editing source code for an obscure or relatively new programming language),
* a language that differs from the file type (for example when editing source code in an extension-less file in an editor that uses file extensions to detect the language).
In these cases, it is not clear what language to use, and a document may not be highlighted or be highlighted incorrectly. Some tools, like Guesslang and PLangRec, are designed to detect the programming language from the source code.
Syntax elements
Most editors with syntax highlighting allow different colours and text styles to be given to dozens of different lexical sub-elements of syntax. These include keywords, comments, control-flow statements, variables, and other elements. Programmers often heavily customize their settings in an attempt to show as much useful information as possible without making the code difficult to read.
Called ''syntax decoration'', some editors also display certain syntactical elements in more visually pleasing ways, for example by replacing a pointer operator like
->
in source code by an actual arrow symbol (→), or changing
text decoration clues like /italics/, *boldface*, or _underline_ in source code comments by an actual ''italics'', boldface, or
underlined presentation.
Examples
Below is a comparison of a
snippet of
C code:
Below is another snippet of syntax highlighted
C++ code:
// Create "window_count" Window objects:
const auto window_count = int;
auto windows = std::array, max_window_count>;
for (auto i = int; i < window_count; ++i)
In the C++ example, the editor has recognized the
keywords
const
,
auto
,
int
, and
for
. The
comment at the beginning is also highlighted in a specific manner to distinguish it from working code.
History and limitations
The ideas of syntax highlighting overlap significantly with those of
syntax-directed editors. One of the first such editors for code was Wilfred Hansen's 1969 code editor, Emily.
It provided advanced language-independent
code completion
Code completion is an autocompletion feature in many integrated development environments (IDEs) that speeds up the process of coding applications by fixing common mistakes and suggesting lines of code. This usually happens through popups while typ ...
facilities, and unlike modern editors with syntax highlighting, actually made it impossible to create syntactically incorrect programs.
In 1982, Anita H. Klock and Jan B. Chodak filed a patent for the first known syntax highlighting system, which was used in the Intellivision's
Entertainment Computer System (ECS) peripheral, released in 1983. It would highlight different elements of BASIC programs and was implemented in an attempt to make it easier for beginners, especially children, to start writing code. Later, the Live Parsing Editor (
LEXX
''Lexx'' (also known as ''LEXX: The Dark Zone Stories'' and ''Tales from a Parallel Universe'') is a science fiction television series created by Lex Gigeroff and brothers Paul Donovan (writer), Paul and Michael Donovan (producer), Michael Dono ...
) written for the
VM operating system for the computerization of the ''
Oxford English Dictionary
The ''Oxford English Dictionary'' (''OED'') is the principal historical dictionary of the English language, published by Oxford University Press (OUP), a University of Oxford publishing house. The dictionary, which published its first editio ...
'' in 1985 was one of the first to use colour syntax highlighting. Its ''live parsing'' capability allowed user-supplied parsers to be added to the editor, for text, programs, data file, etc. On
microcomputers, ''MacPascal 1.0'' (October 10, 1985) recognized Pascal syntax as it was typed and used font changes (e.g., bold for keywords) to highlight syntax on the monochrome
compact Macintosh and automatically indented code to match its structure.
Some text editors and code formatting tools perform syntax highlighting using
pattern matching
In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually must be exact: "either it will or will not be a ...
heuristics
A heuristic or heuristic technique (''problem solving'', '' mental shortcut'', ''rule of thumb'') is any approach to problem solving that employs a pragmatic method that is not fully optimized, perfected, or rationalized, but is nevertheless ...
(e.g.
Regular expressions
A regular expression (shortened as regex or regexp), sometimes referred to as rational expression, is a sequence of character (computing), characters that specifies a pattern matching, match pattern in string (computer science), text. Usually ...
) rather than implementing a
parser
Parsing, syntax analysis, or syntactic analysis is a process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar by breaking it into parts. The term '' ...
for each possible language. This can result in a text rendering system displaying somewhat inaccurate syntax highlighting and in some cases performing slowly. A solution used by text editors to overcome this problem is not always parsing the whole file but rather just the visible area, sometimes scanning backwards in the text up to a limited number of lines for "syncing".
On the other hand, the editor often displays code during its creation, while it is incomplete or incorrect, and the strict parsers (like ones used in compilers) would fail to parse the code most of the time.
Some modern, language-specific
IDEs (in contrast to text editors) perform full language parsing which results in very accurate understanding of code. An extension of syntax highlighting was called ''"semantic highlighting"'' in 2009 by David Nolden for the open-source C++ IDE
KDevelop. For example, semantic highlighting may give local variables unique distinct colours to improve the comprehensibility of code. In 2014 the idea of coloured local variables was further popularized due to a blog post by Evan Brooks, and after that, the idea was transferred to other popular IDEs like
Visual Studio,
Xcode
Xcode is a suite of developer tools for building apps on Apple devices. It includes an integrated development environment (IDE) of the same name for macOS, used to develop software for macOS, iOS, iPadOS, watchOS, tvOS, and visionOS. It w ...
,
and others.
Colour in a user interface is less useful if the user has some degree of
colour blindness.
See also
*
Programming features in a
Comparison of text editors
*
Indent style
In computer programming, indentation style is a convention or style, governing the indentation of lines of source code. An indentation style generally specifies a consistent number of whitespace characters before each line of a block, so that ...
*
Secondary notation
*
Structure editor
*
Parsing
Parsing, syntax analysis, or syntactic analysis is a process of analyzing a String (computer science), string of Symbol (formal), symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal gramm ...
*
Solarized (colour scheme)
References
{{DEFAULTSORT:Syntax Highlighting
Highlighting
User interface techniques
Text editor features
Articles with example C code