Q - How does one avoid habitual sin?
A - Thanks for the question. My answer might be somewhat simplistic, but doing what I suggest will be much more difficult.
Here is what I wrote about the subject previously:
Scriptural help for avoiding temptation.
Also, the following might help for the temptation of pornography and here is more on the Pornography issue.
Stopping habitual sin is a difficult thing to do and accountability helps immensely. But, a proper kind of accountability is needed. You need someone who understands the seriousness of sin but is kind and patient in helping you. A good priest, spiritual director, counselor, friend, spouse, etc. can help - depending on what the issue is.
You should not get down on yourself for sinning. This is exactly where the devil wants you to be. Sin is universal to the human condition, until we reach heaven. So, beating yourself up or quitting on trying should not be options. Open yourself up to God's grace and allow Him to give you strength where you are weak. The Sacraments are one of the primary weapons you should use as well as a deep prayer life. The closer you are to Christ, the harder it is to sin.
I will keep you in my prayers. Please know that St. Mary's staff is here to help.