Replace bad habits with better ones by identifying what motivates your unhealthy behaviour. Find better habits that fulfil those needs in healthier ways.
For example, your daily trip to the office cafeteria for a cookie might be less about sugar cravings than a desire to socialize. Identify the cue for the bad habit, and create a new routine connected with that cue.