Let's do some “enciph5r47g” This is the inverse of Let&#39;s do some &quot;deciph4r4ng&quot;

In this challenge, your task is to encipher a string. Luckily, the algorithm is pretty simple: reading from left to right, each typical writing character (ASCII range 32-126) must be replaced by a number N (0-9) to indicate that it is the same as the character N+1 positions before it. The exception is when the character does not appear within the previous 10 positions in the original string. In that case, you should simply print the character again.
Effectively, you should be able to reverse the operation from the original challenge.
Example
The input string &quot;Programming&quot; would be encoded this way:

Hence, the expected output is &quot;Prog2am0in6&quot;.
Clarifications and rules

The input string will contain ASCII characters in the range 32 - 126 exclusively. You can assume that it will never be empty.
The original string is guaranteed not to contain any digit.
Once a character has been encoded, it may in turn be referenced by a subsequent digit. For instance, &quot;alpaca&quot; should be encoded as &quot;alp2c1&quot;.
References will never wrap around the string: only previous characters can be referenced.
You can write either a full program or a function, which either prints or outputs the result.
This is code golf, so the shortest answer in bytes wins.
Standard loopholes are forbidden.

Test cases
Input : abcd
Output: abcd

Input : aaaa
Output: a000

Input : banana
Output: ban111

Input : Hello World!
Output: Hel0o W2r5d!

Input : this is a test
Output: this 222a19e52

Input : golfing is good for you
Output: golfin5 3s24o0d4f3r3y3u

Input : Programming Puzzles &amp; Code Golf
Output: Prog2am0in6 Puz0les7&amp;1Cod74G4lf

Input : Replicants are like any other machine. They're either a benefit or a hazard.
Output: Replicants 4re3lik448ny3oth8r5mac6in8.8T64y'r371it9376a1b5n1fit7or2a1h2z17d.
Retina, 24 23 bytes

(.)(?&lt;=\1(.{0,9}).)
$.2


Try it online!

A fairly simple regex substitution. We match each character and try to find a copy of it 0-9 characters before it. If we find it, we replace the character with the number of characters we had to match to get to the copy.

The results don't quite match the test cases, because this one uses the largest possible digit instead of the smallest possible one.