Styles

Pygments comes withsome builtin styles that work for both theHTML and LaTeX formatter.

The builtin styles can be looked up with theget_style_by_name function:

>>>frompygments.stylesimportget_style_by_name>>>get_style_by_name('colorful')<class 'pygments.styles.colorful.ColorfulStyle'>

You can pass a instance of aStyle class to a formatter as thestyleoption in form of a string:

>>>frompygments.stylesimportget_style_by_name>>>frompygments.formattersimportHtmlFormatter>>>HtmlFormatter(style='colorful').style<class 'pygments.styles.colorful.ColorfulStyle'>

Or you can also import your own style (which must be a subclass ofpygments.style.Style) and pass it to the formatter:

>>>fromyourapp.yourmoduleimportYourStyle>>>frompygments.formattersimportHtmlFormatter>>>HtmlFormatter(style=YourStyle).style<class 'yourapp.yourmodule.YourStyle'>

Creating Own Styles

SeeWrite your own style.

Builtin Styles

Pygments ships some builtin styles which are maintained by the Pygments team.

To get a list of known styles you can use this snippet:

>>>frompygments.stylesimportSTYLE_MAP>>>STYLE_MAP.keys()['default', 'emacs', 'friendly', 'colorful']

Getting a list of available styles

Added in version 0.6.

Because it could be that a plugin registered a style, there isa way to iterate over all styles:

>>>frompygments.stylesimportget_all_styles>>>styles=list(get_all_styles())

Terminal Styles

Added in version 2.2.

Custom styles used with the 256-color terminal formatter can also map colors touse the 8 default ANSI colors. To do so, useansigreen,ansibrightred orany other colors defined inpygments.style.ansicolors. Foreground ANSIcolors will be mapped to the correspondingescape codes 30 to 37 thus respecting anycustom color mapping and themes provided by many terminal emulators. Lightvariants are treated as foreground color with and an added bold flag.bg:ansi<color> will also be respected, except the light variant will be thesame shade as their dark variant.

See the following example where the color of the string"helloworld" isgoverned by the escape sequence\x1b[34;01m (Ansi bright blue, Bold, 41 being redbackground) instead of an extended foreground & background color.

>>>frompygmentsimporthighlight>>>frompygments.styleimportStyle>>>frompygments.tokenimportToken>>>frompygments.lexersimportPython3Lexer>>>frompygments.formattersimportTerminal256Formatter>>>classMyStyle(Style):        styles = {            Token.String:     'ansibrightblue bg:ansibrightred',        }>>>code='print("Hello World")'>>>result=highlight(code,Python3Lexer(),Terminal256Formatter(style=MyStyle))>>>print(result.encode())b'\x1b[34;41;01m"\x1b[39;49;00m\x1b[34;41;01mHello World\x1b[39;49;00m\x1b[34;41;01m"\x1b[39;49;00m'

Colors specified usingansi* are converted to a default set of RGB colorswhen used with formatters other than the terminal-256 formatter.

By definition of ANSI, the following colors are considered “light” colors, andwill be rendered by most terminals as bold:

  • “brightblack” (darkgrey), “brightred”, “brightgreen”, “brightyellow”, “brightblue”,“brightmagenta”, “brightcyan”, “white”

The following are considered “dark” colors and will be rendered as non-bold:

  • “black”, “red”, “green”, “yellow”, “blue”, “magenta”, “cyan”,“gray”

Exact behavior might depends on the terminal emulator you are using, and itssettings.

Changed in version 2.4.

The definition of the ANSI color names has changed.New names are easier to understand and align to the colors used in other projects.

New names

Pygments up to 2.3

ansiblack

#ansiblack

ansired

#ansidarkred

ansigreen

#ansidarkgreen

ansiyellow

#ansibrown

ansiblue

#ansidarkblue

ansimagenta

#ansipurple

ansicyan

#ansiteal

ansigray

#ansilightgray

ansibrightblack

#ansidarkgray

ansibrightred

#ansired

ansibrightgreen

#ansigreen

ansibrightyellow

#ansiyellow

ansibrightblue

#ansiblue

ansibrightmagenta

#ansifuchsia

ansibrightcyan

#ansiturquoise

ansiwhite

#ansiwhite

Old ANSI color names are deprecated but will still work.