This is one I struggled with for a long time and it’s not easy to even figure out how to fight it, let alone to actually fight it. A lot of people are going to tell you to try and distract yourself whenever temptations or bad thoughts enter your head. In one sense they are absolutely correct, finding things (especially prayers) that can get you out of your bad headspace is vital, but alone it is not enough. If you just fight temptations as they arise you are fighting a losing battle.
So here are the two steps that really helped me:
First, this isn’t just a temptation, it is an ingrained bad habit. Like any bad habit you can’t just attack the habit itself, you have to find all the smaller triggers that lead to that habit. Look for the situations where you are most overwhelmed by urges and thoughts and try to avoid them where you can. When you find yourself under assault by unwanted thoughts (whether you fall to them or not) try to figure out how you got there. You need to find all your triggers and start retraining yourself.
Secondly, pay attention to what you are praying for and what you are not. I spent a lot of time just praying for help fighting temptation, but that’s like being in a cage with a hungry lion and asking that he not eat you for a few more minutes. Your goal is not to resist temptation, your goal is to become the kind of person who isn’t tempted. Make sure you pray for that.