Most habits are loops that we automatically repeat: a cue or trigger in our environment sets off our routine and the reward we get from the habit. Once we recognise this loop, we can develop a plan to change it. Here's how.
Psychology and self-improvement blog The Emotion Machine notes that making a plan to act in a certain way at a certain time and place makes it more likely that we'll be able to replace bad habits with new ones. These implementation intentions, in psychology terms, intercept the cue and the old routine and redirect to a new routine instead.
The basic idea is to form an if-then plan to help instill new habits. Write down and repeat your plan, such as, "If situation X arises, I will perform response Y." X refers to the cue from your environment. Y refers to the new routine you want to replace with the old routine.
The key is to have that specific plan and know your triggers.
Some days you might miss your cue or you might really struggle and resort to your old habits, but that's ok. It'll take practice until the new habit loop is established. (And if this doesn't work, don't give up. There are lots of other things you can try to break bad habits, ultimately rewiring your brain for the better!)
Identify Your Habit Loops [The Emotion Machine]