Mr. Trump also implied that he planned to pardon Joe Arpaio, the former sheriff of Maricopa County, Ariz., who became a national symbol of the crackdown on undocumented immigrants with round-’em-up searches that landed him in legal trouble. “Was Sheriff Joe convicted for doing his job?” Mr. Trump asked to wild whoops and cheers.
“I won’t do it tonight because I don’t want to cause any controversy,” Mr. Trump said. “I’ll make a prediction: I think he’s going to be just fine.”
In an angry, unbridled and unscripted performance that rivaled the most sulfurous rallies of his presidential campaign, Mr. Trump sought to deflect the anger toward him against the news media, suggesting that they, not he, were responsible for deepening divisions in the country.
“It’s time to expose the crooked media deceptions,” Mr. Trump said. He added, “They’re very dishonest people.”
“The only people giving a platform to these hate groups is the media itself and the fake news,” he said.
Mr. Trump also derided the media for focusing on his tweets, which are his preferred form of communication.
“I don’t do Twitter storms,” said the president, who often posts a few tweets in a row on a given subject, with exclamation points.
It was the latest shift in what has become a nearly daily change of roles for this president: from the statesmanlike commander in chief who sought harmony on Monday evening by citing the example of America’s soldiers to the political warrior who, just a day later, preached unapologetic division to his supporters here, eliciting louder cheers with every epithet.
Returning repeatedly to Charlottesville, he said the news media failed to focus on anarchists, who he said turned out in their “helmets and the black masks — Antifa,” Mr. Trump said, spitting out the nickname for the anti-fascist groups.
Mr. Trump accused the news media of “trying to take away our history and our heritage,” an apparent reference to the debate over removing statues to heroes of the Confederacy, which prompted the rally by neo-Nazis and white supremacists in Charlottesville.
The president singled out a familiar list of malefactors — including the “failing New York Times,” which he said erroneously had apologized for its coverage of the 2016 election; CNN; and The Washington Post, which he described as a lobbying arm for Amazon, the company controlled by the newspaper’s owner, Jeff Bezos.
Pointing repeatedly to the cameras in the middle of a cavernous convention center, Mr. Trump whipped the crowd into fevered chants of “CNN Sucks.” Members of the audience shouted epithets at reporters, some demanding that the news media stop tormenting the president with questions about his ties to Russia.
The people in Arizona on Mr. Trump’s enemies list include both of the state’s Republican senators: Jeff Flake, a longtime nemesis whom Mr. Trump has described as “toxic,” not to mention a “flake”; and John McCain, who cast the decisive Republican vote in the Senate to dash Mr. Trump’s effort to repeal President Barack Obama’s Affordable Care Act.
His voice thick with sarcasm, Mr. Trump said he had been instructed not to mention either of the senators by name. Of Mr. Flake, he said, “Nobody knows who the hell he is.” Of Mr. McCain, he repeated over and over, “One vote,” which cost Republicans health care.
Mr. Trump recited a familiar litany of complaints about lawless immigrants and naïve trade deals. But aside from a reference to renegotiating the North American Free Trade Agreement with Canada and Mexico — he said he expected he would have to terminate the accord first — the speech was light on policy specifics.
At another point, he heralded the arrival of clean coal plants, adding, “They are taking out coal; they are going to clean it” — which is not how clean coal plants function.
Mr. Trump also said little about foreign policy, offering only a bare summary of the Afghanistan policy he unveiled on Monday night, and suggesting that North Korea’s dictator, Kim Jong-un, had retreated in the face of Mr. Trump’s threats of military action against him.
The president made no mention of the accident involving the Navy destroyer John S. McCain, named for the grandfather and father of Senator McCain, which left several sailors presumed dead.
Hours earlier, Sarah Huckabee Sanders, the White House press secretary, had said that Mr. Trump would not issue a pardon for Mr. Arpaio on Tuesday. Mr. Arpaio was found guilty of criminal contempt of court after he flouted an order to stop detaining people his office suspected of being undocumented immigrants.
He said in an interview on Tuesday night that he did not know Mr. Trump was going to mention his name at the rally and reiterated that he had not talked to the president since last fall. But Mr. Arpaio said he “wasn’t really surprised” to hear he would likely be pardoned.
“I just know him,” Mr. Arpaio said of the president. “And even though everybody said he’s not going to talk about it — deep in my heart, I knew he was going to say something. I had no hints, but that’s who he is.”
Mr. Trump’s teased pardon of Mr. Arpaio energized the crowd at the convention center, where the president had been expected to stick to a theme of national unity. Even so, the forum drew scores of protesters and fanned fears of arousing more of the ugly nativist sentiments that exploded more than a week ago in Charlottesville.
Outside the convention center, the scene was a tense caldron, with hundreds of supporters screaming at one another, chanting slogans and hoisting placards that said “Fire Trump” and “Fake President.” Some voiced fears about the potential for the repeat of the violence that broke out in Charlottesville, while others griped about the 108-degree heat in Phoenix.
At one point, as Mr. Trump defended his remarks about the unrest in Charlottesville, protesters interrupted.
“How did he get in here?” Mr. Trump said. “He’s supposed to be with the few people outside.”
Earlier Tuesday, Mr. Trump traveled to a sun-scorched border post in Yuma, Ariz., to highlight his determination to crack down on illegal border crossings from Mexico. In Phoenix, the president threatened to shut down the federal government if his proposal to build a border wall is not funded.
Arizona was the site of one of Mr. Trump’s most raucous rallies during the presidential campaign, and if anything, the atmosphere was even more charged on this visit, his first as president. The Democratic mayor of Phoenix, Greg Stanton, pleaded with Mr. Trump to put off his trip, saying it would only aggravate racial tensions, coming so soon after clashes between white supremacists and counterprotesters in Virginia.
An earlier version of this article misstated whom the Navy destroyer John S. McCain was named after. It was John S. McCain Sr. and John S. McCain Jr., not Senator John McCain. (The ship was named for Senator McCain’s grandfather and father, both Navy admirals.)