This is a major principle of Judaism: A person must not divert his attention from all of the matters that are duties of the body and duties of the heart. And if it should happen that the person’s mind was diverted, then, when he recalls the Rock from which he was hewn, he shall reconnect to it and not let it go again. He should then remember what his duty is [in the world] and hurry to fulfill it. The most helpful method to prevent distraction is writing, because seeing leads to remembering.
If a person received some favor from his fellow, it is proper that he not forget it or divert his mind from it. Conversely, if one’s fellow harmed him, he should forget it and push it out of his mind, in order to fulfill the Torah’s injunction, Do not bear a grudge. Or if stray or evil thoughts enter a person’s mind — especially during prayer and Torah study and Divine service — he should push the thoughts out of his mind and focus instead on words of Torah or on serving Hashem, may He be blessed.
It is especially important while wearing Tefillin to not divert one’s thoughts from them. Hashem’s purpose when He commanded the wearing of Tefillin was that we remember what is written in them, including the benefit that flows from it — to fear and love [Hashem] etc. The main fulfillment of the mitzvah of Tefillin is dependant on concentration.
While a person is engaged in serving Hashem, which requires that his mind be settled, the person should remove all worrisome thoughts from his mind. And by thinking deeply on thoughts of purity one can find sufficient sentiments to distract his mind from that which needs to be distracted. This method works because a human’s mind cannot simultaneously think about two thoughts at once. When a thought that one should be banishing enters one’s mind, he should exchange it for a different, positive thought.

