The decay-time-dependent $CP$ asymmetry in $B^0 to D^{*pm}D^{mp}$ decays is measured using a data set corresponding to an integrated luminosity of $9$fb$^{-1}$ recorded by the LHCb detector in proton-proton collisions at centre-of-mass energies of 7, 8 and 13 TeV. The $CP$ parameters are measured as $ S_{D^*D} = -0.861 pm 0.077,text{(stat)} pm 0.019 ,text{(syst)},, $ $ Delta S_{D^*D} = 0.019 pm 0.075 ,text{(stat)} pm 0.012 ,text{(syst)} ,, $ $ C_{D^*D} = -0.059 pm 0.092 ,text{(stat)} pm 0.020 ,text{(syst)} ,, $ $ Delta C_{D^*D} = -0.031 pm 0.092 ,text{(stat)} pm 0.016 ,text{(syst)} ,, $ $ {cal A}_{D^*D} = 0.008 pm 0.014 ,text{(stat)} pm 0.006 ,text{(syst)},. $ The analysis provides the most precise single measurement of $CP$ violation in this decay channel to date. All parameters are consistent with their current world average values.