Why do we say that the air is blue when someone has been swearing a lot?
The association of "blue air" with cursing has been around since at least the 1880s. Farmer & Henley, Slang and Its Analogues (1890) offers this brief entry:
To MAKE THE AIR BLUE, phr[ase], (popular). To curse; to swear; to use profane language.
Farmer & Henley then sends the reader off to look at its entry for sense 2 of the adjective blue, which is as follows:
Indecent; 'smutty'; obscene. this may be derived from the blue dress of harlots ... although Hotten suggests it as coming from the French Bibliotheque Bleu, a series of books of very questionable character.
But a somewhat different association may also have been at work in the original association of cursing with "blue air"—namely, the blueness of smoke-filled air, of the sort one might see on a battlefield or in a region of eternal fire and brimstone. To such effect is this excerpt from "A Rose of Glenbogie," written in the mid-1880s(?) by Bret Harte:
"The puir man [a minister] consented—although he dinna ken why and wherefore—and preached a gran' sermon! Ay, man! it was crammed wi' denunciation and an emptyin' o' the vials o' wrath! The congregation sat dumb as huddled sheep—when they were no' starin' and gowpin' at the meenester's wife settin' bolt upright in her place. And then when the air was blue wi' sulphur frae tae pit, the meenester's wife uprises!"
Something similar is going on in G. H. Emerson, Life of Alonzo Ames Miner (1896):
Any attempt to portray Dr. Miner as a Melanchthon would be preposterous. It would deceive no one, and would revolt his friends as a grotesque disfigurement. No, in every drop of his blood he was a Luther; and in Luther's day with Luther's thought he would have gone o the Diet of Worms, and had he been confronted "by as many devils as there were tiles upon the house-tops," even the devils would have trembled, even if they had not believed. When aroused, not doubting that he was battling with a foe that meant only evil, he knew no honeyed words, no arts of indirection, no compromising accents. Literally he flamed, the "air was blue," and every syllable was a missile that struck home.
Both "the meenester" in Hart's story and Dr. Miner (a Universalist minister) in real life are unlikely to have stood in their pulpits and filled the air with four-letter words; so it seems probable that "turning the air blue" had, as one possible meaning, loosing a cannonade or fusillade of excoriating denunciation such that the meeting place seems filled with blue smoke afterward.
The first instance that a Google Books search finds of "blue air" being used in connection with cursing involves a snatch of collegiate doggerel titled "Geological" from The [Williams College] Argo, volume 15 (February 16, 1884), reprinted from an earlier appearance in The [Yale] Record:
A stratum of solid, slippery ice;/A stratum of slush so soft and nice;/A stratum of water; over that/A stratum of man in a new silk hat;/Above, the startled air is blue/With oath on oath a stratum or two.
The most common source (other than cursing) of "blue air" from a colorless predecessor in 19th-century references is pipe smoke, but other possibilities are also mentioned.
Anna Cumming Miller, The Myrtle Wreath, or, Stray Leaves Recalled (1854), nominates cold:
There are not so many storms, but everybody you meet will say, "it is bitter cold"—the very air is blue—you can see it and feel it, though it moves not—it is heavy and still, and presses upon you like a weight.
H. Prescott Beach, "Shore-Bird Shooting in New England," in Outing: Sport, Adventure, Travel, Fiction, Volume 20 (September 1892) cites gun smoke:
In the midst of this we opened fire, and while the frightened denizens wildly circled in uncertain flight around us, we loaded and reloaded many times. The air was blue with smoke, and quivered with the shrill cries of the willets, the scared whistle of the plover, the squeaking call of sandpipers, and, over all, the thunder of the guns.
Frank S. Pinckney, The Tarpon, or "Silver King" (1889) suggests raucous noise without profanity:
I let him [the fish] run 100 ft, when I strike, and out comes the fourth member of this royal family, amid the whoops and yells of all the interested spectators. Listen to the shouts. "Hurrah for Olds! Bully for Kinney! Go it, S------! Oh, that leap! Look at him fly! Give him the tip! Great guns! I wouldn't have missed this for any money." The remaining boats begin to scatter to give us room, while the air is blue with yells and war whoops. The splash after splash which alone can be heard a mile away, as each fish comes out in his turn—words cannot describe such a scene. It must be seen and heard to be understood.
Perhaps an offshoot of this sense is the sense "rife," as in this quotation from Alice Elinor Bartlett, A New Aristocracy (1891):
"I am afraid I'm a heathen," he said dubiously, " if in this day and age, when the air is blue with reforms, I object to seeing the girl I love wearing her life away for a mere chimera."
H. K. Stroud, "System for the Sales Manager," in Ad Sense: Devoted to the Interests of Buyers of Advertising (September 1905) puts in a vote for suppressed heat:
He is very apt to receive an indignant phone call, calculated to raise all his ire, but his answer must be courteous and pleasant, while his inner feelings are hot enough to turn the air blue around him.
And "The Show Life in America," in The Showman: An Illustrated Journal for Showmen and All Entertainers (October 4, 1901) implies prurience or lewdness:
Some shows are very coarse, such as the "beauty shows," and a weird dance known as the "hootche kootche," which is guaranteed to turn the air blue for a mile around, where they are exhibited.
Yet other early references to turning the air blue have a jokey element, as in this one from Florence Howe Hall, Social Customs (1887):
It is interesting to note that according to Buddhist tradition the first lie was told by a king, and was therefore no doubt a white, or society lie. The citizens who heard it were even more innocent than George Washington. He, at least, knew what a lie was, even if he didn't know how to tell one; but these poor people were utterly ignorant on the subject, and asked whether a lie was white, black, or blue! It is to be feared that the blue lie has disappeared from the face of the earth, unless it survives in that kind of swearing which is said to turn the air blue.
And this one by K.F.R., from The [Cornell University] Cornellian (1891), which brings together "blue from smoke" and "blue from swearing":
Musing, I sat smoking a pipe,/In reverie deep till the air was blue;—/My father came, alas! too true,/I smoked no more. The air was blue.
To sum up, we have instances between 1854 and 1905 in which blue air seems to be ascribed to cold, to suppressed heat, to pipe smoke, to gun smoke, to a general rifeness, to prurience, to raucous (but not profane) noise, to angry (but not profane) denunciation, and to cursing.
Update (July 7, 2020): Early newspaper instances of "turn the air blue"
I checked the Elephind newspaper database to see whether very early instances of "turn the air blue" consistently appeared in the context of cursing. The evidence seems to support this connection.
From "A Word to Voters" in the Wheeling [West Virginia] Daily Register (September 1, 1869):
Is your name on the registration lists? If not, have it placed there at once. Your township registrar declares that he is willing to enroll you when you satisfy him you are qualified. Call upon him at once. Don't neglect the matter and turn the air blue with curses in October.
From "Concerning Army Chaplains" in the New-York Tribune (July 13, 1878):
The words used [by Colonel Steedman] in my defence would not look well in print. One of the mildest epithets that the Colonel applied to the agent was "thief." The man ventured to defend himself. Then came the words: "You stole my chaplain's trunk, and I order you at once to deliver it over to him, and if I hear another impudent word from you I will put you under arrest." He said he would relinquish it, but "under protest." The stream of bad words that fell from the lips of the good Colonel seemed to turn the air blue, and actually made me feat sorry that I had said anything about my lost treasure. It Is needless to say that I recovered the trunk and have it yet.
From an untitled item in the Columbus [Nebraska] Journal (September 14, 1881), reprinted from the Lincoln [Nebraska] Globe:
The amenities of Chicago daily papers will soon pass into proverb. The Joemedillian and Billstorean force whack each other over the head with their journalistic shillalies, and turn the air blue with their expressive epithets. Meanwhile the Inter Ocean, towering above both the com[b]atants, gives soothing advice only to be hooted at by both.
From "Kimball Defeats Sexton for $1,000," in the New York Clipper (May 6, 1882):
When they discovered that they were virtually an eighth of a mile away from the billiard-table, they rushed to exchange seats, which cost them from fifty cents to one dollar additional. This helped to fill the balcony, balcony-boxes and parquet; but it turned the air blue at the same time, and was damaging to all billiard-matches in the immediate future.
In each of these early instances, the sense of the usage is that actual or fancied cursing caused or would cause the air to turn blue.
The OED is uncertain of the origin of the term 'blue' with reference to pornography, swearing etc. see below.
The origin of sense A. 10 is uncertain. Quot. 1818 at sense A. 10a suggests a connection with the blue flame of burning brimstone (as characteristic of hell), but it is not entirely certain that this quot. shows continuity with the later use. Perhaps compare blue gown n. 1(a), although this appears to have been current only in a rather earlier period. A suggested connection with French bibliothèque bleue , denoting literature in a blue paper cover sold by itinerant sellers from the 17th to the 19th centuries and read by the lower social classes, seems unlikely, since such material appears in general to have been highly moral in tone.
As a supplement to Sven Yarg's answer, Sulfur is the element that makes black powder (a.k.a. gunpowder) burn blue. Sven Yarg made a good references to why the turn of phrase would reference smoke/haze/air because of battlefields and burning things. But he didn't address why the color was blue and not some other. It was probably the use of sulfur in the explosives of the time. Sulfur based gunpowder could have literally turned the air blue. From there, we could logically make the jump -> explosives = blue (literally) = "explosive" language or content = blue figuratively.
I found a 1872 use of the expression in the Appleton's Journal. I don't have the original version with me right now but the translation I did to Spanish. Will post the original as soon as possible. It was used in the sense of cursing until the air got blue. I like the Sulphur hypothesis because we use in Spanish "to sulphurate" to indicate getting angry.
This harks back to the days of official censorship in Britain. The censors were equipped with blue pencils, which they used to cross out sections of radio (later, TV) script or written articles which were considered too rude or sensitive for publication. It's usually little remembered outside places such as the BBC or the studies of historians, but the state censors had power.
Turning the air blue is a peculiar phrase, but could refer to the airwaves of radio, or (my personal preference) the idea of a little man in a grey suit, desperately scribbling at the air itself...