Shmuel II / II Samuel 22:1 · Pshat
וַיְדַבֵּר דָּוִד לַיהוָה אֶת־דִּבְרֵי הַשִּׁירָה הַזֹּאת בְּיוֹם הִצִּיל יְהוָה אֹתוֹ מִכַּף כָּל־אֹיְבָיו וּמִכַּף שָׁאוּל׃
And David spoke to the Eternal the words of this song on the day the Eternal rescued him from the hand of all his enemies and from the hand of Shaul (Saul).
Pshat פְּשָׁט
A song at the end of the road
Thanks for rescue often comes not at once but when the troubles are behind. The haftarah to the song of Moshe (Moses) is David's song, which he said looking back on his whole life.
Rashi: David said this song in old age, when all his troubles had passed and he had been saved from them all. Scripture names Shaul (Saul) separately, though he was among the enemies, because he pursued David more than all. Metzudat David adds a second reading: David sang this song every time he was saved from trouble.
How Rashi and Metzudat David explain it
Rashi On the day the Eternal rescued him
In his old age, after all his troubles had passed over him and he had been saved from them all.
Rashi And from the hand of Shaul
Was Shaul not included in the whole? But he was his enemy and pursuer more than all. Like this: "nineteen men and Asahel" (Shmuel II / II Samuel 2:30), like this: "Go, see the land and Yericho (Jericho)" (Yehoshua / Joshua 2:1), and like this: "And King Shlomo (Solomon) loved many foreign women, and the daughter of Pharaoh" (Melachim I / I Kings 11:1).
Metzudat David On the day He rescued
That is: in old age, when he had been saved from all troubles and his servants kept him from going out to war again, as written above. Then he said this song. Or: every time trouble came upon him and he was saved from it, he sang this song.
Takeaway
Chapter 22 of the Second Book of Samuel repeats Tehillim / Psalms 18 of the Book of Psalms almost word for word.
"As written above": in the war with the Philistines David grew faint (Shmuel II / II Samuel 21:15), and his men swore that he would no longer go out to war with them (Shmuel II / II Samuel 21:17).
The death of Asahel is named apart from the nineteen fallen (Shmuel II / II Samuel 2:30). Yericho is named apart from the whole land, the daughter of Pharaoh apart from all of Shlomo's wives.