How to add source code to flashcard in Anki

Syntax Highlighting for Code is an addon for Anki which enables you to add source code or pseudo-code to a flash card question or answer section. This is necessary for source code because Anki centers all text and that makes any source code text you add to a flash card impossible to understand!

This addon also automatically adds the appropriate syntax highlighting colors to the text. There is a huge variety of languages that it supports for this highlighting.

  • This addon depends on Pygments. Note that installing the latest version from PyPI using Pip caused errors with the bytes package. So, I installed the older version from Ubuntu repositories:
$ sudo apt install python-pygments
  • This addon used to be available from the Anki addons webpage with the code 491274358. It is no longer available there. But, we can install it from its source code. First, we get the code:
$ git clone
  • Copy the contents of the above directory to the Anki addons directory. On my system, this is the directory ~/Documents/Anki/addons

  • Close down Anki and start it up again.

  • Before you add any source code to a flashcard, indent it correctly in an external editor. Copy it to the system clipboard.

  • In the Add dialog, you will find a yellow lightning button and a languages dropdown beside it. Choose the language for syntax highlighting from dropdown. Click the lightning button and the source code from your system clipboard is processed through Pygments and the output HTML code is pasted to the flashcard. You can see that it looks great! 🙂

  • This addon adds line numbers to the code by default. To turn it off, go to Tools -> Syntax Highlighting. Switch it off in the options.

Tried with: Anki 2.0.33 and Ubuntu 14.04

How to get syntax highlighting for cat using pygments

Some people like to quickly have a look at short source files using cat at the terminal. It would be nice to have syntax highlighting for such a use. Though cat does not support syntax highlighting, it is easy to get a similar experience using Pygments, a syntax highlighter written in Python.

First, install Pygments using pip:

$ sudo pip install Pygments

This installs a pygmentize program, whose -g can be used to request it to guess the type of input file and apply the appropriate syntax highlighting:

$ pygmentize -g foobar.cpp

If you use this regularly, create an alias for it with a name that is easy to remember like ccat.

Tried with: Pygments 1.6, Python 2.7.3 and Ubuntu 12.04