333

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lei
  • 3,381
  • 3
  • 18
  • 7

6 Answers6

511

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

  • C-q for quoted-insert,

  • C-j is a newline.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonathan Arkell
  • 10,526
  • 2
  • 24
  • 32
  • 3
    C-q is really `quote-insert` which quotes the next character. – Joe Casadonte Mar 05 '09 at 02:17
  • 3
    I believe it's actually "quoted" rather than "quote", as in "quoted-insert". At least that's the way it is on version 22.1. – Bryan Oakley Mar 05 '09 at 15:50
  • 10
    `C-j` is a literal `0x0a` control code, versus `Ret` which is the key next to your quote and sends `0x0d`. https://en.wikipedia.org/wiki/C0_and_C1_control_codes – Jonathan Arkell Jan 23 '14 at 18:28
  • 2
    Note for vim users, using `C-j` in **emacs**, while `C-m` in **vim**. –  Nov 06 '16 at 05:48
  • 3
    @JonathanArkell That's a description *how* it works, but not *why*. Within the editor, Ret produces 0x0a, so why should it produce 0x0d in quoted-insert? That doesn't make any sense. (Just because the internal keyboard code is 0x0d for historical reasons? We use key maps all over the place, why not here where it makes perfect sense?) – vog Sep 25 '17 at 12:44
  • Above works in emacs but not in spacemacs. How can it be done in spacemacs using emacs mode? – Wawrzyniec Pruski Apr 27 '20 at 15:45
94

There are four ways I've found to put a newline into the minibuffer.

  1. C-o

  2. C-q C-j

  3. C-q 12 (12 is the octal value of newline)

  4. C-x o to the main window, kill a newline with C-k, then C-x o back to the minibuffer, yank it with C-y

itsjeyd
  • 5,070
  • 2
  • 30
  • 49
slipmthgoose
  • 1,041
  • 7
  • 3
  • 12
    +1 for mentioning `C-o`! One less key stroke compared to `C-q C-j`. Hadn't thought of using that one before. – itsjeyd Apr 09 '14 at 09:53
  • Thanks for version 4! Helpful if you have C-o and C-q rebound. – Joachim W Jun 24 '14 at 06:58
  • Thanks a lot. Can you explain why C-o works and C-q C-j doesn't? – Christian Madsen Oct 24 '14 at 06:16
  • `C-o` doesn't move the point after inserting the newline in the minibuffer, so you still need to press the right arrow key as well if you want to insert something after the newline in the replacement string. – Robin Green Jan 27 '16 at 13:40
  • 1
    On my version of Emacs 25.2.2, `C-o` will enter a newline in the main window when using search, while `C-q C-j` will enter the newline in the minibuffer. `C-o` does work with query - replace though. – T.C. Proctor Apr 02 '20 at 21:50
29

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.

11

More explicitly:

To replace the semicolon character (;) with a newline, follow these exact steps.

  1. locate the cursor at the upper left of buffer the containing text you want to change

  2. Type m-x replace-string and hit Return

  3. The mini-buffer will display something like this: Replace string (default ^ -> ):

  4. Type in the character you want to replace. In this case, ; and hit Return

  5. The mini-buffer will display something like this:

    string ; with:

  6. Now execute C-q C-j

  7. All instances of semicolon will be replaced a newline (from the cursor location to the end of the buffer will now appear)

There is a bit more to it than the original explanation says.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
afc
  • 111
  • 1
  • 2
2

Switch to text mode:

M-x text-mode

Highlight the block to indent.

Indent: Ctrl + M </kbd>

Switch back to whatever mode...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
enkdr
  • 363
  • 5
  • 15
-2

Inline just:

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • What is meant by "inline"? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/58334096/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Oct 24 '21 at 18:50