CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pygments

A syntax highlighting package that supports over 500 programming languages and text formats with extensive output format options

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

command-line.mddocs/

Command Line Interface

The pygmentize command-line tool provides syntax highlighting functionality directly from the terminal, supporting all lexers, formatters, and styles available in Pygments.

Capabilities

Basic Command Structure

pygmentize [options] [file]

The main entry point for command-line syntax highlighting.

Core Options

# Specify lexer (language)
pygmentize -l <lexer> [file]
pygmentize --lexer=<lexer> [file]

# Specify formatter (output format)  
pygmentize -f <formatter> [file]
pygmentize --formatter=<formatter> [file]

# Specify style (color scheme)
pygmentize -S <style> -f <formatter>
pygmentize --style=<style> --formatter=<formatter>

# Guess lexer from content
pygmentize -g [file]
pygmentize --guess-lexer [file]

# Output to file
pygmentize -o <output> [options] [file]
pygmentize --outfile=<output> [options] [file]

Option Passing

# Pass options to lexer
pygmentize -l python -P stripall=true -P tabsize=4 file.py

# Pass options to formatter
pygmentize -f html -O linenos=true -O cssclass=highlight file.py

# Multiple options
pygmentize -l python -f html -O linenos=true,cssclass=code file.py

Information Commands

# List all lexers
pygmentize -L lexers

# List all formatters
pygmentize -L formatters

# List all styles
pygmentize -L styles

# List all filters
pygmentize -L filters

# Get help for specific component
pygmentize -H lexer python
pygmentize -H formatter html
pygmentize -H filter codetagfilter

Usage Examples

Basic Highlighting

# Highlight Python file to HTML
pygmentize -l python -f html script.py

# Highlight with guessed lexer
pygmentize -g -f html script.py

# Highlight to terminal with colors
pygmentize -l python -f terminal script.py

Output to Files

# Save HTML output
pygmentize -l python -f html -o output.html script.py

# Generate LaTeX
pygmentize -l python -f latex -o document.tex script.py

# Create PNG image (requires Pillow)
pygmentize -l python -f png -o code.png script.py

Style Customization

# Use different color schemes
pygmentize -l python -f html -S monokai script.py
pygmentize -l python -f html -S github script.py  
pygmentize -l python -f html -S vim script.py

# Terminal with color schemes
pygmentize -l python -f terminal256 -S monokai script.py

Advanced Options

# HTML with line numbers and custom CSS class
pygmentize -l python -f html -O linenos=true,cssclass=highlight script.py

# HTML with inline styles (no external CSS)
pygmentize -l python -f html -O noclasses=true script.py

# Full HTML document
pygmentize -l python -f html -O full=true,title="My Code" script.py

# LaTeX with line numbers
pygmentize -l python -f latex -O linenos=true script.py

Filter Application

# Apply filters
pygmentize -l python -f html -F codetagfilter script.py
pygmentize -l python -f html -F keywordcase:case=upper script.py

# Multiple filters
pygmentize -l python -f html -F codetagfilter -F visiblewhitespace script.py

Stdin/Stdout Usage

# Read from stdin
echo "print('Hello')" | pygmentize -l python -f html

# Pipe output
pygmentize -l python -f html script.py | less

# Chain with other tools
cat script.py | pygmentize -g -f terminal | grep -A5 -B5 "function"

Output Formats

HTML Output

# Basic HTML
pygmentize -l python -f html script.py

# HTML with options
pygmentize -l python -f html -O linenos=true,cssclass=code,title="Code" script.py

Terminal Output

# Basic terminal colors (16 colors)
pygmentize -l python -f terminal script.py

# 256-color terminal
pygmentize -l python -f terminal256 script.py

# True color terminal (24-bit)
pygmentize -l python -f terminal16m script.py

Document Formats

# LaTeX
pygmentize -l python -f latex script.py

# RTF (Rich Text Format)
pygmentize -l python -f rtf script.py

# SVG vector graphics
pygmentize -l python -f svg script.py

Image Formats

# PNG image (requires Pillow)
pygmentize -l python -f png -O font_size=14 script.py

# JPEG image
pygmentize -l python -f jpeg script.py

# GIF image
pygmentize -l python -f gif script.py

Configuration

Style Generation

Generate CSS for HTML output:

# Generate CSS for a style
pygmentize -S default -f html > style.css
pygmentize -S monokai -f html > dark.css

# Use generated CSS with HTML
pygmentize -l python -f html -O cssfile=style.css script.py

Batch Processing

# Process multiple files
for file in *.py; do
    pygmentize -l python -f html -o "${file%.py}.html" "$file"
done

# Using find
find . -name "*.py" -exec pygmentize -l python -f html -o {}.html {} \;

Integration Examples

# Git diff with syntax highlighting
git show | pygmentize -l diff -f terminal

# Highlight code in README
pygmentize -l python -f html code_sample.py > code.html

# Generate documentation
pygmentize -l python -f latex -O full=true,title="API Reference" api.py > api.tex

Error Handling

Common command-line errors:

# Unknown lexer
pygmentize -l nonexistent file.py
# Error: no lexer for alias 'nonexistent' found

# Unknown formatter  
pygmentize -l python -f badformat file.py
# Error: no formatter found for name 'badformat'

# File not found
pygmentize -l python nonexistent.py
# Error: cannot read file

# Invalid options
pygmentize -l python -f html -O badoption=true file.py
# Error: unknown option 'badoption'

Environment Variables

# Default options
export PYGMENTIZE_STYLE=monokai
export PYGMENTIZE_FORMATTER=terminal256

# Use in scripts
pygmentize -l python file.py  # Uses environment defaults

Integration with Other Tools

Vim Integration

# In .vimrc
command! -range=% Highlight :<line1>,<line2>w !pygmentize -l python -f html

Less Integration

# Use as LESSOPEN processor
export LESSOPEN="| pygmentize -g %s"
less script.py  # Automatically highlighted

Shell Functions

# Bash function for quick highlighting
highlight() {
    pygmentize -g -f terminal256 "$1" | less -R
}

# Usage: highlight script.py

Install with Tessl CLI

npx tessl i tessl/pypi-pygments

docs

command-line.md

custom-components.md

filter-system.md

formatter-management.md

high-level-api.md

index.md

lexer-management.md

style-management.md

tile.json