by Rabbi Avigdor Miller
You can practice training yourself to think unselfishly whenever you see a frum Jew in the distance. Simply say a prayer out loud wishing him hatzlachah (success) in all his endeavors. Nobody will know that you said it, but this totally unselfish act will have an effect on you. When acts like that are multiplied...
You can train yourself to look back and thank Hashem for His kindness to you
...their effect will gradually train you to do things without the intention of getting more out of it.
Another exercise that you can do is to train yourself to look back on your past history and see what Hashem did for you thirty years ago, twenty years ago, and so on, and thank Him for those benefits. For example, you can thank Hashem that you were married. You can thank Him that you were able to go to yeshivah when you were a boy. You might even thank Hashem that you once had a narrow escape from an accident. You can also make an extra effort to thank Hashem for benefits that you have recently experienced but that are completed.
Let’s say you have just married off your last child. So you thank Hashem for arranging good shidduchim for your children. Since you are not doing this in order to get more shidduchim, that’s an honest form of thanks. All the past benefits are very important for us even though they are over and done with, as it says, "Bless Hashem, O my soul, and forget not all His kindnesses that He bestowed upon me" (Tehillim 103:2).
All the past benefits are very important for us even though they are over and done with
Since these benefits are in the past your gratitude can be completely unselfish. You can train yourself to look back and thank Hashem for His kindness to you. When you are on the verge of the grave and just about to depart from This World, you are capable of wholeheartedly thanking Hashem. Since you don’t expect to live more than 120 years, if you are 119 you don’t expect Hashem’s goodness to continue. Nevertheless you look back as you trained yourself to do and you can honestly thank Hashem with a full heart for all His kindness. — Ohr Avigdor Shaar Avodas Elokim