Swap a bad habit for a good one
If you want to get rid of a bad habit, you cannot just expect to stop the habit without replacing it with something else. Nature hates a void. Rather than distracting yourself with a mindless activity, plan a good habit that you will do instead.
The new habit should give you the same benefits as the old habit, otherwise it won’t stick. After a while, the good habit will set in and it will be easier to forget the old one.
My friend Jenny started working from home and discovered that she had more snacks when she was alone. The benefit she got from snacking was that it made her feel less bored when she was alone. Realising this, Jenny started saving all the phone calls she needed to make for the times when she felt like snacking. Once she had made her phone calls, she no longer felt lonely and didn’t feel like snacking any longer.