When possible I quarantine it into a separate function and stick it in the bottom of the script where I don't have to look at it. If that's too much work or it's too ingrained in everything, I just comment it out. I hate having to repeat my work, especially if I got it to work precisely the way I wanted the first time. My code may be less clean at as a result honestly, but I never collaborate so it's never been an issue. Saving myself time and effort takes precedence over pretty much everything as an indie dev.
What I also did was working on a copy of a function, but that's annoying as
well.
I'm far from well-versed enough in programming to answer this but I think it sounds plausible if you write a standalone program that checks what you have highlighted. Windows obviously knows what you've saved inside ctrl+c and can handle text highlighting so it's clearly possible somehow

Appending the text to a different *.txt file or similar is child's play, I remember doing something similar in a beginner's course at school

...
Should be doable. I don't know why there isn't any such program to begin with,
which for example records all your copies into the clipboard with the program
being sort of a container saving everything you've copied to the clipboard but
with a history attached (could be disabled for certain programs like PS, gimp
etc.). Would be cool, for you could re-copy a text into the clipboard which
was in there many copies/selections ago so to speak.
Nope, don't have that issue because I use git (version control) for it.
"Wait, I need that function I deleted last week" - Open version control check the diff, copy paste it back or just rollback the whole file if needed.
"Oh I need to update my code to the latest engine update" - Just update because everything is in version control, make all the changes, commit/tag and all your old work is still saved if problems arise.
"You want to make a new feature, but don't want to mess up the rest of the code" - Branch it, and merge it back into your main if you're satisfied.
You might want to look into some version control to make this process easier for yourself. It saves so much time compared to commenting/uncommenting or saving it separately.
https://www.atlassian.com/git/tutorials
That's all fine, but I don't want to make a new brunch or similar each time.
It's different. If I know in advance that I'm going to change the code
drastically or want to make a new feature etc., sure I will make a copy
whatever. But what I want to say is that what I experience is sort of a
silent change to the code over time, that is to say, you find yourself having
changed the code more than you thought you would (experimentation
gone too far) and now want to revert back to a given "point", but the point
isn't a clear one, because if it is one could have made a checkpoint right
from the beginning, but there was no real beginning. So I select the given
section and comment it out (deleting soon thereafter if there are way too
many commented sections) up to the point where I think the beginning was, or
up to the point I see fit. Over time I've found out that these snippets of
experimental code are more valuable than I thought at the time of writing.
And now I want to save these snippets of code but in an easy fahsion. Best
would be so select the section/snippet and have it automatically saved
somewhere with a timestamp.
It sounds like what you want is what's usually called something like "shelving" in version control systems.
Visual Studio has this, but it I think it's tied to TFS (Team Foundation Server)
missile, you want to use
git stash. It gives you a way to store temporary changes separately from main version history. You can apply and unapply the changes quickly.
^and yeah I'm sure most other VCS have something similar.
Shelving sounds interesting. Stash, too.
Perhaps my problem is more related to graphics/math programming where you
change the formulas, algorithms etc. a lot between the lines and go a lil
further experimenting more with similar stuff within the same function to
get to a similar or improved result.
For example, the week I did all the pixelized volume rendering stuff I was in
a real flow and actually also programmed a pixelized volumetric global
illumination algorithm out of what I already had. It was just an experiment
seeing how far I can go with my pixelation stuff. I just wanted to know if it
works with global volume lighting. And well, it works! The emissive and light
reflecting objects were illuminating the volume in their surrounding. But
because it was only an experiment I didn't really wanted to do for real (was
something I wanted to do a bit later), like just seeing how far I can go with
my current stuff adding "two more things", I deleted the code without having
said anything about it nor have made any pictures. I just wanted to see if it
works or not, because I thought I will do it again later yet much better. But
know I think, well, the code wasn't too bad, actually. xD