Pomodoro. It trains you to stay on task for 25 minutes at a time, and evaluate how you did during the last cycle, both from a distraction standpoint and a chasing-your-tail standpoint.
Over time you build up to 4.5-6 hours of solid, head-down coding a day.
The pomodoro technique helped me enormously. I don't follow it strictly, I just set the timer and focused on one task until timer is done. I never worked the duration up, I still just do 25 minutes or less. I even put websurfing in a 25 min bucket, then stop.
I ended up learning how infrequent real emergencies pop up. Which in turn, got me to stop looking at my phone at red lights.