I can’t remember where I learnt this, but I use it all the time and it’s not at all obvious.
When in the minibuffer (at the prompt for
replace-regexp, for example), enter
C-q C-j for a newline (
Depending on the coding system for the buffer (DOS mode for example), you may also need to use
C-q C-m for carriage return (
CR) – suddenly it makes sense where all those
^Ms come from!