This Anki template offers two main features:
- A text field in which you can type in your answer. This differs from the build in Anki note type, because it allows multi line answers to be typed in and checked for correctness
- Dynamic code highlighting of the sample answer.
Install Anki add-on CrowdAnki:
- Open Anki on your computer, go to the Tools menu and select Add-ons.
- In the dialog box, click on Get Add-ons... and paste in the code provided on this page.
- Click on OK to install the add-on, and then restart Anki.
You're now ready to install the template:
- Go to the Releases page.
- In the latest release's Assets section, download the ZIP archive.
- Extract the content of the archive on your computer.
- Open Anki and make sure your devices are all synchronised.
- In the File menu, select CrowdAnki: Import from disk.
- Browse for and select the folder you extracted from the archive.
- Don't change anything in the CrowdAnki Import Settings dialog box that opens -- just press OK to start the import. A dialog box should then confirm that the import was successful.
Alternatively, you can download the .apkg file with a sample deck from here.
Your answer will be saved automatically on every keystroke, including deletions.
To exit the text field, press Esc or Ctrl (Anki Desktop) or click outside the text area. To compare your answer with the suggested solution, press Space or click on Anki's Show Answer button.
Text marked like this was typed by you into the text field, and does appear in the suggested Solution.
Text marked like this was not typed by you into the text field, but does appear in the suggested Solution.
Text marked like this was typed by you into the text field, but does not appear in the suggested Solution.
When adding new notes to your collection, make sure to specify the programming or markup language which the code snippet is written in. This is necessary to enable code highlighting by prism.js, which (currently) does not support auto-detection of the language used in a code snippet. It is important to use the exact text string OR a supported alias, which is recognized by prism.js. You can find out which names and aliases are recognized by prism.js on the website of prism.js. Add this piece of text to field 5 (named "Language") of the note.
If you want your code to not be color-highlighted, but still e.g. display whitespace characters, you can specify the language as "plain".
Replacing the themes for code highlighting:
- Download a theme from the main prism.js repository or get a wider range of themes from here.
- Prepend the file name with an underscore ("
_") to prevent Anki from auto-removing it when it checks for unused media files. - Place the downloaded file in your collections file directory.
- Reference the file in the loading script.
The main work is done by two libraries, without which I could not have created this template:
Comparing the typed answer with the solution is done by a slightly modified version of the diff part of diff_match_patch.js. Huge thanks to Neil Fraser and the other contributors for making that script available.
The code highlight is currently done by prism.js. Previously this template used highlight.js. This code is still available in a separate branch. I want to thank the contributors, of prism.js, as well as the contributors of highlight.js.
I also want to thank Arthur Milchior for writing a blog post, from which I first learned about the possibility of using highlight.js in an Anki template.
The CSS style sheets for highlighting the code were created by Jon Rohan and are maintained by Jan Pilzer.
I also want to mention the add-on Multi-Line Type Answer Box - 2. If you only need this functionality on the desktop version of Anki, this add-on should well.
Furthermore, I want to mention EmiliaClarkeGG who has already posted a template which lets you save your typed answer and display it on the back of the Card. If you just want to save your typed answer, without comparing it to text in your card, his template seems more straight forward and potentially all you need.
Syntax Highlighting (NG) Addon for Anki Desktop. Creates Static code highlighting.
Anki programming flashcards Script using pandoc to create static code highlighting from markdown files.
When multi line type checking has been requested and discussed in the past, some people have expressed the concern that it violates the minimum information principle.
In my opinion, the minimum information principle serves as a great guideline, and should be followed for the majority of your flashcards. It optimizes memorization and retention of information, and it also makes grading your answers easy.
Like for most principles, I think there may be legitimate reasons which make it justifiable to not follow the "Minimum information principle" in certain circumstances.
My own primary use case for the template is to train myself to replicate constructs of programming languages which span multiple lines.
The built-in type checking in Anki, which supports checking the answer for a single line, is sufficient for a large percentage of programming language constructs. For example, you can prompt yourself to type in language keywords ("What is the keyword for an integer variable in the C?") or combinations of certain language constructs ("Declare and initialize a string variable in C!") or functions of the standard library ("Which function of the C standard library is used to copy the value of one string variable into another string variable?").
However, certain language constructs can be rather awkward to prompt for, when you can only ask about a single line. In addition, it can get difficult to prompt for these lines without also giving too many hints as to what the answer might be (this is also an issue with {{type:cloze:field}} cards). Finally, I think there is a certain skill of "putting things together", which is not trained when asking about single lines of a multi line construct.
With that being said, I think multi line type checking flashcards have only a limited and auxiliary function. Learning about the language concepts by studying educational material and by creating your own projects should always be your first step ("understand before remembering"). And following that, creating and memorizing flashcards about the atomic parts of the programming language, as well as about the general concepts of computing, data structures and algorithms, math, etc. etc. should be the cornerstones for retaining your understanding.
I do think however, that multi line type checking flashcards can be used to train and/or remember language constructs which span a few lines. Because of this particular goal, it might also be worth thinking about employing a different repetition algorithm. For e.g. you might experiment with increasing the repetition steps in comparison to your regular flashcard. Because you are already memorizing the atomic parts of the programming language with different flashcards, repeating the multi line flashcards at the same rate might result in unnecessary duplication.
