How do you give up an unhealthy, dysfunctional habit, whether it's drugs or compulsive thinking?
That is a very broad general question, dealing with a very personal, albeit common, problem. The approach to healing is very dependent on your personality.
My approach is to go to the root of the cause, which is always some need within yourself, which is not fulfilled. Being able to fulfill your deeper emotional needs is a tremendous step n the right direction to becoming a more wholesome person. (The “how to,” I will explain further down.)
Having said that, some people don’t have the capacity or interest to undertake that inner journey on their own, in which case I’d start identifying the external triggers. Think about ways to respond to those external triggers. For example. If a certain person’s remarks causes you to go into an negative thinking spiral, come up with an action plan to prevent it. It could be avoiding that person, having a rehearsed, empowering response back to that person, a notebook in your pocket with uplifting thoughts to grab after contact with that person, or a good friend to phone immediately that will help you prevent that negative spiral. Be creative.
If you simply want to change the negative habit with something positive, that can be easy. I know of people who replaced smoking with lemonade or water. I myself have replaced anger burst outs with laughter. I did make the conscious decision, went for a hypnotherapy session and released the pressure on myself to “get it right.”
For the deeper work, there are various methods to start working with the emotional needs you are not currently fulfilling in your own life. Inner child work is my absolute favorite. There are plenty free examples on Youtube to choose form, or if it is in your budget, receiving this as session work from a professional practitioner delivers remarkable results. Remembering (or sometimes making up) events from your childhood where a certain need was not met can be very powerful. For example, not receiving attention, being treated unfairly, being abandoned or rejected, not being heard, or being bullied, or put under unnecessary pressure. As adult you can offer the appropriate response, support and love to the innocent child you meet. This is also quite fun!
Another very powerful method to start observing and changing unhealthy reactive patterns is through mindfulness meditation. Vipassana Meditation offers worldwide courses. These however is not suitable for drug addiction or serious mental conditions, for which the first step is always finding professional assistance.