'Add Notes…' dialogue: replace selected text with (e.g.) all underscores?

(question to all geeks/nerds; probably not so much specifically an Anki question)

How could I with one keyboard shortcut replace the selected text with all underscores ?

This would save me from having to count characters and then typing each underscore separately … :frowning:

I want these underscores to serve as hints; indicating the approximate length of the phrase.

These hints are redundant. Remove them with “Find & Replace” (Treat input as regular expression):

image

Find: \{\{c(\d+)::([^{]+)::.[^{]*}}
Replace: {{c$1::$2}}

Then, add this script to your front template:

<script>
  setTimeout(() => {
    /**
    * Anki script: Replace clozes with blanks
    * For this to work, append <div id="clozeText" hidden>{{Text}}</div>
    * to your front template.
    * 
    * @author Matthias Metelka | @kleinerpirat
    */
    const cardIdx = parseInt(document.body.className.match(/card(\d+)/)[1]);
    const clozeWords = Array.from(
      document
        .getElementById("clozeText")
        .innerText.matchAll(new RegExp(`\{\{c${cardIdx}::([^{]+)}}`, "g")),
      (match) => match[1]
    );
    [...document.getElementsByClassName("cloze")].forEach((cloze, i) => {
      cloze.innerHTML = `${clozeWords[i].replace(/[^\s]/g, "_")}`;
    });
  });
</script>

If you also want to use actual hints sometimes, ask me and I’ll update the script to support that.

2 Likes

Why are you using the cloze notetype, actually? Looking at your screenshot, you can achieve the same result with the “Basic” notetype and save some keystrokes :slight_smile:

1 Like

Thanks for replying; I’ll try this later … :wink:

Why are you using the cloze notetype

Sometimes there are looooong sentences, and I just want to learn a phrase of 3 (4, 5, etc.) words; it’s just faster using the cloze note type, especially if I want to have these inline hints

P.S.

Seems like my card templates are filling up with stuff I don’t really understand … :wink:

I think I will simply keep the underscore key pressed until the line has the approximate length of the phrase; that should be good enough … again, thanks very much for your answer, I’m sure I’m going to use this stuff at some point in the future :smiley:

Well, that’s just the part of the code you actually see, Anki as a whole is about 100000 lines of these :wink:
The main point is that it’s not that much of an issue to pile up code you don’t understand (in this particular context, I mean), however if your concern is the ability to maintain your card templates (in the future) and that piling up too much stuff you don’t understand might undermine you, it’s a perfectly valid point, and even people that understand it may suffer the same issue. It’s why there is an add-on, called asset manager, that lets you handle all that stuff in an other place than your card template, and then it’s going to insert a single tag (that you can simply ignore) which is self-contained (in the sense that it will contain everything needed). It may make it worth for you to add it, or it may be still too much of a burden.

1 Like

piling up too much stuff you don’t understand […] it’s a perfectly valid point, …

… especially for relatively minor issues, that, like in the above case, can easily be achieved otherwise :wink:

As I said, it was not so much meant as a question specifically for Anki, but more general, something that one might also use, for instance, in a text editor.

Asset Manager

Thanks very much for that one; for a noob like myself it might be an interesting challenge to try and incorporate the above Regex-adventure provided by the Little Pirate into that add-on …

Anyway, it’s time to actually learn some language, and not so much worry about the tool (i.e. Anki), something that I have been, and continue to be, guilty of … :smiley:

Aren’t we all…