| name | syntect |
| description | Expert knowledge for syntax highlighting in Rust with syntect — Sublime Text syntax definitions and TextMate themes producing ANSI-terminal and HTML output, including TypeScript support and modern themes. Use when highlighting code for terminal or HTML output, loading custom syntaxes or themes, or troubleshooting highlighting performance. |
| last_updated | "2025-12-24T00:00:00.000Z" |
| hash | 285587f8ac19de6c |
Syntect
Syntect is a high-performance syntax highlighting library for Rust that uses Sublime Text syntax definitions (.sublime-syntax) and TextMate themes (.tmTheme). It's the industry standard for Rust-based syntax highlighting, used in bat, delta, and various static site generators.
Core Principles
- Two-stage pipeline: Parsing (text → scopes) then Highlighting (scopes → styles)
- Line-by-line processing: Process code incrementally for memory efficiency
- Pre-load defaults: Use
SyntaxSet::load_defaults_newlines() and ThemeSet::load_defaults() for convenience
- Binary dumps for production: Compile syntaxes/themes to binary at build time for <5ms startup
- ANSI for terminals: Use
as_24_bit_terminal_escaped() for 24-bit color terminal output
- HTML has two modes: Inline styles (self-contained) or CSS classes (better performance/theme switching)
- Streaming for large files: Use
HighlightFile for memory-efficient file processing
- Always reset terminal: Print
\x1b[0m after ANSI output to reset colors
Setup
[dependencies]
syntect = "5.2"
Quick Reference
Terminal Output (ANSI Escape Codes)
use syntect::easy::HighlightLines;
use syntect::parsing::SyntaxSet;
use syntect::highlighting::ThemeSet;
use syntect::util::{as_24_bit_terminal_escaped, LinesWithEndings};
let ps = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults();
let syntax = ps.find_syntax_by_extension("rs").unwrap();
let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
let code = "fn main() {}\n";
for line in LinesWithEndings::from(code) {
let ranges = h.highlight_line(line, &ps).unwrap();
print!("{}", as_24_bit_terminal_escaped(&ranges[..], false));
}
println!("\x1b[0m");
HTML Output (Inline Styles)
use syntect::html::highlighted_html_for_string;
use syntect::parsing::SyntaxSet;
use syntect::highlighting::ThemeSet;
let ps = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults();
let syntax = ps.find_syntax_by_extension("rs").unwrap();
let theme = &ts.themes["base16-ocean.dark"];
let html = highlighted_html_for_string("let x = 5;", &ps, syntax, theme).unwrap();
HTML Output (CSS Classes)
use syntect::html::{ClassedHTMLGenerator, ClassStyle};
use syntect::parsing::SyntaxSet;
use syntect::util::LinesWithEndings;
let ps = SyntaxSet::load_defaults_newlines();
let syntax = ps.find_syntax_by_extension("rs").unwrap();
let mut html_gen = ClassedHTMLGenerator::new_with_class_style(
syntax, &ps, ClassStyle::Spaced
);
for line in LinesWithEndings::from("fn main() {}") {
html_gen.parse_html_for_line_which_includes_newline(line).unwrap();
}
let html = html_gen.finalize();
Topics
Output Targets
Language Support
Optimization
- Binary Dumps - Pre-compile syntaxes/themes for fast startup, modern themes integration
Common Patterns
Finding a Syntax by Extension
let ps = SyntaxSet::load_defaults_newlines();
let syntax = ps.find_syntax_by_extension("rs").unwrap();
Finding a Syntax by First Line (Shebang)
let syntax = ps.find_syntax_by_first_line("#!/usr/bin/env python3");
Available Default Themes
Common themes in ThemeSet::load_defaults():
base16-ocean.dark
base16-ocean.light
InspiredGitHub
Solarized (dark)
Solarized (light)
When to Use Syntect
| Use Case | Choose Syntect When |
|---|
| CLI tools | Need fast, accurate terminal highlighting |
| Static site generators | Generating HTML from code blocks |
| Documentation tools | Rendering code examples in docs |
| Code viewers | Displaying source code with syntax colors |
Alternatives:
- Tree-sitter: For semantic analysis, ASTs, or IDE features (slower for pure highlighting)
- Pygments (Python): For Python-based tools (much slower, requires Python bridge)
Resources