Definitely would echo @Life_and_Death's suggestion in looking at DBT, specifically at techniques for managing distress (crisis survival strategies).
Dealing with terrible thoughts that make you want to self-harm, what can you do instead of self-harming.
That site "dbt.tools" looks pretty thorough and includes worksheets to practice with. What I would suggest is reading through its section on Distress Tolerance and taking note of any techniques that pique your interest. And then hit up Google or Reddit and search for resources on those specific techniques, looking for sources that expound even further on them and for stories or insights from people who have found them helpful.
In doing this (or any therapeutic approach), you're reconditioning your brain to think and feel differently than what it's used to. This takes a lot of time, energy, effort, and consistent practice. You'll probably feel like a failure at times or as if you're an idiot or as if it's a fruitless endeavour, but this is part of the process that everyone goes through and you persevere by being patient with yourself, keeping an open mind, and practicing whenever you can.
Part of this is even remembering to try coping techniques at all in the moment. Sometimes when those terrible thoughts hit, they can be so overwhelming that you completely forget about this. So if this is a barrier for you (it certainly is, for me!), then you could set little reminders for yourself -- maybe notes on your phone, or post-it notes, or readily apparent links to online resources that could help prompt you in the moment.