A few weeks ago I woke up with a horrifying nightmare. My shoulder was in nearly-severe pain and I was left with a vague impression that my fingers had just been bitten off. It does happen sometimes that I have nightmares, although I have to say over the last few years the intensity has died down. And alhamdulillah, I'm learning more and more about what to do when I have one.
In fact, I just learned a lot more, thanks to a handy little book, Fortress of the Muslim (Hisnul-Muslim), a present from Qabeelat Nurayn (may Allah reward them and increase them in knowledge), from when I visited them for the AlMaghrib seminar Divine Link: Fiqh of Salah. An appropriate present really, this book, which is basically a collection of du'a for every occasion. And occasion number 31 is "What to do if you have a bad dream or a nightmare."
The book cites a hadith in Sahih Muslim which it references as 4/1772. However, when I looked up a hadith so referenced I found one about reading Surah Ikhlas. Another search however turned up a collection of ahadith to support the statement however, so I conclude that they are what is meant to be cited. The following sums them all up best:
Abu Qatada reported Allah's Messenger (may peace be upon him) as saying: The good vision are from Allah and the evil dreams are from the satan. If one sees a dream which one does not like, one should spit on one's left side and seek the refuge of Allah from the satan; it will not do one any harm, and one should not disclose it to anyone and if one sees a good vision one should feel pleased but should not disclose it to anyone but whom one loves. (link)
The first thing to do, if you wake up from a nightmare, is to spit to your left side, three times. This is like what you do in prayer if you feel plagued by excessive waswasa (whispering of Shaytan.) And the next step mentioned is to seek refuge in Allah from Shaytan, and from the evil that you have seen. Some ahadith say seek refuge with Allah from Shaytan, and other say from the evil of what was seen in the dream--so the book of du'a concludes to seek refuge from both.
The part about not telling the dream to anyone is kind of important, I think, and a wise instruction from our beloved messenger (saws). I've found, at least, that the more I tell people about any particular dream, the more strongly I remember it. Similarly, the more I dwell on it, the more I remember it. And why would anyone want to remember such awful images? Clearly, dismissing them and remembering Allah is the way to go--and then you can go back to sleep awaiting better dreams.
And then the next tip is also very interesting--actually moving, turning over onto your other side. There is another narration of the above hadith indicating that the person should change the side on which he had been lying before--i.e., turn over. Psychologically, if you just turn over and go back to sleep, you're likely to forget the dream entirely. I'm not sure if it's changing the position that helps, relieving some pressure in the body, altering the position of your brain, and really Allah knows better. In my own experience, the rolling over and going back to sleep (after the spitting and seeking refuge with Allah) helps me to forget the bad dreams.
And there's just one more tip: pray. Get out of bed, go make wudhu, and "savor the sweetness of salah."