How do I keep my intention clean when I first begin a deed, but halfway through the thought of showing off creeps in uninvited?
What you describe happens to nearly everyone who tries to work for Allah alone. You begin a deed with a clean intention, and then partway through a thought arrives that wants to be seen. Guilt follows at once, as though the whole deed were now spoiled. It is not, and knowing why will settle you.
Begin with the foundation the whole matter rests on:
“Actions are but by intentions, and every person will have only what he intended.”
The Prophet (peace be upon him), Sahih al-Bukhari (1) · meaning
The deed is judged by the intention you set at its root. When you began purely for Allah, that beginning is written for you. A stray thought that wanders in later, unwelcome and unwilled, does not uproot what your intention has already planted.
The second hadith settles the rest:
“Allah has forgiven for my ummah the whispers of their hearts, so long as they do not act upon them or speak of them.”
The Prophet (peace be upon him), Sahih al-Bukhari and Sahih Muslim (Bukhari 5269 / Muslim 127a) · meaning
So the thought of showing off, arriving on its own, is a whisper, and it is already forgiven. It begins to count only if you take hold of it, feed it, and let it steer the deed. Leave it alone and it passes through you like a breeze through an open window, leaving the room untouched.
Do not fight the whisper in a panic. Struggling with a thought only fixes your attention on it. The moment you notice it, turn your face back toward your Lord and let the thought go on its way unattended. That turning back is not weakness. It is sincerity being renewed, quietly, again and again, deed after deed.
One small step: Keep one short phrase ready, such as “O Allah, this is for You alone,” and whenever the wish to be seen drifts in mid-deed, whisper it once in your heart and simply continue, without stopping to wrestle the thought.
