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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.