How can I replace a character with a newline in Emacs?

Viewed 116596

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 two 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 another way to do it?

6 Answers

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

  • C-q for quoted-insert,

  • C-j is a newline.

Don't forget that you can always cut and paste into the minibuffer.

So you can just copy a newline character (or any string) from your buffer, then yank it when prompted for the replacement text.

Inline just:

C-M-S-% (if the binding keys are still the default) and then replace-string ^J.

Related