pygmentize
Highlight source code syntax
TLDR
Highlight file syntax and print to stdout (language is inferred from the file extension)
Explicitly set the language for syntax highlighting
List available lexers (processors for input languages)
Save output to a file in HTML format
List available output formats
Output an HTML file, with additional formatter options (full page, with line numbers)
SYNOPSIS
pygmentize [options] [filename]
PARAMETERS
-L
List available lexers, formatters, or styles.
-f
Specify the formatter to use (e.g., html, terminal). Default is 'terminal'.
-l
Specify the lexer to use (e.g., python, c++). If not specified, pygmentize will try to guess it from the filename.
-s
Specify the style to use for highlighting (e.g., default, solarized-dark). Default is 'default'.
-O
Set formatter/lexer options (e.g., linenos=1, title='My Code').
-g
Guess lexer from filename (same as not specifying -l).
-v
Show the pygments version number.
-h
Show help message and exit
Input file to highlight. If not specified, pygmentize reads from standard input.
-o
Output file to write the highlighted code to. If not specified, pygmentize writes to standard output.
DESCRIPTION
pygmentize
is a generic syntax highlighter suitable for use in command-line tools and other applications. It takes source code or other text as input and outputs a highlighted version of it, using a lexer to determine the language and a formatter to control the output format (e.g., HTML, LaTeX, terminal ANSI colors).
It supports a wide variety of languages and output formats. This versatility makes it useful for tasks such as generating formatted code snippets for websites, producing colored terminal output for scripts, and preparing code for inclusion in documents.
EXIT STATUS
pygmentize
returns 0 on success, and a non-zero value on error.
LEXER GUESSING
When a lexer isn't explicitly specified with the -l
option, pygmentize
attempts to determine the appropriate lexer based on the input filename's extension or the file content. While generally effective, this guessing mechanism might not always be accurate, particularly with files lacking a standard extension or ambiguous content. Explicitly specifying the lexer guarantees the correct highlighting.
HISTORY
pygmentize
is part of the Pygments library, a general-purpose syntax highlighting package written in Python. Pygments has been developed over time and has become a popular choice for highlighting code in various applications and websites due to its flexibility and wide language support.
SEE ALSO
highlight(1)