alecthomas/chroma is tracked by TopGit as a backend project, with 5.0k stars on GitHub, written primarily in Go. A general purpose syntax highlighter in pure Go
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
[!NOTE]
v3 alpha available. Chroma v3 replaces the custom Iterator type with Go's built-in iter.Seq[Token], removes the EOF sentinel, and bumps the module path to github.com/alecthomas/chroma/v3. See the v3.0.0-alpha.1 release for migration details.
A general purpose syntax highlighter in pure Go
Chroma takes source code and other structured text and converts it into syntax
highlighted HTML, ANSI-coloured text, etc.
Chroma is based heavily on Pygments, and includes
translators for Pygments lexers and styles.
V, V shell, Vala, VB.net, verilog, VHDL, VHS, VimL, vue
W
WDTE, WebAssembly Text Format, WebGPU Shading Language, WebVTT, Whiley
X
XML, Xorg
Y
YAML, YAML+Jinja, YANG
Z
Z80 Assembly, Zed, Zig
I will attempt to keep this section up to date, but an authoritative list can be
displayed with chroma --list.
Try it
Try out various languages and styles on the Chroma Playground.
Using the library
This is version 2 of Chroma, use the import path:
import "github.com/alecthomas/chroma/v3"
Chroma, like Pygments, has the concepts of
lexers,
formatters and
styles.
Lexers convert source text into a stream of tokens, styles specify how token
types are mapped to colours, and formatters convert tokens and styles into
formatted output.
A package exists for each of these, containing a global Registry variable
with all of the registered implementations. There are also helper functions
for using the registry in each package, such as looking up lexers by name or
matching filenames, etc.
In all cases, if a lexer, formatter or style can not be determined, nil will
be returned. In this situation you may want to default to the Fallback
value in each respective package, which provides sane defaults.
Quick start
A convenience function exists that can be used to simply format some source
text, without any effort:
In all cases, nil will be returned if the language can not be identified.
if lexer == nil {
lexer = lexers.Fallback
}
At this point, it should be noted that some lexers can be extremely chatty. To
mitigate this, you can use the coalescing lexer to coalesce runs of identical
token types into a single token:
lexer = chroma.Coalesce(lexer)
Formatting the output
Once a language is identified you will need to pick a formatter and a style (theme).
By default the html registered formatter generates standalone HTML with
embedded CSS. More flexibility is available through the formatters/html package.
Firstly, the output generated by the formatter can be customised with the
following constructor options:
Standalone() - generate standalone HTML with embedded CSS.
WithClasses() - use classes rather than inlined style attributes.
ClassPrefix(prefix) - prefix each generated CSS class.
TabWidth(width) - Set the rendered tab width, in characters.
WithLineNumbers() - Render line numbers (style with LineNumbers).
WithLinkableLineNumbers() - Make the line numbers linkable and be a link to themselves.
HighlightLines(ranges) - Highlight lines in these ranges (style with LineHighlight).
WithLinePrompts(prompt, ranges) - Render non-selectable prompts before lines in these ranges.
LineNumbersInTable() - Use a table for formatting line numbers and code, rather than spans.
BaseLineNumber(n) - Set the first line number; highlight and prompt ranges follow it.
WrapLongLines(true) - Wrap long lines instead of scrolling.
PreventSurroundingPre(true) - Emit the highlighted spans without the surrounding <pre>.
InlineCode(true) - Generate output suitable for inline <code> elements.
WithPreWrapper(wrapper) - Customise the wrapping <pre> element.
WithCustomCSS(css) - Add custom CSS per token type.
WithModeClasses(true) - Scope generated CSS by the style's light/dark mode class.
If WithClasses() is used, the corresponding CSS can be obtained from the formatter with:
For light/dark theme switching, enable WithModeClasses(true) and write the
CSS for both styles into one stylesheet. Each rule is scoped by its style's
mode class (eg. .chroma.dark), so the theme can be toggled at runtime by
swapping the mode class on the wrapper element.
More detail
Lexers
Lexers are community maintained. Please use
Discussions for lexer
requests and lexer highlighting bugs.
See the Pygments documentation
for details on implementing lexers. Most concepts apply directly to Chroma,
but see existing lexer implementations for real examples.
In many cases lexers can be automatically converted directly from Pygments by
using the included Python 3 script pygments2chroma_xml.py. I use something like
the following:
uv run --script _tools/pygments2chroma_xml.py \
pygments.lexers.jvm.KotlinLexer \
> lexers/embedded/kotlin.xml
A list of all lexers available in Pygments can be found in pygments-lexers.txt.
Formatters
Chroma supports HTML output, as well as terminal output in 8 colour, 256 colour, and true-colour.
A noop formatter is included that outputs the token text only, and a tokens
formatter outputs raw tokens. The latter is useful for debugging lexers.
Styles
Chroma styles are defined in XML. The style entries use the
same syntax as Pygments. All Pygments styles have been converted to Chroma using the _tools/style.py
script.
Style names are case-insensitive. For example, monokai and Monokai are treated as the same style.
When you work with one of Chroma's styles,
know that the Background token type provides the default style for tokens. It does so
by defining a foreground color and background color.
For example, this gives each token name not defined in the style a default color
of #f8f8f8 and uses #000000 for the highlighted code block's background:
Also, token types in a style file are hierarchical. For instance, when CommentSpecial is not defined, Chroma uses the token style from Comment. So when several comment tokens use the same color, you'll only need to define Comment and override the one that has a different color.
For a quick overview of the available styles and how they look, check out the Chroma Style Gallery.
Command-line interface
A command-line interface to Chroma is included.
Binaries are available to install from the releases page.
The CLI can be used as a preprocessor to colorise output of less(1),
see documentation for the LESSOPEN environment variable.
The --fail flag can be used to suppress output and return with exit status
1 to facilitate falling back to some other preprocessor in case chroma
does not resolve a specific lexer to use for the given file. For example:
Replace cat with your favourite fallback preprocessor.
When invoked as .lessfilter, the --fail flag is automatically turned
on under the hood for easy integration with lesspipe shipping with
Debian and derivatives;
for that setup the chroma executable can be just symlinked to ~/.lessfilter.
Projects using Chroma
moor is a full-blown pager that colorizes
its input using Chroma
Hugo is a static site generator that uses Chroma for syntax
highlighting code examples
f4 is asynchronious cross platform Far Manager clone in Go
that uses Chroma for syntax highlighting in built-in editor
Testing lexers
If you edit some lexers and want to try it, open a shell in cmd/chromad and run:
go run . --csrf-key=securekey
A Link will be printed. Open it in your Browser. Now you can test on the Playground with your local changes.
If you want to run the tests and the lexers, open a shell in the root directory and run:
go test ./lexers
When updating or adding a lexer, please add tests. See lexers/README.md for more.
What's missing compared to Pygments?
Quite a few lexers, for various reasons (pull-requests welcome):
Pygments lexers for complex languages often include custom code to
handle certain aspects, such as Raku's ability to nest code inside
regular expressions. These require time and effort to convert.
I mostly only converted languages I had heard of, to reduce the porting cost.
Some more esoteric features of Pygments are omitted for simplicity.
Though the Chroma API supports content detection, very few languages support them.
I have plans to implement a statistical analyser at some point, but not enough time.
No homepage URL was recorded for alecthomas/chroma in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
How does alecthomas/chroma compare to other Backend projects?
alecthomas/chroma is tracked by TopGit in the Backend category, with 5.0k GitHub stars and written in Go. Browse the Backend topic page on TopGit to compare it against similar projects by stars and activity.
How many stars does alecthomas/chroma have?
alecthomas/chroma has 5.0k GitHub stars — refresh the page for the live number, or check github.com/alecthomas/chroma. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is alecthomas/chroma open source?
TopGit's metadata for alecthomas/chroma does not record a license. Most public repositories on GitHub ARE open source, but the exact terms vary — verify by opening the LICENSE file directly.
What else is in the Backend space?
alecthomas/chroma is tracked by TopGit under the Backend category, alongside 9 GitHub-tagged topics. Trending and Topics pages list peer repositories of comparable stars and language.
What is alecthomas/chroma?
alecthomas/chroma (alecthomas/chroma) is a Go project on GitHub. From the project's own README: A general purpose syntax highlighter in pure Go
Where do I read more about alecthomas/chroma?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/alecthomas/chroma is the definitive source.
Read full README in the tab above.
Want a second opinion on chroma?
Ask an AI that can read this page — one click and you get its take on chroma.