Controlling the Rayleigh instability of nanowires