Recently, a driver buzzed by me, leaving just a couple of inches between my handlebars and his car. It was very scary.
In general, how can I prevent cars from passing me too closely?
Please cite sources.
Use proper positioning. This is most important. Unless the outermost lane is as wide as two SUVs, ride in its center. When it's safe, reasonable and necessary to let drivers pass, kindly move over; but always leave at least 18 inches (0.5 m) between you and the curb. The driver behind you may have to wait a minute or two, but they'll survive. If they've waited a few minutes, be nice and pull over. (Adapted mainly from Wikipedia citing Cyclecraft.) Paradoxically, correct positioning will stop most drivers from buzzing you. Correct positioning can be scary, but does get easier.
On rare occasions, drivers may still buzz you. To prevent this:
Use a safety flag. A spring-mounted one: e.g. a Flash Flag. (Source.) It must be spring-mounted, so that it can bend easily if hit. (If you can't afford a flag: Maybe you could use a dollar-store pool noodle plus a washable marker pen. See here.)
Or use a doll. Put a baby-sized doll in a trailer or rear-mounted carrier.
About attempted murder: If a driver ever intentionally endangers your life, note down the license plate number, location, time, and date. If you value your safety, don't touch the driver's car. Contact a cop: preferably immediately; but better later than never.
This post is marked as community wiki. Please add to it.
Cars overtaking too closely is often down to the width of the road (ref):
For a cyclist to be safely overtaken, the width required depends upon the width of the overtaking vehicle but in general a lane width of 4m is needed. For widths of between 3 and 4m the cyclist will be 'squeezed'. Road widths less than 3m ensure the overtaking vehicle must wait behind the cyclist.
There's a more detailed discussion of road width and overtaking on humantransport.org. The diagram below shows distances in feet:
If you can choose a route with wide roads then you're unlikely to be overtaken in a dangerous manner. Roads with (at least) 2 lanes each way usually work well if the lanes are narrow enough that cars have to change lane to overtake you.
On narrower roads you can discourage dangerous overtaking by positioning yourself correctly (see this question on Claiming The Lane). If you position yourself at least 0.5m from the edge of the road you will have a little bit of room to manoeuvre if you are overtaken dangerously.
Sources: Linked above, as you can see. The summary from the British study, from the source.
You won't be able to stop this completely. There will be always car drivers how try to pass you, no matter what. However, there are things you can do about this.
Most car drivers usually let enough space between you and the car, if possible. So they will likely pass you closer when:
Now, you have a few strategies to encounter this situations:
Personally, I prefer option 1 in the cities, because cars will have enough time to see you and slow down. You still have enough space to ride to the side in risky situations to increase the space between you and the car.
2 is more risky in cities because it kind of invites cars to pass you close. I usually to this on broad highways, because there most cars newer slow down to wait to pass you (at least in my experience).
I do option 3 on narrow roads with oncoming traffic. These are usually side roads with few traffic anyway. I don't like to stop, but in the end I will get hurt if a car hits me.
When I ride, I usually wear a little rearview mirror attachment on my glasses that allow me to see traffic behind me. If I see a vehicle approaching and I don't get the sense that they see me, I'll actually crane my head around so it's very obvious that I've spotted them. Most drivers will notice you if you do that.
(obviously not to take away from the very thorough and technical answer by @unforgettableld - just a trick a friend taught me).