At times during the ultra long rides, when I'm starting to fade, I've picked myself back up by just counting.
Or chanting. Chanting things.
I've recited the first few portions of the Tibetan Book of the Dead a few times, often starting over with each person that I pass (afterall, the book is meant to be read to
someone and addressed
to someone, might as well start with the person I just passed or am about to pass)
I've recited poetry - Omar Khayyam, Rilke, Rumi, Hafez and so on.
I've started to sing some favourite songs. ... and, sadly, in some cases I've started to sing songs that I actually would not have wanted to sing... but they stuck in my head and I kept with them until someone, or something, would switch me out of it. I would forget that I'm pedaling.
Often, especially on climbs, I would pick a distant object and promise myself to do something when I get to that object, like take a sip of fluid or a swig of gel from the flask, but keep pedaling through it, pick another target, and keep going. This only works during the day however. At night, sometimes the only objects I've seen are the pairs of eyes from coyotes, foxes, deer and sometimes bears that reflect my bike's lights... and they're probably thinking "WTF is this dude doing out here at this time?!"... and I think about what they're thinking, and I keep going, I keep going, I keep going.
But, really: chanting, counting out (anything) and reciting verses has really helped.
Whoever says "HTFU" has never done a really serious endurance ride. It's more than just "HTFU" - the mental battle is far greater than the physical can ever get to.