How to use the command pygmentize (with examples)
Pygmentize is a Python-based syntax highlighter that can be used to highlight the syntax of various programming languages. It provides a command-line interface for easy integration into scripts and workflows. This article will guide you through several use cases of the pygmentize command.
Use case 1: Highlight file syntax and print to stdout
(language is inferred from the file extension)
Code:
pygmentize file.py
Motivation: This use case is useful when you want to quickly view the syntax-highlighted version of a file in your terminal. By simply providing the file name, pygmentize will automatically infer the programming language based on the file extension and print the highlighted result to the standard output.
Explanation: In this example, we use the command pygmentize
followed by the name of the file (file.py
). Pygmentize will detect the file extension (.py
in this case) and apply the appropriate syntax highlighting for Python. The output will be displayed directly in the terminal.
Example output:
def greet(name):
print("Hello, " + name)
greet("World")
Use case 2: Explicitly set the language for syntax highlighting
Code:
pygmentize -l javascript input_file
Motivation: Sometimes, the file extension may not accurately represent the programming language used in the file. In such cases, it is beneficial to explicitly specify the language for syntax highlighting.
Explanation: In this example, we use the command pygmentize
with the -l
option followed by the desired programming language (javascript
). We also provide the name of the input file (input_file
). By specifying the language, pygmentize will apply syntax highlighting based on the chosen language, regardless of the file extension.
Example output:
function calculateSum(a, b) {
return a + b;
}
console.log(calculateSum(3, 5));
Use case 3: List available lexers (processors for input languages)
Code:
pygmentize -L lexers
Motivation: It can be helpful to know the available lexers supported by pygmentize when dealing with a wide variety of programming languages.
Explanation: By using the -L
option followed by lexers
, pygmentize will list all the available lexers (i.e., processors for input languages) supported by the tool. This allows you to see the range of languages you can work with when using pygmentize for syntax highlighting.
Example output:
ABAP : *.abap
ActionScript : *.as
Ada : *.ads, *.adb, *.ada
...
Use case 4: Save output to a file in HTML format
Code:
pygmentize -f html -o output_file.html input_file.py
Motivation: Saving the syntax-highlighted output to an HTML file allows you to later view the output in a web browser or embed it in a webpage.
Explanation: In this example, we use the -f
option followed by html
to specify the output format as HTML. The -o
option is used to specify the name of the output file (output_file.html
). Finally, we provide the name of the input file (input_file.py
). Pygmentize will convert the syntax-highlighted output into HTML format and save it to the specified file.
Example output:
<pre class="syntax"><code><span class="kd">def</span> <span class="nf">greet</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s">"Hello, "</span> <span class="o">+</span> <span class="n">name</span><span class="p">)</span>
<span class="n">greet</span><span class="p">(</span><span class="s">"World"</span><span class="p">)</span>
</code></pre>
Use case 5: List available output formats
Code:
pygmentize -L formatters
Motivation: Knowing the available output formats provided by pygmentize allows you to choose the format that best suits your needs.
Explanation: By using the -L
option followed by formatters
, pygmentize will list all the available output formats. This command-line option shows the different formats you can use when saving or displaying the syntax-highlighted output.
Example output:
TerminalFormatter : terminal, ansi, plain, colorful
HtmlFormatter : html, html4, html5, htmlpre, htmlfull, htmlfull, style
RtfFormatter : rtf
...
Use case 6: Output an HTML file, with additional formatter options (full page, with line numbers)
Code:
pygmentize -f html -O "full,linenos=True" -o output_file.html input_file
Motivation: When saving syntax-highlighted output in HTML format, you may want to include additional formatting options to enhance the readability of the code. In this example, we use the full
option to create a full HTML page, and the linenos=True
option to display line numbers alongside the highlighted code.
Explanation: Here, we use the -f
option followed by html
to specify the output format as HTML. The -O
option is used to provide additional formatter options enclosed in double quotes ("..."
). In this case, we use "full,linenos=True"
to create a complete HTML page with line numbers. The -o
option is used to specify the output file (output_file.html
). Finally, we provide the name of the input file (input_file
). The output will be an HTML file with the desired formatting options.
Example output:
<!DOCTYPE html>
<html>
<head>
<title>Code Highlighting Example</title>
<link rel="stylesheet" href="default.css">
</head>
<body>
<table class="highlighttable">
<tr>
<td class="linenos">
<div class="linenodiv">
...
</td>
<td class="code">
<div class="codediv">
...
</td>
</tr>
</table>
</body>
</html>
Conclusion:
In this article, we explored several use cases of the pygmentize command, which is a powerful Python-based syntax highlighter. By using the examples provided, you can effectively highlight syntax, save output in various formats, and customize the formatting of your code. Pygmentize provides an easy-to-use command-line interface for integrating syntax highlighting into your scripts and workflows, making it a valuable tool for developers and other users who work with code.