KABUL, Afghanistan — When American Airlines Flight 77 struck the Pentagon on the morning of Sept. 11, 2001, Lt. Col. John W. Nicholson Jr. survived by chance. That morning, as dozens of his colleagues were killed, he was moving from one house to another and was not at his desk — which he said was 100 feet from the nose of the plane.

Nearly 17 years to the day, Nicholson, now a four-star general departing as the commander of the U.S. and NATO forces in Afghanistan, delivered an emotional farewell in Kabul on Sunday.

The general, who spent 31 months at the helm of a quagmire of a mission that has shaped his career over four tours of the country and has cast a shadow on a generation of U.S. military leaders, said he wanted to speak from the heart.

"It is time for this war in Afghanistan to end," Nicholson said.

The general called on the Taliban to "stop killing your fellow Afghans," but he also referred indirectly to regional players — particularly Pakistan, where the militants enjoy sanctuary — who have complicated the fight.

"Whose voices are important?" he asked. "The outsiders who are encouraging you to fight, or the voices of your own people who are encouraging you to peace?"

Naming the first and the last American soldier killed under his command, and praying for the hundreds in between, the general demonstrated little of the chest-thumping of previous commanders and put aside his sometimes-rosy assessments of the situation for a more somber reality that seems to be dawning on U.S. military leadership.

He sought to provide a reminder about why the United States went into Afghanistan, a narrative that is increasingly lost on much of the public. The war has dragged on so long that it is now fought by a generation of troops too young to remember the day when planes flown by members of al-Qaida, which had found protection in Afghanistan under the Taliban, struck the World Trade Center and the Pentagon.

Nicholson, 61, then echoed a call to immediately begin peace negotiations — an approach that has become a U.S. priority that the Trump administration hopes will allow it to diminish its presence in the country — while also warning the Taliban that the United States would continue to fight.

During his tenure, the longest by a U.S. commander in Afghanistan, he helped double the size of the Afghan special forces and triple that of the country’s air force. But he and his fellow generals tout a temporary rare cease-fire in June as a major success under his watch, a reflection of how much the goals and expectations of the U.S. military have changed.

His departure comes as the war seems to spiral deadlier even as it recedes from American attention. Nicholson did not meet once with President Donald Trump in the 19 months since Trump moved into the White House.

"Our soldiers are volunteers, permitting the American people and their elected representatives to be indifferent about the war in Afghanistan," said Karl Eikenbery, a former commander of the American forces in Afghanistan who later served as the U.S. ambassador to Kabul. "We continue to fight simply because we are there."

Like his predecessor, John F. Campbell, Nicholson is likely to retire immediately, a diplomat with ties to the general said. That's a sign that the posting is no longer a springboard to more senior roles.

Nicholson will be replaced by Gen. Austin "Scott" Miller, who left the shadowy Joint Special Operations Command to take on the war effort of a coalition to which 41 nations contribute. Defense officials described him as a "straight shooter" largely expected to approach the war with a more realistic attitude about defeating the Taliban than that of Nicholson, who on more than one occasion suggested that the United States had turned a corner in the war.