Make your code look beautiful with Pygmentize

Ever wanted your source code listings to look the same in your blog as in your favorite IDE? It’s now possible with a Python syntax highlighter, called Pygmentize.

Pygmentize is a syntax highlighter written in Python. It can be used as a simple command line tool, as well as being reached from Python scripts.

In the following example I want to highlight a JavaScript file. I tell pygmentize to output HTML:

pygmentize -f html -o example.html example.js

The command above generated this output (I simply imported the HTML created by Pygmentize into my blog post).

if (time < 10)
{
    document.write("<b>Good morning</b>");
}
else
{
    document.write("<b>Good day</b>");
}


Take a closer look in the quickstart guide for more details on the command line tool, and how to use it in Python scripts. A package called Minted that utilizes pygmentize can be used to list source code in LaTeX. I used it in my Master thesis, and it looks so much better than the default lstlistings.

Leave a Reply