There are plenty of tools to help you build better habits, but in many ways it really comes down to willpower and understanding the mental process behind how behaviours turn into habits. Over at 99U, they call this the "habit loop", and the key to breaking bad habits is tricking yourself into swapping out the habit you want to pick up for the one you want to drop.
The habit loop itself is straightforward: you receive a cue of some kind, you address the cue through a behaviour, and you reap a reward for your behaviour, either mentally or physically. What you want to do is fix the behaviour, instead of trying to remove the cue or the reward, the way many of us do. Here's an example from the article:
If you want to get rid of a bad habit, you have to find out how to implement a healthier routine to yield the same reward. Let's say you like to go out with your coworkers at the end of a long day and have a few drinks. In this situation, there are actually two rewards: (1) the socializing that inevitably occurs, and (2) the relaxing effects of the alcohol on your nervous system.
Both of those rewards are valid and necessary. If you remove drinking from your life, but replace it with nothing else, you'll likely be unhappy. The trick is to keep the cue (e.g. tired after a long day) and the rewards (e.g. social time, relaxation) while changing the routine (e.g. drinking).
They go on to point out that the key to fixing the bad habit in this case isn't in giving up entirely on the socialisation and the relaxation -- if you do that, you'll just make yourself unhappy and alienate yourself from your coworkers, and therefore more likely to slide back into your old habits. They suggest instead trying to get one of the coworkers you go drinking with to do something else -- running, yoga, rock climbing, anything -- that gives you the same rewards and is triggered by the same cue. Whatever you do, the key to success is to fix the routine without breaking the other elements of the loop.