To save you the pain of my journey (although perhaps the pain is a necessary prerequisite?) I can tell you that what has worked for me is engaging in Mindfulness and having sessions with an expert in Transactional Analysis. Transactional Analysis teaches you to realize you are not just one person all of the time but instead have multiple "Ego States" that you will move through depending on various circumstances. TA has deep scientific grounding and I have found it the only way to explain myself to myself clearly. Understanding TA (with the help of a therapist) can give you some explainations as to why you think, feel and do the things you do.
There is a LOT more to all this but I encourage you to read a couple of books and perhaps reach out to a therapist that specializes in TA for a couple of introductory sessions and see if it works for you.
- Mindfulness: https://www.amazon.co.uk/Mindfulness-practical-guide-finding...
- TA Today: https://www.amazon.co.uk/Today-New-Introduction-Transactiona...
http://www.amazon.co.uk/Mindfulness-practical-guide-finding-... (Mindfulness: A practical guide to finding peace in a frantic world, Prof Mark Williams and Dr Danny Penman)
It's a great book that leads you through the first 8 weeks of meditating, and contains meditation aid audio clips for various scenarios. The most useful I've found is the 3 minute "breathing space" meditation, particularly for that noisy bus - you need good noise excluding ear buds though.
If you don't like the speaking audio, and/or you've practiced enough not to need the prompts, you should check out Simply Noise:
http://simplynoise.com/ << they also have apps for phones.
I've found it equally helpful for rendering words unintelligible, which makes mindful awareness much easier.
1) don't waste cognitive energy on silly tasks (games, arguing in comment threads, etc.)
2) practice exercising willpower - it's a muscle, you can train it to be better. Start by forcing yourself to complete a routine every morning (the trick with habit forming is to not give up after you miss a day.) examples of habits to form below.
3) look into mindfullness meditation - this can help you identify distracting thoughts as they arrive and practice ignoring them.
Meditating is a good habit to form as practice, and it will also help you get better at habits. You could also exercise on a schedule (and record when you do, including how heavy you lifted/how fast you were running). Eventually, with a stronger willpower-muscle, you'll be able to choose the fruit salad over the cake, even when you've just spent your 7.5 hours a day coding.
I've not found pomodoro to work for me as an easily-distracted person, it's better when you're prioritising work tasks (e.g. 25 code vs 5 email) and even then, 25 mins is too short for good programming "flow".
This is a hard problem, everyone has trouble with it. Good luck!
 http://seriouspony.com/blog/2013/7/24/your-app-makes-me-fat (HN discussion: https://news.ycombinator.com/item?id=6124462 )
 http://www.amazon.co.uk/Mindfulness-practical-guide-finding-... (US edition: http://www.amazon.com/Mindfulness-practical-guide-finding-fr... )
Depending on where you are, you may need to wait to get an appointment (I had to wait 4 months to see a CBT worker on the NHS in the UK) so in the meantime, you may want to try this book:
Which is an 8 week course of ways to meditate to practise breaking the negative spirals of thoughts.
My partner and I are currently working though it, and while it can seem like it's talking down to you some of the time (especially if you're a geek like me) you should persevere, as the actual conclusions, practical advice and meditations are very helpful (and have been shown to work in clinical trials.)
Good luck, life gets better than this! :)
Get dozens of book recommendations delivered straight to your inbox every Thursday.