Numbers 30:2(1) to 32:42, Jeremiah 1:1 to 2:3
The Midyanim were attacked to punish them for heeding Bilaam's advice and having their women (with the women of Moav) seduce the Israelites. Chapter 30 had just discussed who is responsible, and among other things taught that men of any age are responsible but girls who were still cared for by their parents (and virgins) were not responsible. Thus when Midyan was punished, Israel was allowed to spare to young girls. In contrast, the nations occupying the Promised Land were attacked simply because those nations were deeply evil, and Israel was told to spare no one.
Why isn't Moav attacked? It had recently been nearly defeated by the Emori (Amorites), which was stated in 21:26 and emphasized in the song of verses 21:27-30. It was struggling to regain its land: that song told of the city of Ar being captured by the Emori, but the Moavim had recaptured in by the time the Israelites arrived (Deuteronomy 2:29). Apparently, God had recently punished Moav and was now allowing its resotration (see Deuteronomy 2:9,19, and note that Moav and Amon were Lot's two children (Genesis 19:37-38)).
Note that Deuteronomy 2:29 says that Moav sold food and water to the Israelites, but Deuteronomy 23:3-4 says that God actually desired Moav to provide these provisions for free!
Recall how last week's Prasha introduced a future balance of power between Yehoshua and Elazar. After the battle with Midyan, Elazar suppliments Moshe's instruction to the returning soldiers. The balance of power is beginning—Moshe is no longer the only person explaining the commandments. (Rashi interprets verse 31:21 differently, and teaches that Moshe was so angry that he forgot to present the teaching, and Elazar covered for Moshe's error.)
Note Leviticus 19:18 forbids vengeance, but in this instance the vengeance is divinely ordained.
Psalm 119:106 says V'akai-aymah lee'shmor mee'sh'ptay tzeedke-cha neeshba-tee, "I have sworn, and shall fulfill, to heed Your righteous judgments." From this we learn it is permissable to swear to fulfill commandments.
Moshe was told that after this war he would die. But Moshe did not delay the war to try to live longer. Neither did Aaron try to avoid his death. Did Joshua? One tradition claims he dies at age 110 because he suspected he would die after capturing the Promised Land and did not rush to do so—thus his optimal age of 120 was reduced.
The Alshich explains that God, focused on Israel, described revenge for the sin of the Midyanim as "on behalf of the people of Israel". Moses, focused on God, called it "the Lord's vengeance". (31:2,3) Berditshever Rebbi says similarly "the festival of matza" (scriptural) highlights Israel's obedience while "Pesach" (more used) focuses on God's power.
If each tribe sent one thousand men to fight there would be 13,000 soldiers. But there were only 12,000. Most commentators suspect Levi did not fight and was thus not counted (Levi carried the ark with the army, see verse 31:6). Another traditional explanation conjuectures that Ephraim hesitated to participate because Yehoshua was from Ephraim, and the tribe, knowing that Moshe would die soon after the battle, did not want to seem impolitely eager to have Yehoshua lead the Israelites. According to this second explanation, the tribe of Levi went in place of Ephraim.
Rashi offers yet another explanation: there were 13,000 soldiers, but only 12,000 were vayimosru ("they were given over", verse 31:5). Rashi explains that the 12,000 were conscripted against their will, knowing that Moshe would die shortly after the war with Midyon. The 1,000 Levites, on the other hand, knew better than to hesitate to obey God, and did not need to be conscripted against there will.
Nehama Leibowitz offers an explanation for why Bilaam's role in seducing the Israelites to Baal worship and consorting with the daughters of Midian was not explained in verses 25:1-9: that offense was so terrible that the Torah does not want to appear to give any excuse for the Israelites' behavior. The Israelites were responsible for their behavior, even if they were being seduced. Bilaam's role was secondary to this truth.
Just as one fiftieth of the bnei Yisrael were conscripted for the war against Midyon, 12,000 out of 600,000, so too one fiftieth of the spoils were given to the Levites. (Kli Yokor)
For what crime do the soldiers offer the gold jewelry as atonement, in verse 21:50? The obvious answer is the crime of allowing the guilty, adult women of Midyan to live until Moshe corrected the soldiers. The Tz'enah Ur'enah offers an alternative lesson. It says that lust is more serious than the act of adultery, for both are unfaithfulness but lust is ongoing; the soldiers were chosen from among the Israelites who had not lusted after the women of Moav in verse 25:1, but these men were now confessing that they had entertained lustful thoughts about the women of Midyan.
In verse 14:3 the Israelites, based on the spies' report, were concerned for the safety of the women and children. Why are the women and children of Re'uven and Gad allowed to stay in safety? And why is there a paragraph break in the Hebrew after 32:4? The answer is that the real issue was how their request would appear to the other tribes. The pause after 32:4 was in hope that Moses would himself offer to let them stay. They offer to lead the army, assuming greatest risk and allowing no suspicion of cowardice. This compensates for their received luxuries.
In 32:4 the children of Re'uven and Gad claim that during the battles God goes before the people. In 32:20-22 Moses corrects them three times. God only moves when people are willing to step first on faith; the people go "before God". In 32:27 the children of Re'uven and Gad agree, having learned the lesson.
Rashi notes that the men of Re'uven and Gad speak of pens for their flocks before dwellings for their families, since their petition is sensible because of their flocks. In his reply Moshe gently corrects them, prioritizing the family members.
The Rambam suggests that after Re'uven and Gad finish negotiation their proposal they realize the land east of the Yarden is too big for them, so they invite Menasheh to join them. This suggestion is offered to explain why the tribe of Menasheh is suddenly mentioned in verse 32:33.
The Kli Yokor reads verse 32:1 differently, and believes that Reuven had very many cattle, whereas Gad have very strong cattle. Gad mentions building pens for its cattle first not because it put cattle before families as discussed above, but because they were offering to build pens beside the Yarden so their fierce cattle could serve as watchdogs for their eastward cities. Moshe's correction is then not to prioritize family above cattle, but to trust in God and not watch-bulls to protect your family.
In chapter 32:25-31 the words dovayr and tzaveh are both used to mean "command". Tradition teaches (based on examing other examples in Torah) that dovayr is used for a one-time command, whereas tzaveh is used for commands that pertain to all generations.