LinuxCommandLibrary

pygmentize

Highlight source code syntax

TLDR

Highlight file syntax and print to stdout (language is inferred from the file extension)

$ pygmentize [file.py]
copy

Explicitly set the language for syntax highlighting
$ pygmentize -l [javascript] [input_file]
copy

List available lexers (processors for input languages)
$ pygmentize -L lexers
copy

Save output to a file in HTML format
$ pygmentize -f html -o [output_file.html] [input_file.py]
copy

List available output formats
$ pygmentize -L formatters
copy

Output an HTML file, with additional formatter options (full page, with line numbers)
$ pygmentize -f html -O "full,linenos=True" -o [output_file.html] [input_file]
copy

SYNOPSIS

pygmentize [options] [file ...]

PARAMETERS

-l <lexer>, --lexer=<lexer>
    Explicitly define the language or lexer to use for syntax highlighting (e.g., python, javascript, xml).

-F <formatter>, --formatter=<formatter>
    Choose the output format (e.g., html, terminal, latex, rtf, svg). The default is typically terminal or html if output is redirected.

-s <style>, --style=<style>
    Apply a specific highlighting style or theme (e.g., default, monokai, solarized-light). Use -L styles to see available options.

-o <outfile>, --outfile=<outfile>
    Write the highlighted output to the specified file instead of standard output.

-O <options>, --options=<options>
    Pass comma-separated options directly to the lexer or formatter (e.g., linenos=True,cssclass=source).

-L <type>, --list-options=<type>
    List available lexers, formatters, styles, or filters. <type> can be lexers, formatters, styles, or filters.

--detect-lexer
    Attempt to automatically detect the lexer based on the file content or its extension. This is useful when the language is unknown.

-v, --verbose
    Print extra information to standard error, useful for debugging or understanding processing steps.

-V, --version
    Show the program's version number and exit.

-h, --help
    Show a help message describing command-line options and exit.

-f
    Read filenames from standard input, one filename per line, instead of from command-line arguments. Useful for batch processing.

DESCRIPTION

Pygmentize is the command-line utility provided by the Pygments project, a versatile syntax highlighting library written in Python. It supports an extensive array of programming languages, markup formats, and configuration files, offering highly customizable output formats such as HTML, LaTeX, RTF, and ANSI escape codes for terminal display. The pygmentize command allows users to apply this powerful highlighting capability directly from the terminal. It reads input from specified files or standard input, processes it according to the chosen lexer (language detector) and formatter (output style), and then prints the highlighted content to standard output. This makes it ideal for integrating syntax highlighting into scripts, documentation generation, or for quick terminal viewing of code with enhanced readability. Its flexibility in handling diverse input types and generating various output formats makes it an indispensable tool for developers and content creators.

CAVEATS

Lexer Detection Accuracy: While pygmentize offers automatic lexer detection, it may not always be perfect, especially for ambiguous file types or very short code snippets. Explicitly specifying the lexer with -l is often more reliable.

Terminal Output Limitations: When using terminal formatters, the display quality depends heavily on the terminal emulator's support for ANSI escape codes and its color capabilities. Some styles might not render well or might be unreadable in certain terminal environments.

Dependencies: pygmentize is a Python-based tool and requires the Pygments library to be installed in your Python environment. Its functionality is directly tied to the capabilities and versions of the installed Pygments package.

LISTING AVAILABLE OPTIONS

To see the full list of supported lexers, formatters, styles, or filters, use the -L or --list-options flag followed by the type (e.g., pygmentize -L lexers, pygmentize -L styles). This is crucial for discovering what's available for your highlighting needs.

COMMON USAGE PATTERNS

Here are a few examples of how pygmentize can be used:

Highlighting a file to HTML:
pygmentize -l python -F html -o output.html myfile.py

Highlighting standard input to terminal with a specific style:
cat myfile.js | pygmentize -l javascript -F terminal256 -s monokai

Using automatic lexer detection for a file:
pygmentize --detect-lexer -F terminal myfile.rb

HISTORY

Pygments was created by Georg Brandl in 2006. It quickly gained popularity as a robust and extensible syntax highlighting library. pygmentize is the command-line interface provided as part of the Pygments distribution, allowing users to leverage its powerful features outside of a programmatic context. Its development has been continuous, adapting to new languages and evolving output formats, maintaining its status as a widely used tool in the Python ecosystem and beyond. Its widespread adoption is due to its accuracy, extensive language support, and flexibility in output formats.

SEE ALSO

cat(1), less(1), bat(1), highlight(1)

Copied to clipboard