This is something that I have been working on for quite a while, and it is still difficult sometimes. It is often difficult to break negatively-ingrained habits.
One way that I've found that helps is to consciously replace a negative thought or emotion with a good one right in the moment.
This has been a good practice for me. Sometimes I slip, but I quickly recognize it, which is part of winning the battle as well--recognizing it.
Too many times, we do things mindlessly, so its sometimes easy to fall back into a bad habit without even realizing it.