On pipes..... I have the same problem. Replace the habit with something else. When the urge came up, I immediately try to do something else to stop thinking about it. The key is the awareness to know that you have the urge and then replace it with something else. Once its out of the mind the urge will go away as well. Keep yourself busy to replace the habit.
Works for me....almost anyway. Can't help myself though when I have the occasion alcohol. It goes hand in hand...