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.