I applaud you for wanting to "stop" and "get rid of this habit" rather than just curbing it. I agree with all of the insights that you have reached regarding this. Some of the answers suggest saving the gossip for another time or place, which might be a starting point, but that doesn't really address the toxic nature of engaging in that habit.
I think the first step is to consider and internalize the idea that regardless of location -- workplace or not -- gossip is harmful to all parties involved, and any solution will require finding a way to stop it, or redirect to a healthier action.
Obviously, it's not helpful to the person being talked about. It also influences preconceptions for the person you are talking to, so even if the two have never met, you can imagine the one-sided bias that is already formed if they ever do meet in the future. In the case where the person you are gossiping to does know the subject, then it reinforces any negativity towards that person. No truly good feelings ever result from gossip.
Mostly it harms you and your reputation. If you become known as a gossip, people will be reluctant to confide in you during times that they need to talk to a close friend. It isolates you from having deeper closeness, and can causes other people to have stress for your moment of entertainment.
It sounds like you are already coming to that realization, or you wouldn't have asked the question. One technique is to get in the practice of mindfulness about what you are about to do or say. One popular approach is to ask yourself these questions before speaking, and letting each act as a "gate" for your words:
- Is it true? Is what I am about to say true, and from a reliable source?
- Is it kind? Even if it is true, does this build someone up or tear them down?
- Is it necessary? Why do I feel the need to say this?
I also think about what should I talk to my friend if we don't gossip about others.
This is a very poignant concern. Your refusal to participate in gossip is likely to irritate others who want someone to share in their gossip, but being clear and persistent is key. After a while, people who know you are going to remember that you don't like to gossip, and they will curb what or how they share things with you. It may feel like you are being excluded at first, but you will realize that your wishes are being respected.
You can invite your friend to join you in this effort to improve yourself. Be sure to follow the three questions as you have this conversation as well. If possible, invite them in a way that isn't hurtful towards them, in much the same way you might ask for assistance in sticking to a workout schedule or breaking any other bad habit. Perhaps you could ask them some version like, "[Trusted friend], I'm trying to be more aware of what I say about others. I have realized that I say things about people that I wouldn't say if they were present. It affects my everyday relationships with those around me, and it's unfair to them. Can you help me catch myself if I start talking about someone in a way that I wouldn't if they were present?"
This lets them see that you are genuinely interested in improving yourself, you value their input, and does not come across as you trying to change their behavior. As a result, it might cause them to reflect on their own behavior and inspire them to change with you. You never know, they may already want the same thing. If they are a true friend, they will try to help with that. If your only common bond is the gossip, then this is a friendship that is unhealthy without some sort of change. You may have to be blunt and say, "I'm sorry, but I don't want to talk about them while they are not here," and don't hang around if they continue anyway. This lets them know you are serious about it.
Your time would better be spent focusing on how you can assist or at get along with the inept coworker. Getting to know them better will help much more than gossiping about them ever will. Best wishes for you on this journey. You can do it, and it's worth it!