Forethought (visualization) and training play a big part as well.
You reminded me of something that happened to me while driving a few years ago. I was on the highway in the far left lane going about 70mph. I was driving an Audi A4, which had much better (sensitive) handling than the pickup truck I was used to driving. There was a breakdown lane to the left of me (unusual for most highways) and I thought to myself, "if I suddenly needed to swerve out of the way for some reason, I could flick my wrist and get into the left breakdown lane to avoid whatever was happening in front of me".
Not more than a few seconds after having that thought, I saw the car in front of the car in front of me jump up onto the car in front of it. The car in front of me slammed on his brakes. I flicked my wrist to the left without thinking. Only when I passed (in the left breakdown lane) all the cars piled on top of each other did I realize just how closely I dodged a serious accident. If I had not visualized what I would do in such a scenario a few seconds earlier, I'm not sure if I would've done the same thing.
Plenty of close calls have taught me the value of always knowing my exits and always leaving plenty of space between me and the car in front of me (driving instructors often teach 1 car length for every 10 mph: 70 mph = 7 car lengths).
Unfortunately a lot of these rules only work if other drivers on the road are also following them (around here, in the Boston area, you'll be lucky to have 2 car lengths of space in front of you no matter your speed), so I've learned that simply being aware at all times (no phone, no distracting conversations, and holy hell no text messaging or emails) is the best thing anyone can do for improving their safety while behind the wheel.
This is what they used to call "defensive driving".
Supposedly, people who think through how they would handle emergencies or other bad situations handle them much better than people who don't like to think about it. This is true even when it's a situation they hadn't actually planned through yet, because they have the mental habit of solving problems rationally rather than just pretending they won't happen.
I've saved my car from rear ending the one in front using exactly that manoeuvre - escaping onto the gravel between the fast lane and the central reservation.
I now leave a lot more space between me and the car in front, and I've trained myself to remember that traffic might stop dead at any time.
My guess is that the first car was doing something odd, some tiny instability that caused the second car to change, say momentarily lifting their foot from the accelerator, etc. Your higher brain functions were doing an executive analysis of options, but your lizard brain was already alert to danger and had sounded the early alarm. Have you ever noticed how you are extremely sensitive to tiny sideways movements of cars as you pass them?
You reminded me of something that happened to me while driving a few years ago. I was on the highway in the far left lane going about 70mph. I was driving an Audi A4, which had much better (sensitive) handling than the pickup truck I was used to driving. There was a breakdown lane to the left of me (unusual for most highways) and I thought to myself, "if I suddenly needed to swerve out of the way for some reason, I could flick my wrist and get into the left breakdown lane to avoid whatever was happening in front of me".
Not more than a few seconds after having that thought, I saw the car in front of the car in front of me jump up onto the car in front of it. The car in front of me slammed on his brakes. I flicked my wrist to the left without thinking. Only when I passed (in the left breakdown lane) all the cars piled on top of each other did I realize just how closely I dodged a serious accident. If I had not visualized what I would do in such a scenario a few seconds earlier, I'm not sure if I would've done the same thing.