This is rooted in Abraham in Egypt, Isaac in Gerar, and (later) Jacob in Aram.
When Abraham goes to Egypt in Genesis 12, the rabbis teach that everything that happens gives us a pattern of the Exodus: it is seen as a prophetic journey, such that whatever Abraham does, so too will the children of Israel later on.
Here, Isaac goes to Gerar.
Now there was a famine in the land, besides the previous famine that had occurred in the days of Abraham. So Isaac went to Gerar, to Abimelech king of the Philistines. And the Lord appeared to him and said, “Do not go down to Egypt; stay in the land of which I shall tell you.
Genesis 26:1-2 (NASB)
You might think that Gerar, being the land of the Philistines, points to the time of the Judges and the Kings, when Israel struggles against the Philistine kingdoms.
But the rabbis say that Isaac’s journey here is actually about the Babylonian exile.
We already saw Babylon being obscured with names like Shinar and Chaldea. It’s quite possible that Babylon is again being obscured with the name Gerar.