How to replace a character with a newline in Emacs?

Lei picture Lei · Mar 5, 2009 · Viewed 99.4k times · Source

I am trying to replace a character - say ; - with a new line using replace-string and/or replace-regexp in Emacs.

I have tried the following commands:

  • M-x replace-string RET ; RET \n

    This will replace ; with 2 characters: \n.

  • M-x replace-regex RET ; RET \n

    This results in the following error (shown in the minibuffer):

    Invalid use of `\' in replacement text.

What's wrong with using replace-string for this task? Is there any other way to do it?

Thanks.

Answer

Jonathan Arkell picture Jonathan Arkell · Mar 5, 2009

M-x replace-string RET ; RET C-q C-j.

  • C-q for quoted-insert,

  • C-j is a newline.

Cheers!